实时动态滚动判断距离显示顶部栏不闪烁

40 篇文章 0 订阅
34 篇文章 0 订阅

问题

在一开始做实时滚动条顶部滚动对时候然后使用,在tabContainerDom滚动到距离顶部一段距离的时候显示headerFixed

<div ref="headerFixed"  class="header-fixed" :class="{'show': showFixedBlock}"></div>

.header-fixed {
    opacity: 0;
    position: fixed;
    z-index: 99;
    top: 0;
    width: 100%;
    height: rem(52);
    background-color: #fff;
    box-sizing: content-box;
    &.show {
        opacity: 1;
    }
}

高度判断

document.addEventListener('scroll', Util.throttle(() => {
        let tabContainerDom = this.$refs.tabContainer;
      let headerFixedHeight = this.$refs.headerFixed.getBoundingClientRect().height;
          if (offsetTop <= headerFixedHeight) {
              this.showFixedBlock = true;
          }
          else {
              this.showFixedBlock = false;
          }
      }), 20);

这样写页面顶部的在滑动的时候会一直闪

解决办法

this.$refs.headerFixed.offsetHeight;

使用offsetHeight获取高度他就好了。这样页面就不闪了。

原因分析

基本是一样的,但是有tranfrom设置的是时候
getBoundingClientRect是动态计算的值,offsetHeight就是原本css设置的值
参考链接: JavaScript getBoundingClientRect() vs offsetHeight while calculate element height

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值