h5监听方向变化?

screen.orientation.addEventListener('change', function() {
  console.log('new orientation is ', screen.orientation.type);
});
window.addEventListener("orientationchange", function() {
  if (window.orientation == 90 || window.orientation == -90) {
    // landscape mode
  } else {
    // portrait mode
  }
});

上面两张监听方向改变的方法,有什么区别呢?推荐哪个?

阅读 1.8k
1 个回答

creen.orientation.addEventListener('change', function() {...})是根据 Screen Orientation API,
window.addEventListener("orientationchange", function() {...}):是根据window 对象的 "orientationchange" 事件和 window.orientation 属性,兼容性更好,但是角度少

推荐问题
logo
Microsoft
子站问答
访问
宣传栏