溫馨提示×

溫馨提示×

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

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

vue.js動態(tài)設(shè)置VueComponent高度遇到問題如何解決

發(fā)布時間:2022-08-16 10:50:30 來源:億速云 閱讀:324 作者:iii 欄目:開發(fā)技術(shù)

這篇“vue.js動態(tài)設(shè)置VueComponent高度遇到問題如何解決”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“vue.js動態(tài)設(shè)置VueComponent高度遇到問題如何解決”文章吧。

    vue.js動態(tài)設(shè)置VueComponent高度的問題

    1.獲取HTML元素高度

    <div v-for="data in list">
        <div ref="abc">{{data.id}}</div>
    </div>
    mounted(){
        console.log(this.$refs.abc[0].clientHeight);//獲取第一個div元素的高度
        this.$refs.abc[0].style.height = 100 +'px';//動態(tài)設(shè)置HTML元素高度
    }

    注意:

    • 1.此處用到v-for循環(huán),this.$refs.abc返回的是個HTMLElement數(shù)組

    • 2.this.$refs在DOM元素掛載完成后才可以調(diào)用

    • 3.不可以通過this.$refs.abc[0].clientHeight = 100 +'px'設(shè)置高度,因為clientHeight屬性是只讀的,不允許修改。

    • 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';//動態(tài)設(shè)置VueComponent元素高度    
        };
    }

    注意:

    this.$refs.leftCol返回的是個VueComponent數(shù)組,this.$refs.leftCol[i]返回的是個VueComponent元素,而不是HTMLElement

    3.判斷一個對象是jQuery對象還是DOM對象

    var jqueryObject = $("#a");
    jqueryObject instanceof jQuery; //jqueryObject 是jQuery對象
    var domObject = document.querySelector("#a");
    domObject instanceof jQuery; //domObject不是jQuery對象
    domObject instanceof HTMLElement; //domObject是DOM對象

    vue動態(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>

    以上就是關(guān)于“vue.js動態(tài)設(shè)置VueComponent高度遇到問題如何解決”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。

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

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

    AI