angular自定义滚动条,滚动时显示,否则隐藏

html:

<div id="div_nav" class="div-class2"></div>

scss:

.div-class::-webkit-scrollbar {
  width: 8px;	// 滚动条宽度
  height: 8px	// 滚动条高度
}
    
.div-class::-webkit-scrollbar-thumb {
  border-radius: 4px;			// 滚动条圆角
  -moz-border-radius: 4px;
  -webkit-border-radius: 4px;
  background-color: #999;		// 滚动条颜色
}

.div-class::-webkit-scrollbar-track {
  background-color: #ffffff;	// 滚动条背景色
}

.div-class {
  overflow-y: auto;
  width: 100px;
  height: 300px;
  color: white;
}

.div-class2::-webkit-scrollbar {
  display: none;	// 隐藏
  width: 8px;		// 滚动条宽度
  height: 8px		// 滚动条高度
}
    
.div-class2::-webkit-scrollbar-thumb {
  border-radius: 4px;			// 滚动条圆角
  -moz-border-radius: 4px;
  -webkit-border-radius: 4px;
  background-color: #999;		// 滚动条颜色
}

.div-class2::-webkit-scrollbar-track {
  background-color: #ffffff;	// 滚动条背景色
}

.div-class2 {
  overflow-y: auto;
  width: 100px;
  height: 300px;
  color: white;
}

ts:

roll = 0; // 滚动的值
stop = 0; // 对比时间的值
timer = null;
  
ngOnInit() {
    const that = this
    document.getElementById('div_nav').addEventListener('scroll', function(event) {
      const e = event || event.target;
      // 每次滑动前都清除一遍我们定义的定时器
      clearTimeout(that.timer);
      $('#div_nav').attr('class', 'div-class');
      // 这里我设置的是300毫秒,您可以更改您想要的间隔秒数
      that.timer = setTimeout(() => {
        that.JudgeScroll()
      }, 300);
      that.roll = (<Element>(<Event>e).srcElement).scrollTop;
    })
  }

  JudgeScroll() {
    this.stop = document.getElementById('div_nav').scrollTop;
    if(this.stop === this.roll) { // 滚动停止
      setTimeout(() => {
        $('#div_nav').attr('class', 'div-class2');
      }, 1000)
    }
  }

注:此种方法仅适用于webkit内核的浏览器

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值