vue3頁(yè)面加載完成后如何獲取寬度、高度
vue3頁(yè)面加載完成后獲取寬度、高度
剛好H5項(xiàng)目有用到這個(gè)需求,頁(yè)面加載完成后獲取當(dāng)前頁(yè)面高度,記錄一下。^_^
<template>
<div class="wrap" :style="{height:hHeight+'px'}">
</div>
</template>
<script lang='ts'>
import { defineComponent, reactive, nextTick, onMounted, toRefs } from "vue";
export default defineComponent({
name: "Aboutus",
setup() {
let state = reactive({
hHeight: 0,//頁(yè)面高度
});
onMounted(() => {
nextTick(()=>{
state.hHeight = document.documentElement.clientHeight;
console.log(document.documentElement.clientHeight)
})
})
return {
...toRefs(state)
}
},
});
</script>用vue3.2版本的也可以用語(yǔ)法糖來(lái)處理,直接上代碼:
<template>
<div class="wrap" :style="{height:state.hHeight+'px'}">
</div>
</template>
<script setup>
import { reactive, nextTick } from "vue"
const state = reactive({
hHeight: 0
})
nextTick(()=>{
state.hHeight = document.documentElement.clientHeight;
console.log(document.documentElement.clientHeight)
})
</script>親測(cè)可用,大家可以試試!

vue3之vue3.2獲取dom元素的寬高
知識(shí)點(diǎn):ref,nextTike
- ref可以用于dom對(duì)象的獲取,以及創(chuàng)建一個(gè)響應(yīng)式的普通對(duì)象類型
- nextTick是一個(gè)函數(shù),它接受一個(gè)函數(shù)作為參數(shù),nextTick官網(wǎng)定義是‘將回調(diào)推遲到下一個(gè) DOM 更新周期之后執(zhí)行’,
未使用nextTike
<!--
* new page
* @author: Blaine
* @since: 2022-06-30
* page_nextTike.vue
-->
<template>
<div class="container" >
<ul ref="myRef">
<li v-for="(item, index) in pepleList" :key="index">{{ item }}</li>
</ul>
<button @click="addHandle">增加</button>
</div>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref, nextTick } from 'vue'
let pepleList = reactive<string[]>(['蜘蛛俠', '鋼鐵俠', '美國(guó)隊(duì)長(zhǎng)'])
const myRef = ref<HTMLElement>();
onMounted(() => {
console.log('列表的高度是:', myRef.value?.clientHeight)
})
const addHandle = async() => {
pepleList.push('閃電俠')
// await nextTick()
console.log('列表的高度是:', myRef.value?.clientHeight)
}
</script>
<style scoped>
</style>

**注意:**這里的list并沒有立即增加
問題在于我們改變list的值時(shí),vue并不是立刻去更新dom,而是在一個(gè)事件循環(huán)最后再去更新dom,這樣可以避免不必要的計(jì)算和dom操作,對(duì)提高性能非常重要。
那么我們需要在dom更新完成后,再去獲取ul的高度,這時(shí)候就需要用到nextTick了,
使用ref+nextTick
<!--
* new page
* @author: Blaine
* @since: 2022-06-30
* page_nextTike.vue
-->
<template>
<div class="container" >
<ul ref="myRef">
<li v-for="(item, index) in pepleList" :key="index">{{ item }}</li>
</ul>
<button @click="addHandle">增加</button>
</div>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref, nextTick } from 'vue'
let pepleList = reactive<string[]>(['蜘蛛俠', '鋼鐵俠', '美國(guó)隊(duì)長(zhǎng)'])
const myRef = ref<HTMLElement>();
onMounted(() => {
console.log('列表的高度是:', myRef.value?.clientHeight)
})
const addHandle = async() => {
pepleList.push('閃電俠')
await nextTick()
console.log('列表的高度是:', myRef.value?.clientHeight)
}
</script>
<style scoped>
</style>

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue實(shí)現(xiàn)電梯樣式錨點(diǎn)導(dǎo)航效果流程詳解
這篇文章主要介紹了Vue實(shí)現(xiàn)電梯樣式錨點(diǎn)導(dǎo)航效果流程,這種導(dǎo)航效果廣泛應(yīng)用于商城點(diǎn)餐購(gòu)物情景,文中通過示例代碼介紹的很詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-05-05
Vue+Element UI實(shí)現(xiàn)下拉菜單的封裝
這篇文章主要為大家詳細(xì)介紹了Vue+Element UI實(shí)現(xiàn)下拉菜單的封裝代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
vue移動(dòng)端下拉刷新和上拉加載的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue移動(dòng)端下拉刷新和上拉加載的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-09-09
Vue的watch和computed方法的使用及區(qū)別介紹
Vue的watch屬性可以用來(lái)監(jiān)聽data屬性中數(shù)據(jù)的變化。這篇文章主要介紹了Vue的watch和computed方法的使用及區(qū)別,需要的朋友可以參考下2018-09-09
詳解關(guān)于Vue版本不匹配問題(Vue packages version mismatch)
這篇文章主要介紹了詳解關(guān)于Vue版本不匹配問題(Vue packages version mismatch),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-09-09
Vue3之元素和組件的動(dòng)畫切換實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了Vue3之元素和組件的動(dòng)畫切換實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Vue MVVM模型與data及methods屬性超詳細(xì)講解
MVVM旨在利用WPF中的數(shù)據(jù)綁定函數(shù),通過從視圖層中幾乎刪除所有GUI代碼(代碼隱藏),更好地促進(jìn)視圖層開發(fā)與模式其余部分的分離,這篇文章主要介紹了Vue MVVM模型與data及methods屬性2022-10-10

