用原生js代码实现虚拟滚动条

效果图
在这里插入图片描述

直接上代码

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      box-sizing: border-box;
    }

    body {
      background-color: #f5f5f5;
    }

    .wrap {
      position: relative;
      width: 300px;
      height: 200px;
    }

    .scrollbar-wrap {
      position: absolute;
      height: 200px;
      width: 8px;
      top: 0;
      right: 0;
    }

    .div1 {
      height: 200px;
      width: 300px;
      margin: 50px auto;
      background: white;
      padding: 0 20px;
      overflow: auto;
      position: relative;
    }

    .div1::-webkit-scrollbar {
      display: none;
    }

    li {
      height: 200px;
      background-color: bisque;
    }

    ul {
      margin: 0;
      padding: 0;
      list-style: none;
      background-color: aquamarine;
      height: 400px;
    }

    .scrollbar {
      display: none;
      position: absolute;
      right: 0;
      top: 0;
      background-color: #cdcdcd;
      width: 8px;
      border-radius: 4px;
      height: 10px;
      user-select: none;
      transition: background-color 0.5s cubic-bezier(0.075, 0.82, 0.165, 1);
    }

    .scrollbar:hover {
      background-color: #8d8d8d;
    }

    .wrap:hover .scrollbar {
      display: block;
    }

    .show {
      display: block;
      background-color: #8d8d8d;
    }
  </style>
</head>

<body>
  <div class="wrap">
    <div class="div1">
      <ul>
        <li>1</li>
      </ul>

    </div>
    <div class="scrollbar-wrap">
      <div class="scrollbar" id="scrollbar"></div>
    </div>
  </div>
  <script>
    const scrollbar = document.getElementById('scrollbar')
    const container = document.getElementsByClassName('div1')[0]
    const { offsetHeight, scrollHeight } = container
    scrollbar.style.height = parseInt(offsetHeight / scrollHeight * 100) + '%'

    container.addEventListener('scroll', () => {
      const { scrollTop } = container
      scrollbar.style.top = parseInt(scrollTop / scrollHeight * 100) + '%'
    })

    let flag = false
    let eventTop = 0
    let scrollTop = 0
    scrollbar.addEventListener('mousedown', (e) => {
      flag = true
      eventTop = e.clientY
      scrollTop = scrollbar.offsetTop
    })
    document.addEventListener('mousemove', (e) => {
      if (!flag) return
      scrollbar.classList.add('show')
      // 滚动方向
      let direction = e.clientY > eventTop ? 'down' : 'up'
      // 移动距离
      const moveLen = Math.abs(e.clientY - eventTop)
      let top = 0
      if (direction === 'up') {
        if (scrollTop - moveLen < 0) {
          top = 0
        } else {
          top = scrollTop - moveLen
        }
      } else {
        if (scrollTop + moveLen + scrollbar.offsetHeight > offsetHeight) {
          top = offsetHeight - scrollbar.offsetHeight
        } else {
          top = scrollTop + moveLen
        }
      }
      scrollbar.style.top = top + 'px'
      const rate = top / offsetHeight
      container.scrollTop = scrollHeight * rate
    })
    document.addEventListener('mouseup', () => {
      flag = false
      scrollbar.classList.remove('show')
    })
  </script>
</body>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值