1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > js监测手机屏幕旋转(横屏还是竖屏)

js监测手机屏幕旋转(横屏还是竖屏)

时间:2020-08-24 19:20:43

相关推荐

js监测手机屏幕旋转(横屏还是竖屏)

主要js代码如下:

示例一:

window.addEventListener("orientationchange", function() {if(window.orientation === 90){console.log('这是竖屏')}if(window.orientation === 0){console.log('这是横屏')}}, false);

示例二:

// 判断屏幕是否旋转function orientationChange() {switch(window.orientation) {case 0:alert("肖像模式 0,screen-width: " + screen.width + "; screen-height:" + screen.height);break;case -90:alert("左旋 -90,screen-width: " + screen.width + "; screen-height:" + screen.height);break;case 90:alert("右旋 90,screen-width: " + screen.width + "; screen-height:" + screen.height);break;case 180:alert("风景模式 180,screen-width: " + screen.width + "; screen-height:" + screen.height);break;};};// 添加事件监听window.addEventListener('load', function(){orientationChange();window.onorientationchange = orientationChange;});

拓展知识:

1.orientationchange事件在设备的纵横方向改变时触发

2.orientation属性有三个值:

0:竖屏模式(portrait),

-90:该设备横向旋转到右侧的横屏模式(landscape),

90:该设备横向旋转到左边的横屏模式(landscape)。

兼容情况:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。