$(窗口).scroll函数无法在移动设备上运行

时间:2014-08-30 08:36:29

标签: javascript jquery scroll

我在我的网站上使用$(window).scroll事件触发功能,一切都很好,只是它不能在移动设备上运行。

这是我正在使用的代码:

$(window).scroll(function(){
    var wh =  $(window).height();
    var scrolledFromtop = $(window).scrollTop();
    if( scrolledFromtop > wh ){

        $('html').addClass('scrolled');
    }else{
        $('html').removeClass('scrolled');
    }
});

我也试过这段代码,但没有成功:

document.addEventListener("touchmove",aaa, false);
function aaa(){
    var wh =  $(window).height();
    var scrolledFromtop = $(window).scrollTop();
    if( scrolledFromtop > (wh) ){           
    $('html').addClass('scrolled');
    }

    else {
        $('html').removeClass('scrolled');
    }

};

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

有些(?)移动浏览器只是在滚动完全停止之前不会触发事件。

你可以尝试一些方法来解决这个问题:

  1. 使用setInterval
  2. 使用触动事件
  3. 另一个解决方案是完全禁用原生滚动,并使用JavaScript来模拟滚动。
  4. $('window').on('touchmove', function(event) { //Prevent the window from being scrolled. event.preventDefault();

       //Do something like call window.scrollTo to mimic the scrolling
       //request the user made.
    });
    

    你可以在这篇伟大的文章中找到更多相关信息:

    http://tjvantoll.com/2012/08/19/onscroll-event-issues-on-mobile-browsers/