Javascript doOnOrientationChange :can't fix a bug loading the page

前端 未结 1 1413
暖寄归人
暖寄归人 2021-01-25 10:48

I used a .js to avoid landscape view from mobile device. I edited a white full-screen image saying \"this site is not thought to be viewed in landscape mode, please

相关标签:
1条回答
  • 2021-01-25 11:10

    You need to move the doOnOrientationChange() function outside of the other one, and then call it on pageload. Like this it should work:

    <script>
    function checkMobile() {
        var isMobile = false;
        if (navigator.userAgent.match(/Android/i)
         || navigator.userAgent.match(/BlackBerry/i)
         || navigator.userAgent.match(/iPhone|iPad|iPod/i)
         || navigator.userAgent.match(/Opera Mini/i)
         || navigator.userAgent.match(/IEMobile/i)) {
                isMobile = true;
            }
        return isMobile;
    }
    function doOnOrientationChange() {
        var a = document.getElementById('alert');
        var b = document.body;
        var w = b.offsetWidth;
        var h = b.offsetHeight;
        if (checkMobile()) {
            (w / h > 1) ? (a.className = 'show', b.className = 'full-body') : (a.className = 'hide', b.className = '');
        } else {
            a.className = 'hide';
            b.className = '';
        }
    }
    window.onload = doOnOrientationChange();
    window.addEventListener('resize', doOnOrientationChange, 'false');
    </script>
    
    0 讨论(0)
提交回复
热议问题