JS原生实现自定义滚动条

实现思路:

1.外层设置一个div,在外层的div里面设置一个特别高的div(高度为外层5个div的高度),在高的div里面设置5个div,每个div的高度,都和父级的高度相同

2.在外层div的右侧边框以里自定义一个滚动条,(本人用div设置样式作为滚动条)

3.最外层的div添加onmousewheel事件,使用e.wheelDelta来获取每次滑动的距离,若为正数则向上滑动,将每次增加的数,赋给滚动条的top值,若为负数则向下互动,将每次减少的值,赋值给滚动条的top值,并对特别高的div作对应的上下平移处理

4.注意:滚动条的高度需要和高的div的高度成比例滑动

 

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>自定义滚轮事件</title>
		<style type="text/css">
			*{
				padding: 0;
				margin: 0;
			}
			#wrap{
				height: 500px;
				width: 300px;
				position: relative;
				/*超出隐藏*/
				overflow: hidden;
				margin: 200px auto 0;
				border: 3px solid black;
			}
			#content{
				width: 300px;
				/*不需要设置高度,可被图片撑开*/
				position: absolute;
				left: 0;
				top: 0;
				border: 1px solid red;
			}
			#content  > div{
				width: 294px;
				/*去除图片间的间隙*/
				vertical-align: top;
				height: 500px;
				border: 1px solid red;
				text-align: center;
				font-size: 100px;
				line-height: 500px;
			}
			#sliderWrap{
				height:100% ;
				width:16px ;
				background-color: greenyellow;
				position: absolute;
				right: 0;
				top: 0;
			}
			#slider{
				width: 10px;
				height: 50px;
				background-color: blue;
				position: absolute;
				left: 3px;
				top: 0px;
				border-radius: 10px;
			}
		</style>
	</head>
	<body>
		<div id="wrap">
			<div id="content">
				<div>1</div>
				<div>2</div>
				<div>3</div>
				<div>4</div>
				<div>5</div>
			</div>
			<!--右侧滚动条部分-->
			<div id="sliderWrap">
				<div id="slider">
					
				</div>
			</div>
		</div>
	</body>
	<script type="text/javascript">
		var wrapDiv = document.getElementById("wrap");
		var contentDiv = document.getElementById("content");
		var sliderWrap = document.getElementById("sliderWrap");
		var slider = document.getElementById("slider");
		//设置比例
		//clientHeight - 不包括border
		var scale = wrapDiv.clientHeight / contentDiv.clientHeight;
		//设置滑块的高度
		var h1 = sliderWrap.clientHeight * scale;
		//为了合理设置高度,设置滑块的最小高度
		if (h1 < 50) {
			h1 = 50;
		}else if (scale >= 1) {
			//说明当前内容能过完全显示在可视区域内,不需要滚动条
			sliderWrap.style.display = "none";
		}
		//设置滑块的高度
		slider.style.height = h1 +"px";
		//设置y轴的增量
		var y = 0;
		//为wrap添加滚轮事件
		wrapDiv.onmousewheel = function(e){
			var event1 = event || e
			if (event.wheelDelta < 0) {
				//滑动条向下滚动
				y += 10;
			}else if (event.wheelDelta > 0) {
				//滑动条向上滚动
				y -= 10;
			}
			//y变化时说明在滚动,此时使滚动条发生滚动,以及设置content内容部分滚动
			//判断极端情况,滑块不能划出屏幕
			if (y <= 0) {
				//滑块最多滑到顶部
				y = 0;
			}
			if(y >= sliderWrap.clientHeight - slider.clientHeight){
				//滑块最多滑到最底部
				y = sliderWrap.clientHeight - slider.clientHeight;
			}
			//更新滑块的位置
			slider.style.top = y +"px";
			scale = wrapDiv.clientHeight / contentDiv.clientHeight;
			contentDiv.style.top = - y / scale +"px";
		}
	</script>
</html>

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,UniApp 中可以使用 `scroll-view` 组件来实现自定义滚动条。以下是一个简单的示例: ```html <scroll-view class="scroll-view" scroll-y> <div class="content"> <!-- 列表内容 --> </div> <div class="scroll-bar"></div> </scroll-view> ``` 其中,`.scroll-view` 为 `scroll-view` 组件的样式类,`.content` 为列表内容的容器,`.scroll-bar` 为自定义滚动条的容器。 接着,在样式文件中可以添加以下代码: ```css .scroll-view { position: relative; /* 父容器需要设置 position: relative; */ height: 100vh; /* 设置高度,这里使用了 viewport height 单位 */ } .content { height: 2000rpx; /* 列表内容高度 */ } .scroll-bar { position: absolute; top: 0; right: 0; width: 6px; height: 50px; /* 滚动条高度 */ background-color: #ccc; border-radius: 3px; opacity: 0; /* 初始透明度为 0,只有在滚动时才显示 */ transition: opacity 0.3s; } .scroll-view::-webkit-scrollbar { width: 0; /* 隐藏原生滚动条 */ } .scroll-view::-webkit-scrollbar-thumb { width: 0; height: 0; background-color: transparent; } ``` 这里使用了 `position: absolute;` 和 `right: 0;` 将自定义滚动条定位到父容器的右侧,使用 `opacity` 和 `transition` 实现滚动条的渐隐渐现效果。同时,使用了 `-webkit-scrollbar` 相关样式将原生滚动条隐藏。 最后,在 `scroll-view` 组件的 `scroll` 事件中可以添加以下代码: ```js onScroll(e) { const { scrollTop, scrollHeight, clientHeight } = e.detail; const bar = this.$refs.bar; // 计算滚动条高度和位置 const barHeight = clientHeight / scrollHeight * clientHeight; const barTop = scrollTop / scrollHeight * clientHeight; // 设置滚动条位置和高度 bar.style.height = barHeight + 'px'; bar.style.top = barTop + 'px'; // 显示滚动条 bar.style.opacity = 1; // 3 秒后隐藏滚动条 clearTimeout(this.timer); this.timer = setTimeout(() => { bar.style.opacity = 0; }, 3000); } ``` 在 `scroll` 事件中,通过 `scrollTop`、`scrollHeight` 和 `clientHeight` 计算出滚动条的高度和位置,并设置到滚动条容器的样式中。同时,通过设置 `opacity` 实现滚动条的渐隐渐现效果。这里还设置了一个 3 秒的计时器,在滚动结束后 3 秒自动隐藏滚动条。 希望能对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值