JQuery: How to call RESIZE event only once it's FINISHED resizing?

后端 未结 8 1158
一向
一向 2020-11-22 07:29

How do I call a function once the browser windows has FINISHED resizing?

I\'m trying to do it like so, but am having problems. I\'m using the JQuery Resize

8条回答
  •  长情又很酷
    2020-11-22 08:06

    Just to add to the above, it is common to get unwanted resize events because of scroll bars popping in and out, here is some code to avoid that:

    function registerResize(f) {
        $(window).resize(function() {
            clearTimeout(this.resizeTimeout);
            this.resizeTimeout = setTimeout(function() {
                var oldOverflow = document.body.style.overflow;
                document.body.style.overflow = "hidden";
                var currHeight = $(window).height(),
                    currWidth = $(window).width();
                document.body.style.overflow = oldOverflow;
    
                var prevUndefined = (typeof this.prevHeight === 'undefined' || typeof this.prevWidth === 'undefined');
                if (prevUndefined || this.prevHeight !== currHeight || this.prevWidth !== currWidth) {
                    //console.log('Window size ' + (prevUndefined ? '' : this.prevHeight + "," + this.prevWidth) + " -> " + currHeight + "," + currWidth);
                    this.prevHeight = currHeight;
                    this.prevWidth = currWidth;
    
                    f(currHeight, currWidth);
                }
            }, 200);
        });
        $(window).resize(); // initialize
    }
    
    registerResize(function(height, width) {
        // this will be called only once per resize regardless of scrollbars changes
    });
    

    see jsfiddle

提交回复
热议问题