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

在vue2中,實現(xiàn)這個效果大多是在mounted生命周期中操作DOM元素,進行元素屬性的獲取和修改。 升級到vue3后,生命周期前都加了on,所以在onMounted這個生命周期中操作DOM。
代碼示例
1、首先獲取頁面可視區(qū)域、header組件,至于為什么在header組件外又套了一層div,是想實驗另外一個東西,先賣個關(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" />這里的值相同(不相同會報錯)
2、接下來就是給test組件高度賦值了,傻喵本來是想直接將值賦值的 test.value.clientHeight = headerHeight;但是沒有實現(xiàn)效果,具體原因不得而知(有知道原因的可以在評論區(qū)告訴傻喵).
所以只能用另一種方法,style動態(tài)綁定<Test ref="test" :style="testStyle" />let testStyle = reactive({ height: "0px", });testStyle.height = testHeight + "px";這樣終于實現(xiàn)了DOM元素的賦值
3、關(guān)于在header組件外多加的一層div,是因為傻喵在獲取頁面元素時,發(fā)現(xiàn)ref獲取的組件和div、span等基礎(chǔ)標簽打印出的結(jié)構(gòu)不同。

如上圖,依次打印的分別為<div ref="top"></div>以及它內(nèi)部的header組件,基礎(chǔ)標簽會直接.value打印出來,而header組件會打印出一個Proxy對象(傻喵猜測應(yīng)該是跟vue3的響應(yīng)式有關(guān),有待考究)。
這同時導(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é),還有很多坑點需要去研究
到此這篇關(guān)于vue3獲取、設(shè)置元素高度的文章就介紹到這了,更多相關(guān)vue3獲取設(shè)置元素高度內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue純前端實現(xiàn)導(dǎo)出excel中的圖片與文件超鏈接
這篇文章主要為大家詳細介紹了Vue如何純前端實現(xiàn)導(dǎo)出excel中的圖片與文件超鏈接,文中的示例代碼講解詳細,感興趣的小伙伴可以參考一下2025-03-03
解決el-tree數(shù)據(jù)回顯時子節(jié)點部分選中父節(jié)點都全選中的坑
本文主要介紹了解決el-tree數(shù)據(jù)回顯時子節(jié)點部分選中父節(jié)點都全選中的坑,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2022-08-08
element el-input directive數(shù)字進行控制
本文介紹了vue使用directive 進行控制的方法,使用element開發(fā)的過程中遇到循環(huán)的數(shù)據(jù)只能輸入數(shù)字,并且有不要小數(shù)點,有需要小數(shù)點的,就有一定的參考價值,有興趣的可以了解一下2018-10-10
Vue3視頻播放器組件Vue3-video-play新手入門教程
這篇文章主要給大家介紹了關(guān)于Vue3視頻播放器組件Vue3-video-play新手入門教程的相關(guān)資料,本文實例為大家分享了vue-video-player視頻播放器的使用配置,供大家參考,需要的朋友可以參考下2023-12-12

