使用原生JS写的自定义滚动条
css代码部分
<style>
* {
margin:0;
padding:0;
}
#box {
width:500px;
height:600px;
border:1px solid #000;
margin:100px auto 0;
position: relative;
overflow: hidden;
}
#box .content {
width:480px;
padding-left:5px;
box-sizing: border-box;
position:absolute;
}
#box .scroll {
width:20px;
height:600px;
border:1px solid rgb(216, 35, 35);
box-sizing: border-box;
position:absolute;
top:0;
right:0;
}
#box .scroll a {
display:block;
width:20px;
height:25px;
text-decoration: none;
background-color: #ccc;
}
#box .scroll .oBar {
width:20px;
height:550px;
position: relative;
}
/* 要记得给滑块设置position属性,滑块才可以移动 */
#box .scroll .oBar .slider {
width:20px;
height:50px;
background-color: rgb(154, 212, 205);
position:absolute;
}
</style>
js部分
此部分要特别注意滚动的文本内容高度和滚动条滚动的距离成比例。
步骤:
1.slider拖拽,滑块在oBar中滑动
2.点击👆,👇键,滑块会滚动,文本也会随之变化————结合间歇定时器实现运动
3.在oBox区域触发鼠标滚轮事件,文本跟滚动条一起滚动
<script>
window.onload = function(){
var oBox = document.getElementById('box');
var oCon = oBox.