在vue中獲取屏幕寬度的方法有:1.使用document.documentElement方法獲取;2.在watch中實時監聽;3.使用mounted函數獲取;
具體方法如下:
1.使用document.documentElement方法獲取屏幕的寬高
windowWidth: document.documentElement.clientWidth, //獲取屏幕寬度
windowHeight: document.documentElement.clientHeight, //獲取屏幕高度
2.在watch中實時監聽屏幕寬高
watch: {
windowHeight (val) {
let that = this;
console.log("實時屏幕高度:",val, that.windowHeight );
},
windowWidth (val) {
let that = this;
console.log("實時屏幕寬度:",val, that.windowHeight );
}
},
3.使用mounted函數獲取屏幕寬高
mounted() {
var that = this;
window.onresize = () => {
return (() => {
window.fullHeight = document.documentElement.clientHeight;
window.fullWidth = document.documentElement.clientWidth;
that.windowHeight = window.fullHeight; //獲取屏幕高度
that.windowWidth = window.fullWidth; //獲取屏幕寬度
})()
};
},