javascript max viewport height after orientation change Android & iOS

前端 未结 3 1732
遇见更好的自我
遇见更好的自我 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

    You could try a self-invoking closure that monitors the change in orientation by itself. Something like this:

    (function () {
    
        var CurrentHeight;
    
        (function CheckForOrientationChange() {
    
            //var NewHeight = window.screen.availHeight / window.devicePixelRatio;
            //var NewHeight = $(window).height();    
    
            var NewHeight = $('#WidthCheck').width();
    
            if (CurrentHeight && CurrentHeight!== NewHeight ) {
    
                alert(NewHeight); // change here
            }
    
            CurrentHeight = NewHeight;
    
            setTimeout(CheckForOrientationChange, 1000);
    
        })();
    
    })();
    

    Just drop this into the document ready function. For now it checks every second but you can shorten that interval. The jsfiddle is here and you can test it by changing the size of the browser to simulate a mobile browser's change and then you can adapt the code to handle your action.

提交回复
热议问题