vue获取屏幕宽高


1、在data中获取

data() {
    
    
	return {
    
    
		screenWidth: document.body.clientWidth, // 屏幕宽度
		screenHeight: document.body.clientHeight, // 屏幕高度
	}
}

2、在mounted函数中获取

mounted() {
    
    
	window.screenWidth = document.body.clientWidth;
    window.screenHeight = document.body.clientHeight;
    this.screenWidth = window.screenWidth;
    this.screenHeight = window.screenHeight;
    
    console.log(window.screenWidth);
    console.log(window.screenHeight);
    console.log(this.screenWidth);
    console.log(this.screenHeight);
}

猜你喜欢

转载自blog.csdn.net/weixin_51157081/article/details/121282216