Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript resize event on scroll - mobile

I'm trying to make a website for mobile. I'm having the "resize" event bounded on window, which should rearrange elements when the mobile device is turning (portrait <-> landscape). On iPhone and Samsung Galaxy SII, the event is triggered when I'm scrolling down the page, and that's not good.

How can I fix this?

like image 666
gabitzish Avatar asked Feb 20 '12 13:02

gabitzish


1 Answers

Cache the width of the viewport and on resize return false if the width is still the same.

A small jQuery snippet:

    var cachedWidth = $(window).width();     $(window).resize(function(){         var newWidth = $(window).width();         if(newWidth !== cachedWidth){             //DO RESIZE HERE             cachedWidth = newWidth;         }     }); 
like image 128
sidonaldson Avatar answered Sep 18 '22 17:09

sidonaldson