scrollTop给了我跨浏览器的问题

scrollTop is giving me cross-browser issue

本文关键字:浏览器 问题 scrollTop      更新时间:2023-09-26

下面是我的JavaScript,用于检测我是否在页面末尾,但当我在internet explorer或safari中运行它时,它会被激发两次,而同一脚本在firefox和chrome中运行良好。我不知道哪里出了问题?

$(window).scroll(function() {
     if ($(window).scrollTop() + $(window).height() == $(document).height()){ 
         somefunctionCall();
     } 
});

在Internet explorer中,滚动时可以多次触发滚动事件。您可以使用debounce(undercoreJs或jQuery)来确保事件不会频繁触发。

示例:

$(window).scroll(jQuery.debounce(100, function() {
     if ($(window).scrollTop() + $(window).height() == $(document).height()){ 
         functionCall(); 
     } 
}));

不鼓励使用滚动事件。请参阅:http://ejohn.org/blog/learning-from-twitter/