移动设备的屏幕有两个方向:lanscape 和 portait。
获取当前屏幕方向
方法一:matchMeadia("(orientation: portait)")
const screenDirction = window.matchMedia("(orientation: portrait)");
打印内容
可以通过访问对象的matches属性来查看查询结果:
if (screenDirection.matches) {
console.log('portait');
} else {
console.log('landscape');
}
监听屏幕方向变化,可以通过MediaQueryList对象的addListener方法来订阅事件:
const screenDirection = window.matchMedia("(orientation: portait)");
function listenerOrientationChange(direction) {
if (direction.matches) {
// 坚屏
} else {
// 横屏
}