30

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?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
gabitzish
  • 9,535
  • 7
  • 44
  • 65

2 Answers2

53

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;
        }
    });
sidonaldson
  • 24,431
  • 10
  • 56
  • 61
9

Use the onOrientationChange event and the window.orientation property instead.

Also see this answer.

Here link to a test page.

Community
  • 1
  • 1
scessor
  • 15,995
  • 4
  • 43
  • 54