Preface
When I was using orientationchange
event, I met a few bugs. So, I take it down.
Main
compatibility problem
When 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:
<div class="box" id="box"> |
html, |
let box = document.getElementById('box') |
To be compatible with iPhone6, I use resize
event instead.
However, the better way I think is :
let eventForOrientationChange = |
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 |
It 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.orientation
According 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 only
If 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) { |