我在我的网站上使用$(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');
}
};
有人可以帮我吗?
答案 0 :(得分:0)
有些(?)移动浏览器只是在滚动完全停止之前不会触发事件。
你可以尝试一些方法来解决这个问题:
$('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/