Home >Backend Development >PHP Tutorial >javascript - 火狐浏览器不能正确执行$(window).on('scroll', function(){})?

javascript - 火狐浏览器不能正确执行$(window).on('scroll', function(){})?

WBOY
WBOYOriginal
2016-06-06 20:18:522031browse

chrome 正常,每滑一次滚轮,alert 1,2,3,4,5,6。。。 而火狐会莫名奇妙执行多次似的,第一次alert会输出不等值!

<code>


<meta charset="utf-8">
<title>无标题文档</title>


<div style="height:3000px;width:500px;background-color: green;margin-left: auto;margin-right: auto;"></div>

<script type="text/javascript" src="http://test-10017943.file.myqcloud.com/jquery_2_2.js"></script>
<script>
a = 0;
$(window).on('scroll', function(){
    b = a++;
    alert(b);
});
</script>

</code>

参照这篇文章,关掉火狐平滑滚动,但是如果滚轮滑动的比较快,还是会出现问题。http://mozilla.com.cn/forum.phpmod=viewthread&tid=35551&highlight=Scroll

回复内容:

chrome 正常,每滑一次滚轮,alert 1,2,3,4,5,6。。。 而火狐会莫名奇妙执行多次似的,第一次alert会输出不等值!

<code>


<meta charset="utf-8">
<title>无标题文档</title>


<div style="height:3000px;width:500px;background-color: green;margin-left: auto;margin-right: auto;"></div>

<script type="text/javascript" src="http://test-10017943.file.myqcloud.com/jquery_2_2.js"></script>
<script>
a = 0;
$(window).on('scroll', function(){
    b = a++;
    alert(b);
});
</script>

</code>

参照这篇文章,关掉火狐平滑滚动,但是如果滚轮滑动的比较快,还是会出现问题。http://mozilla.com.cn/forum.phpmod=viewthread&tid=35551&highlight=Scroll

throttle debounce

requestAnimationFrame + customEvent

<code>;(function() {
    var throttle = function(type, name, obj) {
        obj = obj || window;
        var running = false;
        var func = function() {
            if (running) { return; }
            running = true;
            requestAnimationFrame(function() {
                obj.dispatchEvent(new CustomEvent(name));
                running = false;
            });
        };
        obj.addEventListener(type, func);
    };

    /* init - you can init any event */
    throttle ("scroll", "optimizedScroll");
})();

// handle event
window.addEventListener("optimizedScroll", function() {
    console.log("Resource conscious scroll callback!");
});
</code>

原文

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn