溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

vue3頁面加載完成后怎么獲取寬度、高度

發(fā)布時間:2023-04-25 15:17:47 來源:億速云 閱讀:203 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了vue3頁面加載完成后怎么獲取寬度、高度的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇vue3頁面加載完成后怎么獲取寬度、高度文章都會有所收獲,下面我們一起來看看吧。

    vue3頁面加載完成后獲取寬度、高度

    剛好H5項目有用到這個需求,頁面加載完成后獲取當(dāng)前頁面高度。

    <template>
    <div class="wrap" :>
    </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" :>
       </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之vue3.2獲取dom元素的寬高

    知識點(diǎn):ref,nextTike

    • ref可以用于dom對象的獲取,以及創(chuàng)建一個響應(yīng)式的普通對象類型

    • nextTick是一個函數(shù),它接受一個函數(shù)作為參數(shù),nextTick官網(wǎng)定義是&lsquo;將回調(diào)推遲到下一個 DOM 更新周期之后執(zhí)行&rsquo;,

    未使用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>

    vue3頁面加載完成后怎么獲取寬度、高度

    **注意:**這里的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>

    vue3頁面加載完成后怎么獲取寬度、高度

    關(guān)于“vue3頁面加載完成后怎么獲取寬度、高度”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“vue3頁面加載完成后怎么獲取寬度、高度”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

    向AI問一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI