【问题标题】:changing scroll-based fade on mobile?在移动设备上更改基于滚动的淡入淡出?
【发布时间】:2016-03-20 14:34:12
【问题描述】:

我已将jsFiddle 链接到我正在使用的代码。淡入淡出滚动功能运行良好。我只想能够根据屏幕尺寸改变淡入淡出的速度。本质上,由于媒体查询的大小发生变化,我需要它比在较小的屏幕(手机/平板电脑)上更快地消失。

例如:能够调整这些数字...

      $('#one').css({'opacity':(( **300**-scroll )/ **300**)+0.4});

基于屏幕尺寸。

我在考虑 if/else 条件,但我对 JS 和 jQuery 非常陌生,无法弄清楚。谢谢你。

$(window).scroll(function() {    
        var scroll = $(window).scrollTop();
          $('#one').css({'opacity':(( 300-scroll )/ 300)+0.4});
          $('#two').css({'opacity':(( 600-scroll )/ 300)+0.4});
          $('#three').css({'opacity':(( 1100-scroll )/ 300)+0.4});
          $('#four').css({'opacity':(( 1400-scroll )/ 300)+0.4});
          $('#five').css({'opacity':(( 1700-scroll )/ 300)+0.4});
           
        });
#one{
  margin: 2em auto 2em auto;
  padding-top: 1em;
  height: 300px;
  width: 100%;
  background-color: coral;
  text-align: center;
  font-family: sans-serif;
  font-size: 2em;
  }
  
  #two{
  margin: 2em auto 2em auto;
  padding-top: 1em;
  height: 300px;
  width: 100%;
  background-color: coral;
  text-align: center;
  font-family: sans-serif;
  font-size: 2em;
  }
  
  #three{
  margin: 2em auto 2em auto;
  padding-top: 1em;
  height: 300px;
  width: 100%;
  background-color: coral;
  text-align: center;
  font-family: sans-serif;
  font-size: 2em;
  }
  
  #four{
  margin: 2em auto 2em auto;
  padding-top: 1em;
  height: 300px;
  width: 100%;
  background-color: coral;
  text-align: center;
  font-family: sans-serif;
  font-size: 2em;
  }
  
  #five{
  margin: 2em auto 2em auto;
  padding-top: 1em;
  height: 300px;
  width: 100%;
  background-color: coral;
  text-align: center;
  font-family: sans-serif;
  font-size: 2em;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>


<div id="one">
first fade element
</div>
<div id="two">
second fade element
</div>
<div id="three">
third fade element
</div>
<div id="four">
fourth fade element
</div>
<div id="five">
fifth fade element
</div>

【问题讨论】:

  • 欢迎来到stackoverflow :)
  • 谢谢!我一直在谷歌上搜索它,并潜伏着,所以我决定加入。也许有一天我会在这方面做得足够好来贡献哈哈。
  • 大声笑,你问了一个好问题:)

标签: javascript jquery html css


【解决方案1】:

检查滚动事件中的窗口宽度

$(window).scroll(function() {    
    var scroll = $(window).scrollTop();
    if($(this).width() > 500){ // if window width bigger than 500  change 500 with width you need
       $('#one').css({'opacity':(( 300-scroll )/ 300)+0.4});
       $('#two').css({'opacity':(( 600-scroll )/ 300)+0.4});
       $('#three').css({'opacity':(( 1100-scroll )/ 300)+0.4});
       $('#four').css({'opacity':(( 1400-scroll )/ 300)+0.4});
       $('#five').css({'opacity':(( 1700-scroll )/ 300)+0.4});
     }else{
            //if less than 500
     }  
});

【讨论】:

  • 就是这样。谢谢!
  • @tiffanyaych 祝你好运:)
  • @tiffanyaych 这更重要.. 如果您创建响应式模板,您可能还需要添加调整大小事件,以便您可以像 $(window).on('scroll resize' , function (){.......}); ..祝你好运:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-01
  • 2010-12-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多