javascript max viewport height after orientation change Android & iOS

前端 未结 3 1789
遇见更好的自我
遇见更好的自我 2021-01-30 18:43

The Goal:

To find the max viewport height of a device including the space of the address bar so that we can dynamically resize the min-body and push our c

3条回答
  •  天命终不由人
    2021-01-30 19:20

    Expected values returned in iOS simulator. I can't test for Android at the moment.

    var supportsOrientationChange = "onorientationchange" in window;
    var orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";
    
    window.onload = updateOrientation();
    
    window.addEventListener(orientationEvent, function() {
        updateOrientation();
    }, false);
    
    function updateOrientation(){
        switch(window.orientation){
        case 0:
        alert(window.outerHeight); // Returns '356' with browser chrome
        break;
    
        case -90:
        alert('Landscape right');
        break;
    
        case 90:
        alert(window.outerHeight); // Returns '208' w browser chrome
        break;
    
        case 180:
        //alert('Portrait view - upside down');
        break;
        }
        var orientation = (window.orientation);
    }
    

    (Note: This code will not test in a browser.)

提交回复
热议问题