CSS-如何使单词末尾模糊?

4
我很喜欢Twitter在鼠标移动到带有消息的框上时模糊长名称的结尾。
我想尝试制作类似的东西,只是有一个小差别——我有一个DIV(假设宽度为100px),当内部文本超过100px时,相应文本的结尾将被模糊处理......
请问有人能给我一些示例/源代码/建议,如何实现这个效果?
谢谢 example

看这里渐变文字 - bart s
1
http://css-tricks.com/text-fade-read-more/ - js-coder
2个回答

10

使用以下HTML代码:

<div>
    <span>@someUserName</span>
</div>​

还有 CSS:

span {
    display: inline-block;
    position: relative;
}

span::after {
    position: absolute;
    top: 0;
    bottom: 0;
    right: 0;
    width: 3em;
    content: '';
    background: -moz-linear-gradient(left, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, right top, color-stop(0%,rgba(255,255,255,0)), color-stop(100%,rgba(255,255,255,1))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(left, rgba(255,255,255,0) 0%,rgba(255,255,255,1) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(left, rgba(255,255,255,0) 0%,rgba(255,255,255,1) 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(left, rgba(255,255,255,0) 0%,rgba(255,255,255,1) 100%); /* IE10+ */
    background: linear-gradient(left, rgba(255,255,255,0) 0%,rgba(255,255,255,1) 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#00ffffff', endColorstr='#ffffff',GradientType=1 ); /* IE6-9 */
}​

JS Fiddle演示

当然,您也可以在生成的内容中使用百分比宽度:JS Fiddle概念验证


在下面的评论中作出回应后进行编辑,报告称IE 8(以及可能更低版本)与以上解决方案不兼容:

  

但对我来说,在IE8中无法工作...

我更新了以下HTML:

<div>
    <span>@someUserName
        <!--[if ie]>
            <span class="ieFadeout"></span>
        <![endif]-->
    </span>
</div>

并更新了CSS:

div > span {
    display: inline-block;
    position: relative;
}

div > span::after {
    position: absolute;
    top: 0;
    bottom: 0;
    right: 0;
    width: 3em;
    content: '';
    background: -moz-linear-gradient(left, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, right top, color-stop(0%,rgba(255,255,255,0)), color-stop(100%,rgba(255,255,255,1))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(left, rgba(255,255,255,0) 0%,rgba(255,255,255,1) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(left, rgba(255,255,255,0) 0%,rgba(255,255,255,1) 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(left, rgba(255,255,255,0) 0%,rgba(255,255,255,1) 100%); /* IE10+ */
    background: linear-gradient(left, rgba(255,255,255,0) 0%,rgba(255,255,255,1) 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#00ffffff', endColorstr='#ffffff',GradientType=1 ); /* IE6-9 */
}

span > .ieFadeout {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    width: 3em;
    background-color: transparent;
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#00ffffff', endColorstr='#ffffff',GradientType=1 ); /* IE6-9 */

}​

JS Fiddle演示链接,在Win XP/IE 8和Chrome 18(与以前一样)中有效

参考:


我真的很喜欢这个解决方案!但在IE8中无法工作...你会推荐使用图像作为备选方案吗?或者有没有办法让它在所有浏览器中都能正常工作? - user359135
1
老实说,我不确定。我原本以为至少IE8应该能够处理这个CSS。我有一种感觉,你可能需要使用条件样式表和潜在的额外元素来处理内容,而不是生成内容。唉。 - David Thomas
我没有进行全面测试,只是当我在Chrome中打开您的jsfiddle时非常高兴,我想在IE中尝试一下..也许这是jsfiddle的问题? - user359135
1
不行,它在我的IE8测试中也失败了。叹气。不过,请查看编辑内容。我可以访问的WinXP/IE8上的后续版本可以工作;希望对您也有效。此外,它没有失去与其他浏览器的兼容性,所以应该没问题。尽管(不幸的是)更混乱。 - David Thomas

3
您可以在其上面放置另一个 DIV,并给它一个半透明的 PNG 背景,从完全透明到完全白色渐变。类似于这样:
<div style="position: relative; width: 100px">
    @GuyKawasaki
   <div style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; background-image:url(overlay.png)"></div>
</div>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接