Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect window resize and perform function with jquery

How can I make it so that each time when user changes the window size, the page perform a function?

like image 330
Henry Avatar asked Aug 09 '11 17:08

Henry


1 Answers

The following function should do what you need, it works on all browsers (with the single exception of not firing when Safari is NOT maximized and resolution changes)

It fires upon window re-sizing as well as resolution change and also has a delay to avoid multiple calls while the user is re-sizing the window.

    var resizeTimer;

    //Event to handle resizing
    $(window).resize(function () 
    {
        clearTimeout(resizeTimer);
        resizeTimer = setTimeout(Resized, 100);
    });

    //Actual Resizing Event
    function Resized() 
    {
        //Your function goes here
    };

Testing jsFiddle

like image 154
Rion Williams Avatar answered Sep 28 '22 09:09

Rion Williams