Preface
When I was using orientationchange event, I met a few bugs. So, I take it down.
Main compatibility problemWhen I was testing my code on my android, it was ok. However, it doesn"t work on my boss"s iPhone6. So, i have to change the code.
The origin code was like:
html, body { width: 100%; height: 100%; } .box { width: 100%; height: 100%; background: pink; } .box.landscape { background: lightblue; }let box = document.getElementById("box") window.addEventListener("orientationchange", orientationChangeCb) function orientationChangeCb(event) { let isLand = isLandscape() if (isLand) { box.classList.add("landscape") } else { box.classList.remove("landscape") } } function isLandscape() { if ("orientation" in window) { return Math.abs(window.orientation) === 90 } else { return window.innerWidth > window.innerHeight } }To be compatible with iPhone6, I use resize event instead.
However, the better way I think is :
let eventForOrientationChange = "onorientationchange" in window ? "orientationchange" : "resize" if (isMobile()) { window.addEventListener(eventForOrientationChange, orientationChangeCb) }isMobile ?Because onorientationchange is a mobile event, so if you try to run code below on your computer with chrome, you will get:
window.onorientationchange //undefined "onorientationchange" in window //falseIt seems a little weird but it"s true until chrome 69. That"s why I add isMobile() before I use window.addEventListener. In that case, we don"t have to listen for the resize event on desktop.
window.orientation or screen.orientationAccording to mdn, window.orientation has been Deprecated. However, similar API screen.orientation has a big problem for compatibility. Safari and QQ doesn"t support. As of 2018.10.4, global support in caniuse is only 72.5%.
css onlyIf you just want to update style, you don"t have to use code above. CSS media queries
support code like:@media (min-height: 680px), screen and (orientation: portrait) { /* ...; */ } @media (min-height: 680px), screen and (orientation: landscape) { /* ...; */ }Original Post
Referencedetect viewport orientation, if orientation is portrait display alert message advising user of instructions
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/99377.html
摘要:可以通过来自动隐藏浏览器地址栏。可是宽度,初始化缩放比例,允许用户缩放的最大比例,允许用户缩放的最小比例,是否允许用户缩放。允许用户添加到主屏幕,并提供的支持。当用时候,不能使用伪类,否则滑动会卡。 阻止弹性滚动 functionBlockMove(event){ //Tell Safari not to move the window. event.preventDefault();...
摘要:可以通过来自动隐藏浏览器地址栏。可是宽度,初始化缩放比例,允许用户缩放的最大比例,允许用户缩放的最小比例,是否允许用户缩放。允许用户添加到主屏幕,并提供的支持。当用时候,不能使用伪类,否则滑动会卡。 阻止弹性滚动 functionBlockMove(event){ //Tell Safari not to move the window. event.preventDefault();...
阅读 1306·2021-09-02 10:19
阅读 1076·2019-08-26 13:25
阅读 2086·2019-08-26 11:37
阅读 2395·2019-08-26 10:18
阅读 2654·2019-08-23 16:43
阅读 2948·2019-08-23 16:25
阅读 754·2019-08-23 15:53
阅读 3282·2019-08-23 15:11