关于window的resize事件


也许你也遇到过这样的问题,或者还没注意到有过这样的问题,如下代码,在窗口发生变化时,会进入死循环:

var _funResize = function(){
    console.log('resize...' + Math.random());
}
$(window).bind('resize',_funResize);

是问题,总得研究解决,方案:

var _funResize = function(){
    // 先解除绑定
    $(window).unbind('resize',_funResize);
   
    console.log('resize...' + Math.random());
   
    // 重新绑定
    setTimeout(function(){
        $(window).bind('resize',_funResize);
    },10);
}
$(window).bind('resize',_funResize);

但是这种方法只能是触发第一次resize事件,有时我们是想触发最后一次resize事件。可以通过时间来判断

        var rtime = new Date();
        var timeout = false;
        var delta = 200;
        $(window).resize(function() {
            rtime = new Date();
            if (timeout === false) {
                timeout = true;
                setTimeout(resizeend, delta);
            }
        });


        function resizeend() {
            if (new Date() - rtime < delta) {
                setTimeout(resizeend, delta);
            } else {
                timeout = false;
                alert($(window).height());
            }
        }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM