vue3獲取、設(shè)置元素高度的代碼舉例
前言
在web端常見的需求場(chǎng)景中,會(huì)經(jīng)常遇到table表格需要根據(jù)頁面可視區(qū)域使高度自適應(yīng)的情況。 傻喵(作者本人)昨天在嘗試使用vue3實(shí)現(xiàn)這個(gè)需求時(shí),看了幾篇網(wǎng)上寫的回答,都不太全面,所以干脆自己寫個(gè)總結(jié)吧.(第一次寫,輕噴QAQ)
正文
先一起來看看頁面結(jié)構(gòu)
在vue2中,實(shí)現(xiàn)這個(gè)效果大多是在mounted生命周期中操作DOM元素,進(jìn)行元素屬性的獲取和修改。 升級(jí)到vue3后,生命周期前都加了on,所以在onMounted這個(gè)生命周期中操作DOM。
代碼示例
1、首先獲取頁面可視區(qū)域、header組件,至于為什么在header組件外又套了一層div,是想實(shí)驗(yàn)另外一個(gè)東西,先賣個(gè)關(guān)子。
setup(props) { console.log(props); const hd = ref(null); let allHeight = ref(""); const test = ref(null); onMounted(() => { //可視區(qū)域高度 allHeight.value = `${document.documentElement.clientHeight}`; //header組件高度 let hdHeight = hd.value.$el.clientHeight; }); return { hd, test, clienHeight, }; },
const hd = ref(null)
定義的名字必須與HTML中<Header ref="hd" />
這里的值相同(不相同會(huì)報(bào)錯(cuò))
2、接下來就是給test組件高度賦值了,傻喵本來是想直接將值賦值的 test.value.clientHeight = headerHeight;
但是沒有實(shí)現(xiàn)效果,具體原因不得而知(有知道原因的可以在評(píng)論區(qū)告訴傻喵).
所以只能用另一種方法,style動(dòng)態(tài)綁定<Test ref="test" :style="testStyle" />
let testStyle = reactive({ height: "0px", });
testStyle.height = testHeight + "px";
這樣終于實(shí)現(xiàn)了DOM元素的賦值
3、關(guān)于在header組件外多加的一層div,是因?yàn)樯颠髟讷@取頁面元素時(shí),發(fā)現(xiàn)ref獲取的組件和div、span等基礎(chǔ)標(biāo)簽打印出的結(jié)構(gòu)不同。
如上圖,依次打印的分別為<div ref="top"></div>
以及它內(nèi)部的header組件,基礎(chǔ)標(biāo)簽會(huì)直接.value打印出來,而header組件會(huì)打印出一個(gè)Proxy對(duì)象(傻喵猜測(cè)應(yīng)該是跟vue3的響應(yīng)式有關(guān),有待考究)。
這同時(shí)導(dǎo)致了獲取兩者元素屬性方式的不同
div屬性直接可以const top = ref(null);
定義,并通過top.value.clientHeight
來獲取它的高度。
而header組件必須hd.value.$el.clientHeight
才可以.
下面貼上完整代碼
<template> <div ref="top"> <Header ref="hd" /> </div> <Test ref="test" :style="testStyle" /> </template> <script> import Header from "./components/Header.vue"; import Test from "./components/Test.vue"; import { onMounted, reactive, ref } from "vue"; export default { name: "App", components: { Header, Test, }, setup(props) { console.log(props); const hd = ref(null); const top = ref(null); const test = ref(null); let allHeight = ref(""); let testStyle = reactive({ height: "0px", }); onMounted(() => { allHeight.value = `${document.documentElement.clientHeight}`; let hdHeight = hd.value.$el.clientHeight; let testHeight = allHeight.value - hdHeight; testStyle.height = testHeight + "px"; console.log(top) console.log(hd) console.log(top.value.clientHeight) console.log(hd.value.$el.clientHeight) }); return { hd, top, test, testStyle, allHeight, }; }, }; </script> <style> #app { font-family: Avenir, Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin: 0px; padding: 0px; /* margin-top: 60px; */ } </style>
結(jié)語
以上就是使用vue3來操作元素高度的總結(jié),還有很多坑點(diǎn)需要去研究
到此這篇關(guān)于vue3獲取、設(shè)置元素高度的文章就介紹到這了,更多相關(guān)vue3獲取設(shè)置元素高度內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue純前端實(shí)現(xiàn)導(dǎo)出excel中的圖片與文件超鏈接
這篇文章主要為大家詳細(xì)介紹了Vue如何純前端實(shí)現(xiàn)導(dǎo)出excel中的圖片與文件超鏈接,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2025-03-03解決el-tree數(shù)據(jù)回顯時(shí)子節(jié)點(diǎn)部分選中父節(jié)點(diǎn)都全選中的坑
本文主要介紹了解決el-tree數(shù)據(jù)回顯時(shí)子節(jié)點(diǎn)部分選中父節(jié)點(diǎn)都全選中的坑,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Vue的事件響應(yīng)式進(jìn)度條組件實(shí)例詳解
這篇文章主要介紹了Vue的事件響應(yīng)式進(jìn)度條組件的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-02-02element el-input directive數(shù)字進(jìn)行控制
本文介紹了vue使用directive 進(jìn)行控制的方法,使用element開發(fā)的過程中遇到循環(huán)的數(shù)據(jù)只能輸入數(shù)字,并且有不要小數(shù)點(diǎn),有需要小數(shù)點(diǎn)的,就有一定的參考價(jià)值,有興趣的可以了解一下2018-10-10Vue3視頻播放器組件Vue3-video-play新手入門教程
這篇文章主要給大家介紹了關(guān)于Vue3視頻播放器組件Vue3-video-play新手入門教程的相關(guān)資料,本文實(shí)例為大家分享了vue-video-player視頻播放器的使用配置,供大家參考,需要的朋友可以參考下2023-12-12