在背景的顶部上滚动梯度褪色的文字(Gradient fading text on scroll on

2019-09-21 14:18发布

一直在寻找一种方式来淡入顶部和底部滚动。 我知道你在哪里使用2 PNG在顶部和底部的方式,问题是只似乎是有用的,当在纯色滚动页面。 但我想它在有背景图像页面上发生..

有任何想法吗? 如果它甚至可能吗? JS,HTML5,CSS?

这里是一个图片来描述的效果:

Answer 1:

这可能是你在找什么。

DEMO

的jsfiddle

HTML

<div class="wrap">
    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna.</p>
    <div class="fader"></div>
</div>​

CSS

.wrap {
    position: relative;
    background: url(http://lorempixel.com/output/fashion-q-c-640-480-3.jpg);
    font-family: Helvetica, 'Helvetica Neue', sans-serif;
}

.wrap p {
    color: white;
}

.fader {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(top, rgba(0,0,0,0.7) 0%, rgba(0,0,0,0) 30%, rgba(0,0,0,0) 70%, rgba(0,0,0,0.7) 100%);
    background: -webkit-linear-gradient(top, rgba(0,0,0,0.7) 0%, rgba(0,0,0,0) 30%, rgba(0,0,0,0) 70%, rgba(0,0,0,0.7) 100%);
    background: -moz-linear-gradient(top, rgba(0,0,0,0.7) 0%, rgba(0,0,0,0) 30%, rgba(0,0,0,0) 70%, rgba(0,0,0,0.7) 100%);
    pointer-events: none;
}

编辑:增加了pointer-events: none以CSS允许正常的文本选择。



Answer 2:

Here is something I tried and seems to work:

<html>
<head>
    <title></title>
    <style type="text/css">
        div.bg 
        {
            position: absolute;
            top: 0px;
            left: 0px;
            z-index: 0;
            width: 800px;
            height: 600px;
            background: url([your_background_image]);
        }

        div.txt 
        {
            position: absolute;
            top: 0px;
            left: 50px;
            z-index: 2;
            width: 700px;
            height: 600px;
            font-family: Arial;
            font-size: 30px;
            color: #FFFFFF;
            font-weight: bold;
        }

        div.fader_in
        {
            position: absolute;
            top: 0px;
            left: 0px;
            z-index: 4;
            width: 800px;
            height: 100px;
            filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#000000', endColorstr='#FFFFFF'); /* for IE */
            background: -webkit-gradient(linear, left top, left bottom, from(#000000), to(#FFFFFF)); /* for webkit browsers */
            background: -moz-linear-gradient(top,  #000000,  #FFFFFF); /* for firefox 3.6+ */ 
            opacity: .5;
            -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";
        }

        div.between
        {
            position: absolute;
            top: 100px;
            left: 0px;
            z-index: 4;
            width: 800px;
            height: 400px;
            background: #FFFFFF;
            opacity: .5;
            -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";
        }

        div.fader_out
        {
            position: absolute;
            top: 500px;
            left: 0px;
            z-index: 4;
            width: 800px;
            height: 100px;
            filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#FFFFFF', endColorstr='#000000'); /* for IE */
            background: -webkit-gradient(linear, left top, left bottom, from(#FFFFFF), to(#000000)); /* for webkit browsers */
            background: -moz-linear-gradient(top,  #FFFFFF,  #000000); /* for firefox 3.6+ */ 
            opacity: .5;
            -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";
        }
    </style>
</head>
<body>
    <div class="bg">
    </div>
    <div class="txt">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    </div>
    <div class="fader_in"></div>
    <div class="between"></div>
    <div class="fader_out"></div>
</body>
</html>


Answer 3:

您可以使用在顶部和底部相匹配的BG的颜色透明灰梯度图像: http://www.cssplay.co.uk/menu/fade_scroll.html



Answer 4:

有趣的问题......烦人这是不可能做到这一点在这不会引起可用性问题的好办法 - 即像应用不透明蒙板实际文本层。 然而,你可以用一些沉重的JavaScript /帆布实现它,或者您可以使用此方法狂(这只会工作,如果你可以让你的背景图片是background-attachment设置为fixed ):

<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">

  body {
    background: url(http://maxcdn.crazyleafdesign.com/blog/wp-content/uploads/2008/09/cool-wallpapers-for-designers-29.jpg) fixed;
  }

  #scrollable {
    position: relative;
    width: 400px;
    height: 200px;
    margin: 50px;
  }

  #scrollable .viewport {
    position: relative;
    overflow: auto;
    width: 100%;
    height: 100%;
  }

  #scrollable .text {
    color: #fff;
  }

  #scrollable .text p {
    margin: 0; padding: 0; margin-bottom: 20px;
  }

  #scrollable .top {
    position: absolute; left: 0; top: 0; right: 0; height: 40px;
  }

  #scrollable .bot {
    position: absolute; left: 0; bottom: 0; right: 0; height: 40px;
  }

  #scrollable .top div, #scrollable .bot div {
    position: relative;
    background: url(http://maxcdn.crazyleafdesign.com/blog/wp-content/uploads/2008/09/cool-wallpapers-for-designers-29.jpg) fixed;
    height: 2px;
    border: 0;
  }

  #scrollable .l01 { opacity: 1; }
  #scrollable .l02 { opacity: 0.95; }
  #scrollable .l03 { opacity: 0.9; }
  #scrollable .l04 { opacity: 0.85; }
  #scrollable .l05 { opacity: 0.8; }
  #scrollable .l06 { opacity: 0.75; }
  #scrollable .l07 { opacity: 0.7; }
  #scrollable .l08 { opacity: 0.65; }
  #scrollable .l09 { opacity: 0.6; }
  #scrollable .l10 { opacity: 0.55; }
  #scrollable .l11 { opacity: 0.5; }
  #scrollable .l12 { opacity: 0.45; }
  #scrollable .l13 { opacity: 0.4; }
  #scrollable .l14 { opacity: 0.35; }
  #scrollable .l15 { opacity: 0.3; }
  #scrollable .l16 { opacity: 0.25; }
  #scrollable .l17 { opacity: 0.2; }
  #scrollable .l18 { opacity: 0.15; }
  #scrollable .l19 { opacity: 0.1; }
  #scrollable .l20 { opacity: 0.05; }

</style>
</head>
<body>
  <div id="scrollable">
    <div class="viewport">
      <div class="text">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse et blandit ligula. Vestibulum sodales porttitor sollicitudin. Etiam turpis purus, tincidunt rhoncus interdum ut, mollis vitae mi. Nulla a mauris et orci scelerisque egestas a nec metus. Cras a massa id quam vehicula sodales. Etiam aliquam justo eu ligula volutpat blandit. Sed sagittis, arcu ut pulvinar blandit, purus mauris euismod felis, eget rutrum nunc arcu vitae augue. Cras magna quam, consectetur at lobortis quis, interdum eget tortor.</p>
        <p>Pellentesque tincidunt molestie urna, ut lacinia justo cursus a. Nunc mauris sem, dignissim id mollis vel, adipiscing nec purus. Fusce feugiat sem et neque cursus pretium. Curabitur tellus nunc, blandit sit amet aliquam tempus, scelerisque eu nulla. Vestibulum imperdiet nunc nec purus dignissim vitae suscipit orci pharetra. Mauris sit amet ante sit amet odio ultricies interdum auctor sit amet nibh. Integer ut sem leo, at vulputate felis. Mauris sit amet neque tortor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</p>
        <p>Mauris lectus leo, luctus non malesuada vitae, facilisis vitae est. Maecenas sed neque lorem, vitae dignissim ante. Cras non lectus sed orci ornare ultricies at ac lectus. Donec quis quam mi, id ornare mauris. Praesent aliquet turpis hendrerit dui fringilla ullamcorper. Morbi facilisis urna non dui bibendum non semper neque dignissim. Vestibulum non pretium justo. Phasellus magna velit, commodo ut facilisis et, hendrerit vitae sem. Curabitur commodo eros a dolor sagittis egestas. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
      </div>
    </div>
    <div class="top">
      <div class="l01"></div><div class="l02"></div><div class="l03"></div>
      <div class="l04"></div><div class="l05"></div><div class="l06"></div>
      <div class="l07"></div><div class="l08"></div><div class="l09"></div>
      <div class="l10"></div><div class="l11"></div><div class="l12"></div>
      <div class="l13"></div><div class="l14"></div><div class="l15"></div>
      <div class="l16"></div><div class="l17"></div><div class="l18"></div>
      <div class="l19"></div><div class="l20"></div>
    </div>
    <div class="bot">
      <div class="l20"></div><div class="l19"></div><div class="l18"></div>
      <div class="l17"></div><div class="l16"></div><div class="l15"></div>
      <div class="l14"></div><div class="l13"></div><div class="l12"></div>
      <div class="l11"></div><div class="l10"></div><div class="l09"></div>
      <div class="l08"></div><div class="l07"></div><div class="l06"></div>
      <div class="l05"></div><div class="l04"></div><div class="l03"></div>
      <div class="l02"></div><div class="l01"></div>
    </div>
  </div>
</body>
</html>

我不建议对任何使用这些方法发布至今不过(包括我),因为它们都具有文本选择和滚动条干扰,由于上面的文字浮动层。 这可能会导致的可用性问题的所有方式,基本上只是惹恼你的用户......这不是大多数网站的目的;)



文章来源: Gradient fading text on scroll on top of background