// 检测横竖屏
var orient = ''
function checkOrient() {
if (window.orientation == 0 ){
orient = 'portrait 0 right';
} else if (window.orientation == 180){
orient = 'portrait 180 left';
} else if (window.orientation == 90 || window.orientation == -90){
orient = 'landscape';
}
}
// 添加事件监听
window.addEventListener('load', function () {
checkOrient()
alert(orient)
})
window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize", function() {
checkOrient()
alert(orient)
})
js 检测手机横竖屏
最新推荐文章于 2023-05-25 10:24:30 发布
这段代码用于检测浏览器窗口的横竖屏状态。通过监听窗口的'load'和'orientationchange'事件,实时更新横竖屏信息。当window.orientation为0或180时,表示处于竖屏;为90或-90时,表示处于横屏。
2485

被折叠的 条评论
为什么被折叠?



