成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Better Way to Use orientationchange Event on Mobil

Julylovin / 517人閱讀

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:

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

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) {
  /* ...; */
}
@media (min-height: 680px), screen and (orientation: landscape) {
  /* ...; */
}

Original Post

Reference

detect viewport orientation, if orientation is portrait display alert message advising user of instructions

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/99377.html

相關(guān)文章

  • 移動(dòng)web開發(fā)小貼示

    摘要:可以通過來自動(dòng)隱藏瀏覽器地址欄??墒菍挾龋跏蓟s放比例,允許用戶縮放的最大比例,允許用戶縮放的最小比例,是否允許用戶縮放。允許用戶添加到主屏幕,并提供的支持。當(dāng)用時(shí)候,不能使用偽類,否則滑動(dòng)會(huì)卡。 阻止彈性滾動(dòng) functionBlockMove(event){ //Tell Safari not to move the window. event.preventDefault();...

    snowLu 評論0 收藏0
  • 移動(dòng)web開發(fā)小貼示

    摘要:可以通過來自動(dòng)隱藏瀏覽器地址欄??墒菍挾?,初始化縮放比例,允許用戶縮放的最大比例,允許用戶縮放的最小比例,是否允許用戶縮放。允許用戶添加到主屏幕,并提供的支持。當(dāng)用時(shí)候,不能使用偽類,否則滑動(dòng)會(huì)卡。 阻止彈性滾動(dòng) functionBlockMove(event){ //Tell Safari not to move the window. event.preventDefault();...

    CoderDock 評論0 收藏0

發(fā)表評論

0條評論

最新活動(dòng)
閱讀需要支付1元查看
<