Vue table表单滑动固定头部和左列

     之前一直想实现一种table表单滑动固定头部和左列的效果,这两天改bug抽时间就做了一个,先看效果。

顶部固定
顶部固定
左侧固定
左侧固定

  

  1.  这里主要用到左右两个table标签,每一个table都由thead和tbody组成;固定thead,设置tbody滑动。
      <div class="container">
        <div class="left-div" @mouseenter="leftenter" @mouseleave="leftleave">
          <table>
            <thead>
              <tr>
                <th v-for="(item,index) in columns" :key="index">{{item.title}}</th>
              </tr>
            </thead>
            <tbody ref="leftbody" style="margin-right:-15px">
              <tr v-for="(item,index) in columnsBottom" :key="index">
                <td class="left-td">{{item.title}}</td>
              </tr>
            </tbody>
          </table>
        </div>
    
        <div class="right-div" @mouseenter="rightenter" @mouseleave="rightleave">
          <table >
            <thead>
              <tr>
                <th v-for="(item,index) in columnsBottom" :key="index">{{item.title}}</th>
              </tr>
            </thead>
            <tbody ref="rightbody">
              <tr v-for="(item,index) in columnsBottom" :key="index">
                <td v-for="(item,index) in columnsBottom" :key="index">{{item.title}}</td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    
    <style scoped>
    .container {
      display: flex;
      height: 800px;
    }
    .container table {
      height: 100%;
    }
    
    .right-div {
      flex: 1;
      overflow: scroll;
    }
    .left-div {
      overflow: hidden;
    }
    
    .left-td{
        cursor: pointer;
        color: blueviolet
    }
    
    td,
    th {
      text-align: center;
      min-width: 300px;
      height: 60px;
    }
    
    th{
      color: blue
    }
    
    table thead,
    tbody tr {
      display: table;
      table-layout: fixed;
      border-bottom: 1px solid rebeccapurple;
    }
    thead {
      height: 60px;
    }
    tbody {
      height: 720px;
      display: block;
      overflow: scroll;
    }
    </style>

     

 2.设置滚动监听,左边滚动联动右边,右边滚动联动左边。

 left.addEventListener(
      "scroll",
      () => {
        if (this.isRight) {
          return;
        }
        let scrollTop = left.scrollTop;
        right.scrollTop = scrollTop;
      },
      false
    );
    right.addEventListener(
      "scroll",
      () => {
        if (this.isLeft) {
          return;
        }
        let scrollTop = right.scrollTop;
        left.scrollTop = scrollTop;
      },
      false
    );

 3.现在的高度什么的都是写死的,这是开发中高度要动态获取。 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值