vue3頁面加載完成后如何獲取寬度、高度
vue3頁面加載完成后獲取寬度、高度
剛好H5項目有用到這個需求,頁面加載完成后獲取當(dāng)前頁面高度,記錄一下。^_^
<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,//頁面高度 }); onMounted(() => { nextTick(()=>{ state.hHeight = document.documentElement.clientHeight; console.log(document.documentElement.clientHeight) }) }) return { ...toRefs(state) } }, }); </script>
用vue3.2版本的也可以用語法糖來處理,直接上代碼:
<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>
親測可用,大家可以試試!
vue3之vue3.2獲取dom元素的寬高
知識點:ref,nextTike
- ref可以用于dom對象的獲取,以及創(chuàng)建一個響應(yīng)式的普通對象類型
- nextTick是一個函數(shù),它接受一個函數(shù)作為參數(shù),nextTick官網(wǎng)定義是‘將回調(diào)推遲到下一個 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[]>(['蜘蛛俠', '鋼鐵俠', '美國隊長']) 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的值時,vue并不是立刻去更新dom,而是在一個事件循環(huán)最后再去更新dom,這樣可以避免不必要的計算和dom操作,對提高性能非常重要。
那么我們需要在dom更新完成后,再去獲取ul的高度,這時候就需要用到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[]>(['蜘蛛俠', '鋼鐵俠', '美國隊長']) 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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue實現(xiàn)電梯樣式錨點導(dǎo)航效果流程詳解
這篇文章主要介紹了Vue實現(xiàn)電梯樣式錨點導(dǎo)航效果流程,這種導(dǎo)航效果廣泛應(yīng)用于商城點餐購物情景,文中通過示例代碼介紹的很詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-05-05Vue的watch和computed方法的使用及區(qū)別介紹
Vue的watch屬性可以用來監(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),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09Vue MVVM模型與data及methods屬性超詳細(xì)講解
MVVM旨在利用WPF中的數(shù)據(jù)綁定函數(shù),通過從視圖層中幾乎刪除所有GUI代碼(代碼隱藏),更好地促進(jìn)視圖層開發(fā)與模式其余部分的分離,這篇文章主要介紹了Vue MVVM模型與data及methods屬性2022-10-10