vue.js動(dòng)態(tài)設(shè)置VueComponent高度遇到的問(wèn)題及解決
vue.js動(dòng)態(tài)設(shè)置VueComponent高度的問(wèn)題
1.獲取HTML元素高度
<div v-for="data in list">
? ? <div ref="abc">{{data.id}}</div>
</div>mounted(){
? ? console.log(this.$refs.abc[0].clientHeight);//獲取第一個(gè)div元素的高度
? ? this.$refs.abc[0].style.height = 100 +'px';//動(dòng)態(tài)設(shè)置HTML元素高度
}注意:
- 1.此處用到v-for循環(huán),this.$refs.abc返回的是個(gè)HTMLElement數(shù)組
- 2.this.$refs在DOM元素掛載完成后才可以調(diào)用
- 3.不可以通過(guò)this.$refs.abc[0].clientHeight = 100 +'px'設(shè)置高度,因?yàn)閏lientHeight屬性是只讀的,不允許修改。
- 4.注意加'px'單位
2.獲取VueComponent標(biāo)簽生成的元素的高度
<Row v-for="(data,idx) in list" :key="idx">
? ?<Col ref="leftCol">
? ? ? <p>{{data.id}}</p>
? ?</Col>
? ?<Col ref="rightCol">
? ? ? <p>{{data.id}}</p>
? ?</Col>
</Row>
mounted(){
? ? for(let i = 0; i < this.list.length; i++){
? ? ? console.log(this.$refs.leftCol[i].$el.clientHeight);//獲取左邊列元素的高度
? ? ? console.log(this.$refs.rightCol[i].$el.clientHeight);//獲取右邊列元素的高度
? ? ? this.$refs.leftCol[0].$el.style.height = 100 +'px';//動(dòng)態(tài)設(shè)置VueComponent元素高度 ? ?
? ? };
}注意:
this.$refs.leftCol返回的是個(gè)VueComponent數(shù)組,this.$refs.leftCol[i]返回的是個(gè)VueComponent元素,而不是HTMLElement
3.判斷一個(gè)對(duì)象是jQuery對(duì)象還是DOM對(duì)象
var jqueryObject = $("#a");
jqueryObject instanceof jQuery; //jqueryObject 是jQuery對(duì)象
var domObject = document.querySelector("#a");
domObject instanceof jQuery; //domObject不是jQuery對(duì)象
domObject instanceof HTMLElement; //domObject是DOM對(duì)象vue動(dòng)態(tài)獲取、設(shè)置組件高度
<template>
<el-row>
<el-col :span="24">
<el-row ref="headerMenu" class="header-menu">
<el-col :span="24">
<el-menu router mode="horizontal">
<el-menu-item index="1" route="/global-overview">全局概覽</el-menu-item>
<el-menu-item index="2" route="/e-commerce-business">電商業(yè)務(wù)</el-menu-item>
<el-menu-item index="3" route="/douniao-business">抖鳥業(yè)務(wù)</el-menu-item>
<el-menu-item index="4" route="/administrative-business">行政業(yè)務(wù)</el-menu-item>
<el-menu-item index="5" route="/admin">管理員入口</el-menu-item>
</el-menu>
</el-col>
</el-row>
<el-row ref="routerView">
<router-view></router-view>
</el-row>
</el-col>
</el-row>
</template>
<script>
export default {
name: "home-page",
mounted() {
/**
* when the component is hung, dynamically obtain the height of the header menu,
* and set this value to router view as margin top
*/
this.$refs.routerView.$el["style"].marginTop = `${this.$refs.headerMenu.$el["offsetHeight"]}px`;
}
}
</script>
<style scoped>
.header-menu {
position: fixed;
top: 0;
width: 100%;
z-index: 999;
}
</style>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
ArcGis?API?for?js在vue.js中的使用示例詳解
這篇文章主要為大家介紹了ArcGis?API?for?js在vue.js中的使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
antd vue 如何調(diào)整checkbox默認(rèn)樣式
這篇文章主要介紹了antd vue 如何調(diào)整checkbox默認(rèn)樣式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
記一次Vue.js混入mixin的使用(分權(quán)限管理頁(yè)面)
這篇文章主要介紹了記一次Vue.js混入mixin的使用(分權(quán)限管理頁(yè)面),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04
vite打包只生成了一個(gè)css和js文件問(wèn)題的解決方法
今天整了一個(gè)項(xiàng)目,試了下打包,發(fā)下打包后只生成了一個(gè)css文件,和一個(gè)js文件,這樣肯定是不行的,因?yàn)檫@樣這個(gè)文件的包大小很大,第一次訪問(wèn)會(huì)導(dǎo)致白屏問(wèn)題,所以本文給大家介紹了vite打包只生成了一個(gè)css和js文件問(wèn)題的解決方法,需要的朋友可以參考下2024-05-05
element el-tooltip動(dòng)態(tài)顯示隱藏(有省略號(hào)顯示,沒(méi)有省略號(hào)不顯示)
本文主要介紹了element el-tooltip動(dòng)態(tài)顯示隱藏,主要實(shí)現(xiàn)有省略號(hào)顯示,沒(méi)有省略號(hào)不顯示,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
vue2實(shí)現(xiàn)可復(fù)用的輪播圖carousel組件詳解
這篇文章主要為大家詳細(xì)介紹了vue2實(shí)現(xiàn)可復(fù)用的輪播圖carousel組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11

