表格表头固定,超过一定高度,除表头部分显示滚动条(Vue)

// 表头固定,超过一定高度,table除表头部分外显示滚动条
<template>
<div>
<table>
  <thead :class="dataHeight > 400 ? 'thead' : '']">
        <tr class="bgtit">
            <th width="18%">A</th>
            <th width="22%">B</th>
            <th width="22%">C</th>
            <th width="18%">D</th>
            <th width="20%">E</th>
        </tr>
    </thead>
    <tbody ref="table" :class="[dataHeight > 400 ? 'tbody' : '']">
            <td width="18%">A</td>
            <td width="22%">B</td>
            <td width="22%">C</td>
            <td width="18%">D</td>
            <td width="20%">E</td>
    </tbody>
</table>
</div>
</template>

<script>
export default{
    name:'demo',
    data() {
        return {
            dataHeight :0
        }
    },
    watch: {
    allData: function () {
      this.$nextTick(function () {
        console.log(this.$refs.table.offsetHeight);
        this.dataHeight = this.$refs.table.offsetHeight;
      });
    },
  },

}
</script>

<style scoped>
/* 设置滚动属性 */
.initialCondition tbody {
  display: block;
  /* max-height: 150px; */
  /* overflow-y: scroll;   */
}

/*设置头与内容自动对齐*/
.initialCondition table thead,
tbody tr {
  display: table;
  width: 100%;
  table-layout: fixed;
}

/*给滚动条预留宽度*/
.initialCondition table thead {
  /* width: calc( 100% - 0.6em);    */
  background: #eff0f5;
}

.tbody {
  max-height: 350px;
  overflow-y: scroll;
}
.thead {
  width: calc(100% - 0.6em) !important;
}
</style>

参考:table实现头部固定,内容滚动效果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值