如何垂直淡出文本,类似于亚马逊的产品描述

时间:2012-08-29 01:52:29

标签: javascript css

我试图将第一行的黑色文本块淡化为最后一行的浅灰色。我注意到亚马逊用它们的产品描述做了这件事。我试着查看CSS代码,我在那里看不到任何东西,我怀疑它是用javascript实现的。

我的问题是:如何在我自己的文本块上模拟类似的垂直淡化效果?

3 个答案:

答案 0 :(得分:7)

以下是由http://www.colorzilla.com/gradient-editor/生成的完全跨浏览器的解决方案:

DEMO:http://dabblet.com/gist/3511782

CSS:

#container {
    width: 400px; /*width of text box*/
    height: 200px; /*height of text box*/
    border: 2px solid black; /*it needs some kind of border to look good*/
    overflow: hidden; /*necessary to cut off text without scrollbars. alternatively you can use overflow: auto; or overflow: scroll; to show a scrollbar, but you'll have to tweak the #fader box*/
    position: relative; /*positioning so that #fader can be relative to this*/
}
#fader {
    height: 100px; /*from where it starts to fade to where it ends*/
    position: absolute; /*so it can overlap the #container*/
    width: 400px; /*has to be the same as #container*/
    margin-top: 25%; /*position it right at the bottom of the #container*/
    /* IE9 SVG, needs conditional override of 'filter' to 'none' */
    background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2ZmZmZmZiIgc3RvcC1vcGFjaXR5PSIwIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiNmZmZmZmYiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);
    background: -moz-linear-gradient(top,  rgba(255,255,255,0) 0%, rgba(255,255,255,1) 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(255,255,255,0)), color-stop(100%,rgba(255,255,255,1))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top,  rgba(255,255,255,0) 0%,rgba(255,255,255,1) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top,  rgba(255,255,255,0) 0%,rgba(255,255,255,1) 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top,  rgba(255,255,255,0) 0%,rgba(255,255,255,1) 100%); /* IE10+ */
    background: linear-gradient(to bottom,  rgba(255,255,255,0) 0%,rgba(255,255,255,1) 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#00ffffff', endColorstr='#ffffff',GradientType=0 ); /* IE6-8 */
}

HTML:

<div id="container"><div id="fader">&nbsp;</div>
Text goes here
</div>

对于IE9支持,您需要在head

中添加条件注释
<!--[if gte IE 9]>
  <style type="text/css">
    .gradient {
       filter: none;
    }
  </style>
<![endif]-->

如果您想知道,亚马逊的方式非常相似:http://pastebin.com/jkq3nbDJ

答案 1 :(得分:5)

所以你拥有的是一个绝对定位的div(在我下面的例子中为.fade-out)淡入淡出效果的高度,背景为png为垂直淡入淡出或css3渐变。

HTML

<div class="description">
    <p>Text Here</p>
    <div class="fade-out"></div>
</div>

CSS

.description { 
    position: relative;
    width: 100%;
}
.fade-out {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 2;
    height: 30px;
    background: ...
}

答案 2 :(得分:0)

有不同的方法可以做到这一点。 使用具有渐变背景的绝对定位的div或例如png是渐变的。