vue.js动态设置VueComponent高度遇到问题如何解决

发布时间:2022-08-16 10:50:30 作者:iii
来源:亿速云 阅读:307

这篇“vue.js动态设置VueComponent高度遇到问题如何解决”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“vue.js动态设置VueComponent高度遇到问题如何解决”文章吧。

vue.js动态设置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';//动态设置HTML元素高度
}

注意:

2.获取VueComponent标签生成的元素的高度

<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';//动态设置VueComponent元素高度    
    };
}

注意:

this.$refs.leftCol返回的是个VueComponent数组,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动态获取、设置组件高度

<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">电商业务</el-menu-item>
            <el-menu-item index="3" route="/douniao-business">抖鸟业务</el-menu-item>
            <el-menu-item index="4" route="/administrative-business">行政业务</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>

以上就是关于“vue.js动态设置VueComponent高度遇到问题如何解决”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

推荐阅读:
  1. ListView 动态设置高度问题
  2. iview中怎么设置table高度动态

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vue.js

上一篇:怎么利用Java手写阻塞队列

下一篇:ASP.NET MVC怎么使用jQuery ui的progressbar实现进度条

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》