判断横屏 页面自适应 判断版本号
<script>
import { browser} from '@/utils/nativeJsBridge'
export default {
inject: ['reload'], // 使用 inject 注入 reload 变量
data() {
return {
screenHW: window.orientation, // 判断横竖屏
isScreen: true, // 横竖屏
}
},
computed: {
watch: {
screenHW: {
immediate: true,
handler(val, old) {
console.log(val)
console.log(old)
this.rotate()
},
},
},
created() {
if (browser.versions.ios) {
if (
this.compareVersion('2.8.8', localStorage.getItem('version')) == -1 ||
this.compareVersion('2.8.8', localStorage.getItem('version')) == 0
) {
console.log('大于')
} else {
// this.popShowBrowser = true
}
} else {
if (
this.compareVersion('2.8.7', localStorage.getItem('version')) == -1 ||
this.compareVersion('2.8.7', localStorage.getItem('version')) == 0
) {
console.log('大于')
} else {
this.popShowBrowser = true
}
}
},
mounted() {
this.screenHW = window.orientation //保证刷新时获取状态准确
// 监听窗口大小
window.onresize = () => {
return (() => {
this.screenHW = window.orientation // 把横竖屏的变化赋值
})()
}
window.addEventListener('load', this.rotate, false)
this.$store.commit('show', false)
},
methods: {
// 判断横竖屏
rotate() {
if (this.screenHW == 180 || this.screenHW == 0) {
// console.log('竖屏')
this.getOrientation()
this.isScreen = true
} else if (this.screenHW == 90 || this.screenHW == -90) {
// console.log('横屏')
const width = document.documentElement.offsetWidth
document.documentElement.style.fontSize = width / 20 + 'px'
this.isScreen = false
}
},
getOrientation() {
//与app交互的横屏方法
},
// 版本号判断
compareVersion(v1, v2) {
if (v1 == v2) {
return 0
}
const vs1 = v1.split('.').map((a) => parseInt(a))
const vs2 = v2.split('.').map((a) => parseInt(a))
const length = Math.min(vs1.length, vs2.length)
for (let i = 0; i < length; i++) {
if (vs1[i] > vs2[i]) {
return 1
} else if (vs1[i] < vs2[i]) {
return -1
}
}
if (length == vs1.length) {
return -1
} else {
return 1
}
},
},
}
</script>