简体   繁体   English

scrollTop给我跨浏览器的问题

[英]scrollTop is giving me cross-browser issue

Below is my JavaScript for detecting if I am at the end of a page, but it is fired twice when I am running it in internet explorer or safari, while the same script is working fine in firefox and chrome. 下面是我的JavaScript,用于检测我是否位于页面末尾,但是当我在Internet Explorer或safari中运行它时,它会被触发两次,而相同的脚本在firefox和chrome中可以正常工作。 I am not able to figure it out where it might have went wrong? 我无法弄清楚哪里可能出错了?

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

In Internet explorer the scroll event can be fired multiple times when scrolling. 在Internet Explorer中,滚动时可以触发滚动事件多次。 You can use debounce (underscoreJs or jQuery) to make sure the event isn't fired too often. 您可以使用debounce(underscoreJs或jQuery)来确保事件不会被频繁触发。

Example: 例:

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

It is discouraged to use the scroll events. 不推荐使用滚动事件。 See: http://ejohn.org/blog/learning-from-twitter/ 请参阅: http//ejohn.org/blog/learning-from-twitter/

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

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