【问题标题】:jquery scrollTop event on mobile not working移动设备上的jquery scrollTop事件不起作用
【发布时间】:2016-01-17 19:18:06
【问题描述】:

如果 scrollTop 值超过 122px,我有一个淡入的 div。

这在桌面上运行良好(浏览器窗口缩小到 500 像素以下以模拟移动屏幕尺寸)但在移动设备上不起作用。有解决办法吗?

我的 HTML:

<div id="topNav" class="fw">
  <div class="container">
    <div id="scrollLogo">
      <img src="images/dualLogoMob-scroll.png" width="63" height="30" alt=""/>
    </div><!-- /#scrollLogo-->
    <div class="twelve columns">
      <a href="#" onClick="return false;" id="mobMenuTrig" class="test">Menu</a>
    </div><!-- /.twelve.columns-->
  </div><!-- /.container-->
</div><!-- /#topNav-->

工作网址:http://www.altitude-digital.co.uk/dev/DUAL-SITE/index.php

我现在的 JS:

<script>
jQuery(document).ready(function(){
    jQuery("#scrollLogo").hide();
    jQuery(function () {
        jQuery(window).scroll(function () {
          if (jQuery(window).scrollTop() > 122) {
            jQuery('#scrollLogo').fadeIn();
          } else {
            jQuery('#scrollLogo').fadeOut();
          }
        });
    });
 });
</script>

【问题讨论】:

标签: jquery jquery-mobile scrolltop


【解决方案1】:

http://jsbin.com/voxace/

从#sitewrap 中删除overflow: scroll

为了让你的手指不用到处乱写"jQuery":

<script>
jQuery(function( $ ){ // DOM ready and $ alias secured

   // Free to use $ as usual

   var $scrLogo = $('#scrollLogo'); // Cache your selectors for expensive DOM lookup
   var $win = $(window);

   $scrLogo.hide();
   $win.on("scroll load", function () { // Do on scroll but also on win load!
     if ($(this).scrollTop() > 122) {
       $scrLogo.stop().fadeIn(); // Use stop() to prevent anim. buildups
     } else {
       $scrLogo.stop().fadeOut();
     }
   });

 });
</script>

【讨论】:

  • 啊,这只是我正在做的一个测试,但无济于事。我已经删除了这个 div 和 css
  • @jarrettowen 在移动设备上打开演示。它必须工作。
猜你喜欢
  • 2019-07-31
  • 1970-01-01
  • 1970-01-01
  • 2015-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多