Vue获取屏幕的宽度和高度
data() {return {screenWidth: document.body.clientWidth, // 屏幕宽度screenHeight: document.body.clientHeight, // 屏幕高度}},mounted() {...
·
1.在数据中定义宽度和高度
data() {
return {
screenWidth: document.body.clientWidth, // 屏幕宽度
screenHeight: document.body.clientHeight, // 屏幕高度
}
},
2、在mounted函数中获取数据
mounted() {
const that = this;
window.onresize = () => {
return (() => {
window.screenWidth = document.body.clientWidth;
window.screenHeight = document.body.clientHeight;
that.screenWidth = window.screenWidth;
that.screenHeight = window.screenHeight;
})()
};
console.log(this.screenWidth);
console.log(this.screenHeight + '----');
},
更多推荐
已为社区贡献4条内容
所有评论(0)