0

我的一个主模板中有以下 jQuery 代码:

$(document).scroll(function() {
    var scroll_top = $(document).scrollTop();
    alert(scroll_top);
    if (scroll_top <= 70) {
        $('#fixedback').fadeOut(500);
    } else {
        $('#fixedback').fadeIn(500);
    }
});

当代码执行时,Firefox 11 和 12 将空白页面并变得无响应。我必须在任务管理器中终止该进程。如果我取出 alert(),代码会完美执行。如果我在任何 .scroll 函数中添加警报,我的任何页面都会发生同样的事情。该页面将加载并工作,直到我滚动页面。

使用 Jquery 1.7.1.min。和 C# ASPX 页面。我没有在其他浏览器上测试过,因为它只是为了开发,我需要警报才能工作。

4

1 回答 1

2

它看起来像 Firefox 中的一个错误。

问题:Firefox scrollTop 问题有一个可以在这里应用的答案。它的建议是你推迟alert()调用使用setTimeout(),让 Firefox 有机会做它需要做的任何事情,以避免空白页面。将解决方法应用于您的代码,您将得到如下内容:

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

function DoStuffOnScrollEvent() {
    var scroll_top = $(document).scrollTop();
    alert(scroll_top);
    if (scroll_top <= 70) {
        $('#fixedback').fadeOut(500);
    } else {
        $('#fixedback').fadeIn(500);
    }
};

alert()或者,您可以使用代替console.log(),它将在更高版本的 IE 和 Chrome 以及通过Firebug的 Firefox 中本地工作。

于 2012-04-27T16:50:29.347 回答