简体   繁体   English

jQueryscrolltop功能在手机上不起作用

[英]Jquery scrolltop function not working on mobile

Im creating a navbar that will fade in when I scroll a little bit down the page. 我创建了一个导航栏,当我向下滚动页面时,该导航栏将逐渐消失。 My code works fine on desktop, but it does not work when I switch to mobile. 我的代码在台式机上可以正常工作,但是当我切换到移动设备时,它无法正常工作。

Here is the javascript: 这是JavaScript:

 $(document).ready(function(){
    $(window).bind('scroll', function() {
        var distance = 100;

        if ($(window).scrollTop() > 50) {
            $('nav').css("background-color","rgba(6, 14, 49, 0.94)");
        }
        else {
          $('nav').css("background-color","rgba(6, 14, 49, 0.50)");
        }
   });
});

Thanks for any and all suggestions. 感谢您提出的所有建议。 I already tried switching $(window) to $('body') or $('html') and they didnt work 我已经尝试将$(window)切换为$('body')或$('html'),但它们没有起作用

You can use $().scroll method to implement the desired result. 您可以使用$()。scroll方法来实现所需的结果。

$(window).scroll(function() {  
      var distance = 50;
        if ($(window).scrollTop() > 50) {
            $('nav').css("background-color","rgba(6, 14, 49, 0.94)");
        }
        else {
          $('nav').css("background-color","rgba(6, 14, 49, 0.50)");
        }
   });      
});

On mobile, the "scroll" event will be fired only when the scroll stops. 在移动设备上,只有在滚动停止时才会触发“滚动”事件。 for example, if you have a big slide on your mobile phone, and the window keep scrolling for 5 seconds, then the scroll event will be fired after 5 seconds. 例如,如果您的手机滑得很大,并且窗口持续滚动5秒钟,则滚动事件将在5秒钟后触发。

Therefore, unfortunately, you cannot monitor the offset while the window is scrolling. 因此,不幸的是,您无法在窗口滚动时监视偏移量。

as an alternative, try to check some plugin that fake the scrolling which allow you to "monitor" the offset while "scrolling" . 或者,尝试检查一些假冒滚动的插件,该插件允许您在“滚动”时“监视”偏移量。 eg : https://github.com/cubiq/iscroll 例如: https : //github.com/cubiq/iscroll

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM