element table表头固定悬浮,element table表头黏性布局,element table表头悬浮窗口

element table表头固定悬浮,element table表头黏性布局,element table表头悬浮窗口

效果图

图一

在这里插入图片描述

图二

![在这里插入图片描述](https://img-blog.csdnimg.cn/b7230fe9193b426db6204c5c4d35e393.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBATG9zIHJhaW4=,size_20,color_FFFFFF,t_70,g_se,x_16

图三

![在这里插入图片描述](https://img-blog.csdnimg.cn/f9ec7ac8d2794f55af4fcb7bd09b67a2.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBATG9zIHJhaW4=,size_20,color_FFFFFF,t_70,g_se,x_16

图四

在这里插入图片描述

修改方案

针对需要table表头黏性布局的页面添加mixins

使用方法

import scroll from '@/views/mixins/scroll'
export default {
  mixins: [scroll]
}

mixins文件代码

const scorll = {
  methods: {
    ishowBottomDiv(bottomDiv, scrollBar, tableBodyDomWrap){
      if(bottomDiv.getBoundingClientRect().top>document.body.clientHeight){
        if(scrollBar.style.display === 'none'){
          scrollBar.style.display = 'block';
          scrollBar.scrollLeft = tableBodyDomWrap.scrollLeft;
        }
        scrollBar.style.display = 'block';
      } else {
        scrollBar.style.display = 'none';
      }
    }
  },
  mounted() {
    let self = this;
    let bottomDiv = document.createElement('div');
    let bar = document.createElement('div');
    let scrollBar = document.createElement('div');
    let tableBodyDom = document.querySelector('.el-table__body')
    let tableBodyDomWrap = document.querySelector('.el-table__body-wrapper')
    this.$nextTick(() => {
      let dom = document.querySelector('.el-table')
      dom.append(bottomDiv);
      let MutationObserver = window.MutationObserver || window.webkitMutationObserver || window.MozMutationObserver;
      window.mutationObserver = new MutationObserver(function (mutations) {
        self.ishowBottomDiv(bottomDiv, scrollBar, tableBodyDomWrap)
      })
      window.mutationObserver.observe(dom, {
        childList: true,
        subtree: true, // 子节点的变动(新增、删除或者更改)
      })
      bar.style.width = tableBodyDom.getBoundingClientRect().width+'px'
      bar.style.height = '1px'
      scrollBar.style.width = dom.getBoundingClientRect().width+'px'
      scrollBar.style.height = '30px'
      scrollBar.style.overflow = 'auto'
      scrollBar.style.zIndex = '10000'
      scrollBar.style.bottom = '0px'
      scrollBar.style.position = 'fixed'
      scrollBar.append(bar);
      dom.append(scrollBar);
    })
    window.addEventListener(
      'scroll',
      () => {
        let dom = document.querySelector('.el-table')
        if (!dom) {
          return
        }
        if(scrollBar.style.display === 'block'){
          tableBodyDomWrap.scrollLeft = scrollBar.scrollLeft;
        } else {
          scrollBar.scrollLeft = tableBodyDomWrap.scrollLeft;
        }
        self.ishowBottomDiv(bottomDiv, scrollBar, tableBodyDomWrap)
        let elTable = document.querySelector('.el-table__header-wrapper')
        let elTableFixed = document.querySelector(
          '.el-table__fixed .el-table__fixed-header-wrapper'
        )
        let elTableFixedRight = document.querySelector(
          '.el-table__fixed-right .el-table__fixed-header-wrapper'
        )
        let elTableFixedRightHeader = document.querySelector(
          '.el-table__fixed-right  .el-table__header'
        )
        if (dom.getBoundingClientRect().top < 64) {
          if (document.querySelector('.zhanweitablehead')) {
            //
          } else {
            let zhanwei = document.createElement('div')
            zhanwei.className = 'zhanweitablehead'
            zhanwei.style.width = elTable.getBoundingClientRect().width + 'px'
            zhanwei.style.height = elTable.getBoundingClientRect().height + 'px'
            dom.insertBefore(zhanwei, dom.children[0])
          }
          if (elTable) {
            elTable.style.position = 'fixed'
            elTable.style.zIndex = '1000'
            elTable.style.top = '64px'
            elTable.style.left = '256px'
            elTable.style.width = dom.getBoundingClientRect().width + 'px'
            elTable.style.overflow = 'hidden'
          }
          if (elTableFixed) {
            elTableFixed.style.position = 'fixed'
            elTableFixed.style.zIndex = '10000'
            elTableFixed.style.top = '64px'
            elTableFixed.style.left = '256px'
            elTableFixed.style.width = '320px'
            elTableFixed.style.overflow = 'hidden'
          }
          if (elTableFixedRight) {
            elTableFixedRight.style.width = document.querySelector(
              '.el-table__fixed-right'
            ).style.width
            elTableFixedRight.style.position = 'fixed'
            elTableFixedRight.style.zIndex = '10000'
            elTableFixedRight.style.top = '64px'
            elTableFixedRight.style.height = '50px'
            elTableFixedRight.style.right = '36px'
            elTableFixedRight.style.overflow = 'hidden'
            elTableFixedRightHeader.style.position = 'absolute'
            elTableFixedRightHeader.style.right = '0'
          }
        } else {
          if (!dom) {
            return
          }
          if (document.querySelector('.zhanweitablehead')) {
            document
              .querySelector('.el-table')
              .removeChild(document.querySelector('.zhanweitablehead'))
          }
          if (elTable) elTable.style = {}
          if (elTableFixed) elTableFixed.style = {}
          if (elTableFixedRight) elTableFixedRight.style = {}
          if (elTableFixedRightHeader)
            elTableFixedRightHeader.style.position = 'initial'
        }
      },
      true
    )
  },
  destroyed() {
    window.removeEventListener('scroll');
    window.mutationObserver.disconnect();
  },
}
export default scorll;

添加公用样式

.is-scrolling-left ~ .el-table__fixed-right,
  .is-scrolling-middle ~ .el-table__fixed-right {
    .el-table__fixed-header-wrapper {
      border-left: 2px solid #d6d6d657;
    }
  }
  .is-scrolling-right ~ .el-table__fixed,
  .is-scrolling-middle ~ .el-table__fixed {
    .el-table__fixed-header-wrapper {
      border-right: 2px solid #d6d6d657;
    }
  }
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值