jQuery $(window).scroll(function() 在 Firefox 上不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9471286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
$(window).scroll(function() not working on firefox?
提问by Alvaro
In order to load pages as facebook or twitter does at their sites, scrolling down, i have tried this with jquery:
为了像 facebook 或 twitter 在他们的网站上那样加载页面,向下滚动,我用 jquery 尝试过这个:
$(window).scroll(function(){
if ($(window).scrollTop() == $(document).height() - $(window).height()){
//do something
}
}
And i am having troubles with Firefox. Not with Chrome and either with Safari (IE not tested yet).
我在使用 Firefox 时遇到了麻烦。不适用于 Chrome,也不适用于 Safari(IE 尚未测试)。
If i use an "alert('xx')" inside the IF, Firefox crashes and i have to restart it.
如果我在 IF 中使用“alert('xx')”,Firefox 就会崩溃,我必须重新启动它。
I am using the Mac version of Firefox but i guess that's not an important fact.
我正在使用 Firefox 的 Mac 版本,但我想这不是一个重要的事实。
Anyone have any idea about what's happening here? Thanks.
任何人都知道这里发生了什么?谢谢。
采纳答案by Devaroop
I have the same problem: it works in chrome but not in firefox and IE. After debugging i found that there is a 1 px difference between $(document).height() - $(window).height() and $(window).scrollTop() in firefox but not in chrome. My working solution here:
我有同样的问题:它适用于 chrome,但不适用于 Firefox 和 IE。调试后我发现 $(document).height() - $(window).height() 和 $(window).scrollTop() 在 Firefox 中存在 1 px 差异,但在 chrome 中没有。我的工作解决方案在这里:
if ($(window).scrollTop() + 1 >= $(document).height() - $(window).height()){
//do something here
}
回答by Jasper
The scroll
event is firing so many times that your Firefox browser is crashing. Instead of using alert()
, use console.log()
...
该scroll
事件触发了太多次,以至于您的 Firefox 浏览器崩溃了。而不是使用alert()
,使用console.log()
...
Here is a demo to show how many events are fired: http://jsfiddle.net/jasper/tQmRU/
这是一个演示,用于显示触发了多少事件:http: //jsfiddle.net/jasper/tQmRU/
回答by jasper_prikr
I know this is an really old article, but i was struggling with this today. I was trying to active an counter when it came into my viewport. It stopped working after i added some style to my style.css.
我知道这是一篇很老的文章,但我今天在努力解决这个问题。当它进入我的视口时,我试图激活一个计数器。在我向 style.css 添加一些样式后,它停止工作。
html {
scroll-behaviour: smooth
}
So, may this be in you favour, adding this will cause $(window).scroll function stop working on firefox and IE.
所以,这可能对你有利,添加它会导致 $(window).scroll 函数在 firefox 和 IE 上停止工作。
回答by Jenn
I had a similar problem occurring because I had put a Firebug breakpoint inside the scroll handler, not an alert()
. I instead used a console.log()
for debugging, and that did not cause crashing.
我遇到了类似的问题,因为我在滚动处理程序中放置了一个 Firebug 断点,而不是alert()
. 我改为使用 aconsole.log()
进行调试,这并没有导致崩溃。