javascript resize event on scroll - mobile

2019-01-07 09:07发布

I'm trying to make a website for mobile. I'm having the "resize" event binded on window, wich 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.

Any suggestions?

2条回答
成全新的幸福
2楼-- · 2019-01-07 09:21

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

Also see this answer.

Here link to a test page.

查看更多
混吃等死
3楼-- · 2019-01-07 09:29

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;
        }
    });
查看更多
登录 后发表回答