vue窗口大小监听以及el-scrollbar的warp宽度设置以及滚动条高度调整

  data () {
    return {
      screenWidth: document.body.clientWidth
    }
  },
  mounted () {
    // 监听窗口大小
    window.onresize = () => {
      return (() => {
        this.screenWidth = document.body.clientWidth
      })()
    }
    this.$nextTick(function () {
      // 计算实际显示的高度(我减掉的是空白部分和分页所占的位置,实际情况自己看)
      const realHeight = this.$refs.infoTable.clientHeight - 153 - 101
      // 这个是源码计算滚动条最终高度的算式上的修改式
      const heightPercentage = (realHeight * 100 / this.$refs.elScroll.$refs.wrap.scrollHeight)
      this.$refs.elScroll.sizeHeight = (heightPercentage < 100) ? (heightPercentage + '%') : ''
      // 这里必须延迟执行,不然覆盖不了原来的高度
      setTimeout(() => {
        this.$set(this.$refs.elScroll.$el.childNodes[2].children[0].style, 'height', ((heightPercentage < 100) ? (heightPercentage + '%') : ''))
      }, 500)

      this.$refs.elScroll.$refs.wrap.style.display = 'none'
      this.$refs.elScroll.$refs.wrap.style.width = this.$refs.elScroll.$el.offsetWidth + 17 + 'px'
      this.$refs.elScroll.$refs.wrap.style.display = '-webkit-box'
    })
  },
  
  watch: {
    screenWidth (val) {
      this.screenWidth = val
      // 根据父容器宽度动态调整el-scrollbar的warp容器的宽度
      this.$refs.elScroll.$refs.wrap.style.display = 'none'
      this.$refs.elScroll.$refs.wrap.style.width = this.$refs.elScroll.$el.offsetWidth + 17 + 'px'
      this.$refs.elScroll.$refs.wrap.style.display = '-webkit-box'
    }
  }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值