先上效果图
1.模板(App.vue中)
<template>
<div id="app">
<div
class="app-hint"
v-if="isScreen && $store.state.app.device != 'desktop'"
>
<div class="rotate">
<img src="./assets/heng.png" alt="" />
<h2>请旋转手机,获得更加体验!</h2>
</div>
<div class="mask"></div>
</div>
<router-view v-else />
</div>
</template>
2.js
<script>
export default {
data() {
return {
screenHW: window.orientation, // 判断横竖屏
isScreen: true, // 横竖屏
};
},
mounted() {
this.screenHW = window.orientation //保证刷新时获取状态准确
// 监听窗口大小
window.onresize = () => {
return (() => {
this.screenHW = window.orientation // 把横竖屏的变化赋值
})()
}
},
watch: {
screenHW: {
immediate: true,
handler(newval, old) {
this.rotate()
}
}
},
methods: {
// 判断横竖屏
rotate() {
if (this.screenHW == 180 || this.screenHW == 0) {
// console.log('竖屏')
this.isScreen = true
} else if (this.screenHW == 90 || this.screenHW == -90) {
// console.log('横屏')
this.isScreen = false
}
},
}
</script>