使用 javascript 检测方向变化
是否可以使用 JavaScript 检测 iPad 或 Galaxy Tab 上浏览器方向的变化?我认为使用 css 媒体查询是可能的。
Is it possible to detect change in orientation of the browser on the iPad or Galaxy Tab using javascript? I think it's possible using css media queries.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
注意: orientationChange 是已弃用
而是使用 screen.orientation 使用 screenOrientation 接口
由 screenorientation.onchange 事件
但请注意截至 2022 年 7 月的支持
Safari 根本不支持 screen.orientation
MDN:
或 jQuery 移动orientationchange
旧的orientationChange和window.orientation应该仍然适用于Safari
旧的答案
http://www.nczonline.net/blog/2010/04/06/ipad-web-development-tips/
iPad 上的 Safari 确实支持 window.orientation 属性,因此,如果有必要,您可以使用它来确定用户是处于水平模式还是垂直模式。作为此功能的提醒:
当设备旋转时,还有在窗口对象上触发的orientationchange事件。
您还可以使用 CSS 媒体查询来确定 iPad 是处于垂直还是水平方向,例如:
http://www.htmlgoodies.com/beyond/ webmaster/toolbox/article.php/3889591/Detect-and-Set-the-iPhone--iPads-Viewport-Orientation-Using-JavaScript-CSS-and-Meta-Tags.htm
NOTE: orientationChange is deprecated
Instead use screen.orientation using the screenOrientation interface
which is triggered by the screenorientation.onchange event
However note the support as of July 2022
The screen.orientation is not supported by Safari at all
MDN:
or jQuery mobile orientationchange
The older orientationChange and window.orientation should still work for Safari
Older answer
http://www.nczonline.net/blog/2010/04/06/ipad-web-development-tips/
Safari on the iPad does support the window.orientation property, so if necessary, you can use that to determine if the user is in horizontal or vertical mode. As reminder of this functionality:
There is also the orientationchange event that fires on the window object when the device is rotated.
You can also use CSS media queries to determine if the iPad is being held in vertical or horizontal orientation, such as:
http://www.htmlgoodies.com/beyond/webmaster/toolbox/article.php/3889591/Detect-and-Set-the-iPhone--iPads-Viewport-Orientation-Using-JavaScript-CSS-and-Meta-Tags.htm
在 2022 年,不添加窗口orientationchange监听器(不推荐监听器,因为已弃用),您应该侦听
screen.orientation
更改事件:除 IE 和 Safari 之外的所有浏览器现在都支持它。 (这是 IE11 的
screen
屏幕截图:...请注意,
orientation
不是screen
在 IE11 中)屏幕方向 API 已完整记录。主要焦点是
ScreenOrientation
接口,它扩展了Screen
。以下是Screen
的orientation
属性的 2 个屏幕截图,其中显示了angle
在屏幕上如何从 0(纵向)到 90(横向)变化。 Android 设备:In 2022, instead of adding a window orientationchange listener (listener not recommended due to deprecation) you should listen for a
screen.orientation
change event:All browsers except IE and Safari now support it. (here is a screenshot of
screen
from IE11:... notice that
orientation
is not a supported attribute ofscreen
in IE11)The Screen Orientation API is thoroughly documented. The main focus is the
ScreenOrientation
interface, which extendsScreen
. Here are 2 screenshots of theorientation
attribute ofScreen
, which shows how theangle
changes from 0 (portrait) to 90 (landscape) on an Android device:您可以使用 mediaMatch 来评估 CSS 媒体查询,例如
CSS 媒体查询在
orientationchange
之前触发。如果您希望捕获事件的结束(旋转完成时),请参阅 方向更改后的移动视口高度。You can use mediaMatch to evaluate CSS media queries, e.g.
CSS media query fires before the
orientationchange
. If you are looking to capture the end of the event (when the rotation has been completed), see mobile viewport height after orientation change.您可以像这样使用orientationchange事件:
You can use the orientationchange event like so:
由于某种原因,没有人提到当该设备在该线程中倒置时会发生什么。
window.orientation
在水平放置时返回 -90 或 90。垂直放置时返回 0 或 180。有些设备支持,有些不支持倒置。因此,推荐的逻辑类似于,另请注意,
window.orientation
在桌面上返回undefined
。For some reason, nobody mentioned what happens when the device is held upside-down in this thread.
window.orientation
returns -90 or 90 when held horizontal. It returns 0 or 180 when held vertical. Some devices do support and some do not support being held upside-down. Therefore, the recommended logic is like,Also note that
window.orientation
returnsundefined
on desktops.来自“跨设备、跨浏览器纵向横向检测”
这是关于确定移动设备是处于纵向模式还是横向模式;你不需要关心它的方向。众所周知,如果您将 iPad 倒置,它就会处于纵向模式。
90表示横向,0表示纵向,跨浏览器、跨设备。
JavaScript 版本是这样的,如果我错了,请纠正我。
From "Cross-device, cross-browser portrait-landscape detection"
This is about finding out whether a mobile device is in portrait or landscape mode; you don't need to care about its orientation. For all you know, if you hold your iPad upside down, it's in portrait mode.
90 means landscape, 0 means portrait, cross browser, cross device.
The JavaScript version would be this, correct me please if I am wrong.
orientationChange 已被弃用,并且在某些浏览器中也不支持,
insideHeight 和outerHeight 有时在ios 中给出不一致的结果
所以我们可以使用 document.documentElement 来检查方向以及调整大小事件
orientationChange is deprecated and also not supported in some browsers,
innerHeight and outerHeight sometimes give inconsistent results in ios
so we can use document.documentElement to check orientation along with resize event
window.orientation 就是您要寻找的。还有一个 onOrientationChange 事件
适用于 Android、iPhone,我确信也适用于 iPad
window.orientation is what you're looking for. there's also an onOrientationChange event
works for android, iphone and, i'm mostly sure, for ipad
添加到@mplungjan答案中,我发现使用webkit“本机”(我真的不知道如何称呼它)事件“deviceorientation”有更好的结果。
在 Mozilla 开发者网络中,他们对如何在 webkit 和 Gecko 之间进行标准化有很好的解释帮助我解决了这个问题。
Adding to the @mplungjan answer, I found better results using the webkit "native" (I don't really how to called it) event, 'deviceorientation'.
In the Mozilla Developer network they have a good explanation about how to normalize between webkit and Gecko that helped me to solve this problem.
一个易于使用的片段:
An easy to use snippet :
创建一个服务来使用 observable 检测方向的变化。
现在在您的组件中使用此服务来订阅正在检测到的更改
}
}
Create a service to detect the change in orientation using observable.
Now Use this service inside your component to subscribe to the changes being detected
}
}
从 2022 年开始,
一旦您像这样做好准备,
您可以
但请注意,
直接从
90
切换到270
时,RESIZE
不会触发(不触发中间是肖像视图)因此我们不能依赖
window.addEventListener("resize",screenOrientationHasChanged);
并且存在完全相同的问题
window.screen.orientation.addEventListener('change',screenOrientationHasChanged);
还带有
window.addEventListener("orientationchange",screenOrientationHasChanged);
这不幸地意味着,截至 2022 年,即使使用
setInterval
也没有可靠的方法来检测屏幕方向变化因为当您从
90
转到270
时,screen.orientation.angle
和screen.orientation.type
都不会更新之间不会触发纵向视图。因此,以下内容并不比移动设备上的
resize
更好。您可以尝试锁定屏幕方向以避免错误,但这在 2022 年起不适用于 iOS,并且仅适用于 Android 上的全屏模式。
As of 2022
Once you get ready like this,
you can
but note that
RESIZE
does not fire when switching from90
to270
directly (without triggering a portrait view in between)THEREFORE WE CANNOT RELY ON
window.addEventListener("resize",screenOrientationHasChanged);
AND THERE IS THE EXACT SAME PROBLEM WITH
window.screen.orientation.addEventListener('change',screenOrientationHasChanged);
ALSO WITH
window.addEventListener("orientationchange",screenOrientationHasChanged);
THIS SADLY MEANS THAT AS OF 2022 THERE IS NO RELIABLE WAY TO DETECT SCREEN ORIENTATION CHANGE even by using
setInterval
BECAUSE neither
screen.orientation.angle
norscreen.orientation.type
is updated when you go from90
to270
without triggering a portrait view in between.So the following is not any better than
resize
on mobile devicesYou may try to lock the screen orientation to avoid errors but that does not work on iOS as of 2022 and it only works with fullscreen mode on Android.