Snippets

Timothy Anderson $.resizeEnd

Created by Timothy Anderson last modified

Use to wait until window has finished resizing before executing scripts:

$(window).on('width.resizeEnd.zion', function() {
    // do stuff when the window width changes
});

$(window).on('height.resizeEnd.zion', function() {
    // do stuff when the window height changes
})

$(window).on('resizeEnd.zion', function() {
    // do stuff when the window is resized in any direction
})
(function($) {
    var resizeTimer = setTimeout(function(){}, 1); // utility timer
    var windowWidth = $(window ).width();
    var windowHeight = $(window ).height();
    $(window).add($('html, body')).on('resize', function() {
        var $window = $(window);
        clearTimeout(resizeTimer);
        resizeTimer = setTimeout(function() {
            if($window.width() !== windowWidth || $window.height() !== windowHeight) {
                $window.trigger('resizeEnd.zion');
            }
            if($window.height() !== windowHeight) {
                windowHeight = $window.height();
                $window.trigger( 'height.resizeEnd.zion' );
            }
            if($window.width() !== windowWidth) {
                windowWidth = $window.width();
                $window.trigger('width.resizeEnd.zion');
            }
        }, 200);
    });
})(jQuery);

Comments (0)