Javascript get mobile screen width after orientation change

此生再无相见时 提交于 2019-12-09 12:13:08

问题


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.


回答1:


$(window).bind( 'orientationchange', function(e){
    var ori = window.orientation,
        width = (ori==90 || ori==-90) ? screen.height : screen.width;
});



回答2:


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




回答3:


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

$(window).resize(function(){
        //your logic
});



回答4:


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);


来源:https://stackoverflow.com/questions/13200135/javascript-get-mobile-screen-width-after-orientation-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!