gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:21:15 24 4
gpt4 key购买 nike

我使用了 .js 来避免移动设备的横向 View 。我编辑了一个白色的全屏图像,上面写着“这个网站不适合在横向模式下查看,请转动你的设备”,每次我将我的设备从纵向旋转到横向时都会显示。它可以工作,除非我加载页面并且我已经处于横向模式。关于如何解决它的任何想法?谢谢

<script>
(function() {
'use strict';

var isMobile = {
Android: function() {
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function() {
return navigator.userAgent.match(/IEMobile/i);
},
any: function() {
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}
};
if (isMobile.any()) {
doOnOrientationChange();
window.addEventListener('resize', doOnOrientationChange, 'false');
}

function doOnOrientationChange() {
var a = document.getElementById('alert');
var b = document.body;
var w = b.offsetWidth;
var h = b.offsetHeight;
(w / h > 1) ? (a.className = 'show', b.className = 'full-body') : (a.className = 'hide', b.className = '');
}
})();
</script>

更新:

我尝试将 window.orientation 添加到脚本中,但出了点问题

if (orientation === "landscape-primary") {
doOnOrientationChange();
window.addEventListener('resize',doOnOrientationChange,'false');
}

window.onload(doOnOrientationChange());

最佳答案

您需要将 doOnOrientationChange() 函数移到另一个函数之外,然后在页面加载时调用它。像这样它应该可以工作:

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

关于JavaScript doOnOrientationChange :can't fix a bug loading the page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30562945/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com