取向变化事件已被废弃。
window.addEventListener("orientationchange", function(event) {
console.log(event.target.screen.orientation.angle);
});窗口:定向更改事件
不再推荐这个特性不再被推荐。虽然有些浏览器可能仍然支持它,但它可能已经从相关的web标准中删除,可能正在被删除,或者可能仅为兼容性目的而保留。避免使用它,并在可能的情况下更新现有代码;请参阅本页底部的兼容性表,以指导您的决策。请注意,此功能可能在任何时候停止工作。
我现在能用什么?有什么别的办法吗?
发布于 2021-06-13 19:14:59
我们可以使用实验特性ScreenOrientation
screen.orientation.addEventListener('change', function(e) { ... })
screen.orientation.onchange = function(e) { ... }您可以检查屏幕定向表的可用值以确定方向类型:
下面是一个例子:
screen.orientation.addEventListener('change', function(e) {
if (e.currentTarget.type === 'landscape-primary') {
// landscape mode => angle 0
} else if (e.currentTarget.type === 'portrait-primary') {
// portrait mode => angle 0
}
})检查浏览器兼容性表。
发布于 2022-02-07 16:18:47
window.orientation
似乎还能用。我想你可以用
window.addEventListener('resize', function(e) {
if (Math.abs(window.orientation) == 90) {
// landscape mode => angle 90 or -90
} else if (window.orientation == 0) {
// portrait mode => angle 0
}
})如果你的目标是严格机动的
https://stackoverflow.com/questions/67961837
复制相似问题