JSFiddle - React, Tailwind, and code Playground

HTML

<div class="scrollbar">
	<div class="scrollbar-content">
    <div class="scrollbar-view" style="position: relative">
      <ul class="box">
        <li>11111</li>
        <li>11111</li>
        <li>11111</li>
        <li>11111</li>
        <li>11111</li>
        <li>11111</li>
        <li>11111</li>
        <li>11111</li>
        <li>11111</li>
        <li>11111</li>
        <li>11111</li>
        <li>11111</li>
        <li>11111</li>
        <li>11111</li>
        <li>11111</li>
        <li>11111</li>
      </ul>
    </div>
	</div>
	<div class='scrollbar-bar'>
		<div ref="thumb" class="scrollbar-thumb"></div>
	</div>
</div>
<button id="add">增加li</button>
<button id="del">删除li</button>

CSS

*{margin:0;padding:0;list-style: none;}

li{ line-height: 46px;  height: 46px; }

.scrollbar{
  position: relative;
  overflow: hidden;
}
.scrollbar-bar{
  position: absolute;
  right: 2px;
  bottom: 2px;
  z-index: 1;
  opacity: 0;
  width: 6px;
  height: 100%;
  border-radius: 4px;
  background-color: rgba(0,0,0,.1);
  transition: opacity 120ms ease-out;
}
.scrollbar-thumb {
  position: relative;
  display: block;
  width: 100%;
  height: 0;
  border-radius: inherit;
  background-color: rgba(135,141,153,.3);
  transition: .3s background-color;
  cursor: pointer;
}
.scrollbar:hover .scrollbar-bar{
  opacity: 1;
  transition: opacity 340ms ease-out;
}
.scrollbar-content{
  height: 400px;
  overflow: auto;
}

.resize-triggers {visibility: hidden;opacity: 0;}
.resize-triggers, .resize-triggers > div {content: " ";display: block;position: absolute;top: 0;left: 0;height: 100%;width: 100%;overflow: hidden;}

.resize-triggers > div {
  overflow: auto;
}

JavaScript

let wrap = document.getElementsByClassName("scrollbar-content")[0];
let bar = document.getElementsByClassName("scrollbar-bar")[0];
let thumb = document.getElementsByClassName("scrollbar-thumb")[0];
let cursorDown = false; //是否按下滚动条
let clickThumbAxis = 0; //鼠标点击滚动条的位置

let gutter = getScrollWidth();{}
if(gutter){wrap.style.marginRight = `-${gutter}px`};
updateThumb()

wrap.addEventListener('scroll',handleScroll);

bar.addEventListener("click",clickTrackHandle);

thumb.addEventListener("mousedown",function (e) {
		startDrag(e);
		clickThumbAxis = e.currentTarget.offsetHeight - (e.clientY - e.currentTarget.getBoundingClientRect().top);
	});

function getScrollWidth(){
    const outer = document.createElement("div");
    outer.style.width = '100px';
    outer.style.visibility = "hidden";
    outer.style.position = "absolute";
    outer.style.top = "-9999px";
    document.body.appendChild(outer);

    const widthNoScroll = outer.offsetWidth;
    outer.style.overflow = "scroll";

    const inner = document.createElement("div");
    inner.style.width = "100%";
    outer.appendChild(inner);

    const widthWithScroll = inner.offsetWidth;
    outer.parentNode.removeChild(outer);
    scrollBarWidth = widthNoScroll - widthWithScroll;

    return scrollBarWidth;
}

function updateThumb(){
    let heightPercentage = (wrap.clientHeight * 100 / wrap.scrollHeight);
    thumb.style.height = heightPercentage + "%";   
}

function handleScroll(){
    let moveY = (wrap.scrollTop *100 / wrap.clientHeight);
    //通过计算出来的百分比,然后对滚动条执行translate移动
    thumb.style.transform = "translateY(" + moveY + "%)";
}

function clickTrackHandle(e){
		//获得点击位置与滚动框顶部之间的距离
		const offset = Math.abs(e.target.getBoundingClientRect().top - e.clientY)
		//让点击位置处于滚动条的中间
		const thumbHalf = thumb.offsetHeight / 2;
		//计算出滚动条在滚动框的百分比位置
		const thumbPositionPercentage = (offset - thumbHalf) * 100 / wrap.offsetHeight;
		//通过改变scrollTop来操作。所有操作滚动条的最后一步都是通过handleScroll来实现
		wrap.scrollTop =...