vue3獲取、設(shè)置元素高度的代碼舉例
前言
在web端常見的需求場(chǎng)景中,會(huì)經(jīng)常遇到table表格需要根據(jù)頁(yè)面可視區(qū)域使高度自適應(yīng)的情況。 傻喵(作者本人)昨天在嘗試使用vue3實(shí)現(xiàn)這個(gè)需求時(shí),看了幾篇網(wǎng)上寫的回答,都不太全面,所以干脆自己寫個(gè)總結(jié)吧.(第一次寫,輕噴QAQ)
正文
先一起來(lái)看看頁(yè)面結(jié)構(gòu)

在vue2中,實(shí)現(xiàn)這個(gè)效果大多是在mounted生命周期中操作DOM元素,進(jìn)行元素屬性的獲取和修改。 升級(jí)到vue3后,生命周期前都加了on,所以在onMounted這個(gè)生命周期中操作DOM。
代碼示例
1、首先獲取頁(yè)面可視區(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、接下來(lái)就是給test組件高度賦值了,傻喵本來(lái)是想直接將值賦值的 test.value.clientHeight = headerHeight;但是沒(méi)有實(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)樯颠髟讷@取頁(yè)面元素時(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打印出來(lái),而header組件會(huì)打印出一個(gè)Proxy對(duì)象(傻喵猜測(cè)應(yīng)該是跟vue3的響應(yīng)式有關(guān),有待考究)。
這同時(shí)導(dǎo)致了獲取兩者元素屬性方式的不同
div屬性直接可以const top = ref(null);定義,并通過(guò)top.value.clientHeight來(lái)獲取它的高度。
而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é)語(yǔ)
以上就是使用vue3來(lái)操作元素高度的總結(jié),還有很多坑點(diǎn)需要去研究
到此這篇關(guān)于vue3獲取、設(shè)置元素高度的文章就介紹到這了,更多相關(guān)vue3獲取設(shè)置元素高度內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue2使用element-ui,el-table不顯示,用npm安裝方式
這篇文章主要介紹了vue2使用element-ui,el-table不顯示,用npm安裝方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
vue中多路由表頭吸頂實(shí)現(xiàn)的幾種布局方式
這篇文章主要介紹了vue項(xiàng)目多路由表頭吸頂實(shí)現(xiàn)的幾種布局方式,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
vue?eslint報(bào)錯(cuò)error?"Component?name?"*****"
這篇文章主要給大家介紹了關(guān)于vue?eslint報(bào)錯(cuò)error?“Component?name?“*****“?should?always?be?multi-word”的解決方法,文中通過(guò)圖文以及實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
vue中對(duì)監(jiān)聽esc事件和退出全屏問(wèn)題的解決方案
這篇文章主要介紹了vue中對(duì)監(jiān)聽esc事件和退出全屏問(wèn)題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
web面試MVC與MVVM區(qū)別及Vue為什么不完全遵守MVVM解答
這篇文章主要介紹了web面試中常問(wèn)問(wèn)題,MVC與MVVM區(qū)別以及Vue為什么不完全遵守MVVM的難點(diǎn)解答,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09

