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

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

WBOY
Release: 2016-06-06 20:18:52
Original
2038 people have browsed it

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>
Copy after login
Copy after login

参照这篇文章,关掉火狐平滑滚动,但是如果滚轮滑动的比较快,还是会出现问题。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>
Copy after login
Copy after login

参照这篇文章,关掉火狐平滑滚动,但是如果滚轮滑动的比较快,还是会出现问题。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>
Copy after login

原文

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template