【问题标题】:orientationchange event doesn't fire on IOS devicesIOS 设备上不会触发orientationchange 事件
【发布时间】:2016-12-24 23:06:19
【问题描述】:

Orientationchange 事件不会在 iOS 设备上触发。在台式机和 Android 设备上都可以完美运行,但操作系统设备不支持此事件。如何确定 iOS 设备上的屏幕方向变化?

【问题讨论】:

  • $(window).on('resize') 也不会在 IOS safari 上触发...我不知道为什么...
  • 这个老问题,但你可以问为什么 window.orientation 甚至typeof window.orientation in alert() 没有显示任何信息?因为对于 Windows 用户来说,警报只是某种调试的方法......

标签: jquery ios dom-events screen-orientation


【解决方案1】:

这真的很老了,但它最近发生在我身上。

我让它工作了:

window.addEventListener('resize', functionName)

function functionName() {

  if(window.innerWidth > window.innerHeight) {
  
   //then do stuff
  }
  
  if(window.innerWidth < window.innerHeight) {

  //then do other stuff
  }
}

【讨论】:

    【解决方案2】:

    我在这篇文章中得到了答案 https://stackoverflow.com/a/9824480/1606668

    但是如果你仍然没有得到方向值,那么试试这个:

       function onOriChange() {
            if(!window.orientation){
                if(window.innerWidth > window.innerHeight){
                    alert('landscape');
                }else{
                    alert('portrait');
                }
            }
        }
    
        window.addEventListener('orientationchange', onOriChange);
    
        // Initial execution if needed
        onOriChange();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-19
      • 1970-01-01
      • 2014-02-07
      • 2019-08-18
      • 2023-02-05
      • 1970-01-01
      • 2019-04-17
      相关资源
      最近更新 更多