uni-app combinada con Vue (obtener dinámicamente el ancho y la altura del contenedor div)

código HTML:

<template>
    <view class="home_page">
		<view class="body" ref="body">
			<view class="ss" ref="ss" @click="goMy">A页面</view>
			<view class="eee" @click="goLoginOut">B页面</view>
		</view>
		<view class="test" :style="styleVar"></view>
    </view>
</template>
<script>
export default {
    
    
    data() {
    
    
        return {
    
    
			styleVar: {
    
    }
        }
    },
    methods: {
    
    
		getStyle() {
    
    
		//使用 uni.createSelectorQuery() 需要在生命周期 mounted 后进行调用。
	      let infos = uni.createSelectorQuery().select(".body")
	     	console.log('info', info)  // 获取元素
	     infos.boundingClientRect((data) => {
    
     //data - 各种参数
				console.log('data--------->', data)
	     		console.log('data.width', data.width) // 获取元素宽度
	     		console.log('data.height', data.height) // 获取元素宽度
				this.styleVar = {
    
    
					height: data.height + 'px'
				}
	      }).exec()
		}
    },
	mounted() {
    
    
		this.$nextTick(function(){
    
    
			// 如果不在mounted里面调用,必须得放到this.$nextTick()里面去执行,不然会报错
			this.getStyle()
		})
	}
}
</script>
<style >
	.ss{
    
    
		width: 100%;
		height: 400px;
		text-align: center;
		background-color: red;
		line-height: 400px;
	}
	/* .eee{
		width: 100%;
		height: 400px;
		text-align: center;
		background-color: forestgreen;
		line-height: 400px;
	} */
</style>

Los siguientes son los datos que imprime la consola:

inserte la descripción de la imagen aquí

Supongo que te gusta

Origin blog.csdn.net/wen15191038073/article/details/128233664
Recomendado
Clasificación