5310 posts The Dude Abides
  • United States
  • Exclusive Author
  • Has been a member for 5-6 years
  • Elite Author
  • Bought between 100 and 499 items
  • Referred between 100 and 199 users
  • Author had a Free File of the Month
+5 more
CodingJack says

If you’ve ever used this:

http://paulirish.com/2009/throttled-smartresize-jquery-event-handler/

... you probably know about spastic resizing.

And for spastic browsers (Mac) this is great, but for browsers that aren’t spastic (Windows) it just isn’t needed. So tonight I figured out a way that satisfies both. Hope someone finds it useful.

$(document).ready(function() {

    var timer, prevW, prevH, fired, win = $(window);

    win.on("resize", resizer);

    // this function is called every time the browser resizes
    function resizer() {

        // catch the event for the first time, call the resize function
        if(!fired) sizeIt("cj");

        // use the "debounced" method for spastic resizing
        clearTimeout(timer);
        timer = setTimeout(sizeIt, 100);

    }

    // the actual resizing happens here
    function sizeIt(st) {

        var winWidth = win.width(),
        winHeight = win.height();

        // at the end of the function we store the windows width and height,
        // if we get the same exact values twice in a row, there's no need to continue
        // on a non-spastic browser, this will ensure that the actual resizing only happens once
        if(prevW === winWidth && prevH === winHeight) return;

        /////////////////////////////

        // DO RESIZE STUFF HERE        

        /////////////////////////////

        prevW = winWidth;
        prevH = winHeight;
        fired = st === "cj";

    }

});

The Benefit: Never a delay for resizing on a non-spastic browser, and resizing only happens once.
The Sacrifice: Content resizing will always happen twice (but only twice) on a spastic browser.

189 posts
  • Attended a Community Meetup
  • Author had a Free File of the Month
  • Grew a moustache for the Envato Movember competition
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Support Staff
  • Envato Staff
  • Reviewer
  • Envato Studio (Microlancer) Beta Tester
+6 more
Crusader12 Reviewer says

Thanks for posting this, can’t wait to test it out :)

by
by
by
by
by
by