Mobile viewport height after orientation change

后端 未结 10 1402
孤街浪徒
孤街浪徒 2020-12-03 04:30

I am attaching a listener to the orientationchange event:

window.addEventListener(\'orientationchange\', function () {
    console.log(window.innerHeight);
}         


        
相关标签:
10条回答
  • 2020-12-03 05:07

    Use the resize event

    The resize event will include the appropriate width and height after an orientationchange, but you do not want to listen for all resize events. Therefore, we add a one-off resize event listener after an orientation change:

    Javascript:

    window.addEventListener('orientationchange', function() {
        // After orientationchange, add a one-time resize event
        var afterOrientationChange = function() {
            // YOUR POST-ORIENTATION CODE HERE
            // Remove the resize event listener after it has executed
            window.removeEventListener('resize', afterOrientationChange);
        };
        window.addEventListener('resize', afterOrientationChange);
    });
    

    jQuery:

    $(window).on('orientationchange', function() {
        // After orientationchange, add a one-time resize event
        $(window).one('resize', function() {
            // YOUR POST-ORIENTATION CODE HERE
        });
    });
    

    Do NOT use timeouts

    Timeouts are unreliable - some devices will fail to capture their orientation change within your hard-coded timeouts; this can be for unforeseen reasons, or because the device is slow. Fast devices will inversely have an unnecessary delay in the code.

    0 讨论(0)
  • 2020-12-03 05:12

    It is important to note that orientationchange will not get the height after the change, but rather before. Use resize to accomplish this.

    $(window).bind('orientationchange', function (e) {
        var windowHeight = $(window).innerHeight();
        console.log('Before Orientation: Height = ' + windowHeight);
    
        $(window).resize(function () {
            windowHeight = $(window).innerHeight();
            console.log('After Orientation: Height = ' + windowHeight);
        });
    });
    
    0 讨论(0)
  • 2020-12-03 05:17

    I used the workaround proposed by Gajus Kuizunas for a while which was reliable albeit a bit slow. Thanks, anyway, it did the job!

    If you're using Cordova or Phonegap I found a faster solution - just in case someone else faces this problem in the future. This plugin returns the correct width/height values right away: https://github.com/pbakondy/cordova-plugin-screensize

    The returned height and width reflect the actual resolution though, so you might have to use window.devicePixelRatio to get viewport pixels. Also the title bar (battery, time etc.) is included in the returned height. I used this callback function initally (onDeviceReady)

    var successCallback = function(result){
        var ratio = window.devicePixelRatio;            
        settings.titleBar = result.height/ratio-window.innerHeight; 
        console.log("NEW TITLE BAR HEIGHT: " + settings.titleBar);
    };
    

    In your orientation change event handler you can then use:

    height = result.height/ratio - settings.titleBar;
    

    to get the innerHeight right away. Hope this helps someone!

    0 讨论(0)
  • 2020-12-03 05:19

    Orientation change needs a delay to pick up on the new heights and widths. This works 80% of the time.

    window.setTimeout(function() {
        //insert logic with height or width calulations here.
    }, 200);
    
    0 讨论(0)
提交回复
热议问题