jquery - 在被溢出剪切之前选择并修改最后一个字符 : hidden

标签 jquery css overflow

我正在尝试创建淡出效果,以便标题栏中的最后 5 或 6 个字符在接近容器边缘时逐渐变得更加透明。

我不能根据字符数来确定这一点,因为页面是响应式的并且可以是任何宽度。我也不能只在它上面叠加一个渐变,因为背景上已经有一个渐变,这会让这一点变得明显,除非我可以将 CSS 从透明渐变为颜色,我认为这是不可能的。

我的希望是我可以以某种方式选择最接近截止点的字符(在它们被父元素上的溢出隐藏之前),如下所示:

enter image description here

我尝试通过“:last”选择器执行此操作,但只能定位标签:

$('#scalable:visible:last').css('opacity','.5');

这是我在 jsfiddle 中的第一次尝试:http://jsfiddle.net/adamnelson/PgerN/

修复是纯CSS:

#scalable {
    background: #408800; /* Old browsers */
    background: -webkit-gradient(linear, 0 0, 0 100%, from(#408800), to(#316600));
    background: -webkit-linear-gradient(#408800 0%, #316600 100%);
    background: -moz-linear-gradient(#408800 0%, #316600 100%);
    background: -o-linear-gradient(#408800 0%, #316600 100%);
    background: linear-gradient(#408800 0%, #316600 100%); /* FF3.6+ */ /* Chrome,Safari4+ */ /* Chrome10+,Safari5.1+ */ /* Opera 11.10+ */ /* IE10+ */ /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#408800', endColorstr='#316600',GradientType=0 ); /* IE6-9 */

    color: white;
    font-size: 20px;
    font-weight: bold;
    margin: 50px auto 0;
    padding: 1em;
    overflow: hidden;
    width: 80%;
    position: relative;
}

#scalable:after {
    content: " ";
    position: absolute;
    height: 100%;
    width: 140px;
    top: 0;
    right: 0;
    background: -moz-linear-gradient(left, rgba(49,102,0,0) 0%, rgba(49,102,0,0.56) 51%, rgba(49,102,0,1) 100%);
    background: -webkit-gradient(linear, left top, right top, color-stop(0%,rgba(49,102,0,0)), color-stop(51%,rgba(49,102,0,0.56)), color-stop(100%,rgba(49,102,0,1)));
    background: -webkit-linear-gradient(left,  rgba(49,102,0,0) 0%,rgba(49,102,0,0.56) 51%,rgba(49,102,0,1) 100%);
    background: -o-linear-gradient(left, rgba(49,102,0,0) 0%,rgba(49,102,0,0.56) 51%,rgba(49,102,0,1) 100%);
    background: -ms-linear-gradient(left, rgba(49,102,0,0) 0%,rgba(49,102,0,0.56) 51%,rgba(49,102,0,1) 100%);
    background: linear-gradient(to right, rgba(49,102,0,0) 0%,rgba(49,102,0,0.56) 51%,rgba(49,102,0,1) 100%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#00408800', endColorstr='#408800',GradientType=1 );
}

记录在这个 fiddle 中:http://jsfiddle.net/PgerN/2/

最佳答案

您介意在这里使用纯 CSS 解决方案吗?

我使用了具有不透明度的 CSS 水平渐变,并且还使用了 :after 选择器,这样您就不必在当前语法中添加另一个元素,这在以下情况下非常方便您无权访问 HTML 或者由于某种原因无法更改它..但我还想告诉您 :after 不支持 IE8,在这种情况下您需要嵌套#scalable

中的元素

Demo

#scalable {
    background-color: #408800;
    color: white;
    font-size: 20px;
    font-weight: bold;
    height: 65px;
    margin: 50px auto 0;
    padding: 0 1em;
    overflow: hidden;
    width: 80%;
    position: relative;
}

#scalable:after {
    content: " ";
    position: absolute;
    height: 100%;
    width: 100%;
    top: 0;
    background: -moz-linear-gradient(left,  rgba(64,136,0,0) 0%, rgba(64,136,0,0) 74%, rgba(64,136,0,1) 100%);
    background: -webkit-gradient(linear, left top, right top, color-stop(0%,rgba(64,136,0,0)), color-stop(74%,rgba(64,136,0,0)), color-stop(100%,rgba(64,136,0,1)));
    background: -webkit-linear-gradient(left,  rgba(64,136,0,0) 0%,rgba(64,136,0,0) 74%,rgba(64,136,0,1) 100%);
    background: -o-linear-gradient(left,  rgba(64,136,0,0) 0%,rgba(64,136,0,0) 74%,rgba(64,136,0,1) 100%);
    background: -ms-linear-gradient(left,  rgba(64,136,0,0) 0%,rgba(64,136,0,0) 74%,rgba(64,136,0,1) 100%);
    background: linear-gradient(to right,  rgba(64,136,0,0) 0%,rgba(64,136,0,0) 74%,rgba(64,136,0,1) 100%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#00408800', endColorstr='#408800',GradientType=1 );
}

如果你看到this example,你可以根据自己的需要调整渐变。 ,我比之前的拉伸(stretch)了很多。

是的,渐变代码确实看起来很可怕,但你有一个 handy tool here用于创建渐变,可以很好地处理它们。

注意:通过使用此功能,当您在文本上叠加元素时,它将禁用文本选择,因此为了防止用户方便,您可以制作类似的内容

#scalable:hover:after {
    display: none;
}

Demo 3 (将鼠标悬停以选择文本)

关于jquery - 在被溢出剪切之前选择并修改最后一个字符 : hidden,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17840752/

相关文章:

css - 为什么我的视频栏没有结束?

jquery - 由于在类似画廊的灯箱上使用,避免在图像悬停时使用标题标签

lua - redis lua位溢出

c - 如何在发生之前或之后检测 GMP 中的尾数精度溢出?

javascript - 通过 URL 直接链接到打开的模态窗口?

javascript - Html导出Excel - 浏览器直接保存Excel,无法以 "View"模式打开,使用IE9

javascript - 使 Arc Canvas 响应

html - IE 正在改变我的图像的背景颜色...嗯?

css - 样式表 - 表布局 :fixed vs td-width:20%

javascript - 滚动到顶部 jquery