Javascript get mobile screen width after orientati

2019-03-29 08:05发布

I am capturing the orientationchange event like this:

            $(window).bind( 'orientationchange', function(e){

            });

How can I get the new screen width and height in pixels after the orientationchage?

I have tried screen.width but this doesn't change, it remains as the initial width, even after orientation change.

4条回答
小情绪 Triste *
2楼-- · 2019-03-29 08:53

If you are using the meta tag viewport you could also update that tag (with jquery example:)

        function adapt_to_orientation() {
        var ori = window.orientation;
        var screenwidth = (ori==90 || ori==-90) ? screen.height : screen.width;
        var screenheight = (ori==90 || ori==-90) ? screen.width : screen.height;
                  // resize meta viewport
                  $('meta[name=viewport]').attr('content', 'initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no,width='+screenwidth + ',height=' + screenheight);
                }   
        adapt_to_orientation();
        $(window).bind( 'orientationchange', adapt_to_orientation);
查看更多
够拽才男人
3楼-- · 2019-03-29 09:03

Also you can use screen.availWidth. It gets changed with orientation.

查看更多
太酷不给撩
4楼-- · 2019-03-29 09:04
$(window).bind( 'orientationchange', function(e){
    var ori = window.orientation,
        width = (ori==90 || ori==-90) ? screen.height : screen.width;
});
查看更多
手持菜刀,她持情操
5楼-- · 2019-03-29 09:07

Use $(window).resize, it runs after orientationchange

$(window).resize(function(){
        //your logic
});
查看更多
登录 后发表回答