Firefox 滚动顶部问题
我对 Firefox 的scrollTop 值和onscroll 事件有疑问。这在 IE、Safari 和 Chrome 中效果很好,但 Firefox 似乎滞后。
我尝试使用onscroll事件更新一些背景位置,但是当我拿起手柄并快速上下拖动它时,Firefox停止更新scrollTop值,并导致我的应用程序出现一些滞后。
您可以尝试此代码,并在拖动手柄时查看 Firefox 控制台,您将看到某些值停止更新:
function SaveScrollLocation () {
console.log(document.documentElement.scrollTop);
}
window.onscroll=SaveScrollLocation ;
知道如何使 Firefox 响应更快吗?
I have a problem with Firefox scrollTop value and onscroll event. This works great in IE, Safari and Chrome but Firefox seems to lag.
I tried to update some background position with the onscroll event, but when I take the handle and drag it up and down quickly, Firefox stops updating the scrollTop value and it causes some lag in my app.
You can try this code and look in the Firefox console when dragging the handle and you will see the values something stops the updating :
function SaveScrollLocation () {
console.log(document.documentElement.scrollTop);
}
window.onscroll=SaveScrollLocation ;
Any idea how to make Firefox respond more quickly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有两种方法可以处理此问题 - 节流(以设定的时间间隔执行函数)和去抖动(自上次调用以来指定的时间之后执行函数)。您可能希望在您的情况下使用限制。
简化的解决方案可能看起来像这样(更新:参见http://jsfiddle .net/yVVNU/1/):
您还可以使用此 jQuery 插件:http://benalman.com/projects/jquery-throttle-debounce-plugin/
There are two ways to handle this - throttle (execute the function with a set interval) and debounce (execute the function after the specified time has passed since the last call). You'll probably want to use throttling in your situation.
A simplified solution may look something like this (Updated: see it at http://jsfiddle.net/yVVNU/1/):
You can also use this jQuery plugin: http://benalman.com/projects/jquery-throttle-debounce-plugin/
$(window).scrollTop()
为我工作$(window).scrollTop()
worked for me快速上下拖动窗口的行为难道不会被认为是异常吗?
在我看来,如果用户这样做,我不想保存状态。我宁愿等到窗口在同一位置至少 250 毫秒后再记录其位置。当用户上下猛击滚动条时位置的微小变化对用户来说可能不是很重要,明白我的意思吗?
借助一点
setTimeout
魔法,您能否回避这个问题,并通过在清除滚动位置之前不触发SaveScrollLocation
来使您的脚本在浏览器 UI 上变得更轻一些吗?值得节省吗?Wouldn't the behavior of dragging the window up and down quickly be considered abnormal?
In my view, I wouldn't want to be saving the state if the user is doing that. I'd rather wait until the window has been in the same spot for at least 250ms before recording it's position. The minor variances in position while the user is slamming the scrollbar up and down are probably not very important to the user, know what I mean?
With a little
setTimeout
magic, couldn't you sidestep this issue AND make your script a little lighter on the browser UI by not firing theSaveScrollLocation
until it clear the scroll location is WORTH saving?Firefox 不像其他浏览器那样频繁地触发
onscroll
事件。 参见此处有趣的是,
scrollTop
确实在正确的位置更新频率,因此您可以使用其他事件,例如mousemove
。我所做的是这样的:在第一个滚动事件上,开始侦听鼠标移动事件 - 根据正确更新的
scrollTop
更新您想要的任何内容。在onscroll
后经过短暂的超时后,停止侦听鼠标移动事件。Firefox does not (or did not used to) fire the
onscroll
event as frequently as the other browsers. see hereInterestingly the
scrollTop
does update at the correct frequency so you can probably use another event such asmousemove
. What i did was something like this :on first scroll event, start listening to mouse move events - update whatever it is you want to based on the
scrollTop
which does update correctly. After a short timeout has elapsed after anonscroll
, stop listening for mouse move events.