Firefox 滚动顶部问题

发布于 2024-10-17 22:27:44 字数 411 浏览 9 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

感情废物 2024-10-24 22:27:44

有两种方法可以处理此问题 - 节流(以设定的时间间隔执行函数)和去抖动(自上次调用以来指定的时间之后执行函数)。您可能希望在您的情况下使用限制。

简化的解决方案可能看起来像这样(更新:参见http://jsfiddle .net/yVVNU/1/):

    window.onscroll=catchScroll;
    var timeOutId = 0;
    var jitterBuffer = 200;
    function catchScroll()
        {
            if (timeOutId) clearTimeout (timeOutId);
            timeOutId = setTimeout(function(){SaveScrollLocation()}, jitterBuffer);
        }

    function SaveScrollLocation () {
        console.log(document.documentElement.scrollTop);
        alert('scrolled');
    }

您还可以使用此 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/):

    window.onscroll=catchScroll;
    var timeOutId = 0;
    var jitterBuffer = 200;
    function catchScroll()
        {
            if (timeOutId) clearTimeout (timeOutId);
            timeOutId = setTimeout(function(){SaveScrollLocation()}, jitterBuffer);
        }

    function SaveScrollLocation () {
        console.log(document.documentElement.scrollTop);
        alert('scrolled');
    }

You can also use this jQuery plugin: http://benalman.com/projects/jquery-throttle-debounce-plugin/

私野 2024-10-24 22:27:44

$(window).scrollTop() 为我工作

$(window).scrollTop() worked for me

梦初启 2024-10-24 22:27:44

快速上下拖动窗口的行为难道不会被认为是异常吗?

在我看来,如果用户这样做,我不想保存状态。我宁愿等到窗口在同一位置至少 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 the SaveScrollLocation until it clear the scroll location is WORTH saving?

初雪 2024-10-24 22:27:44

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 here

Interestingly the scrollTop does update at the correct frequency so you can probably use another event such as mousemove. 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 an onscroll, stop listening for mouse move events.

盗心人 2024-10-24 22:27:44
var last = +new Date;

function SaveScrollLocation () {
    var now = +new Date;
    if (now - last > 50) {
      // ...
      last = now;
    }
}

window.onscroll = SaveScrollLocation ;
var last = +new Date;

function SaveScrollLocation () {
    var now = +new Date;
    if (now - last > 50) {
      // ...
      last = now;
    }
}

window.onscroll = SaveScrollLocation ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文