vue实现一个滚动条
起初是想修改浏览器滚动条样式来达到效果 但是查阅了资料 浏览器滚动条不能修改宽度与位置 没办法只能自己写 首先是滚动条样式
<div class="scrollBar" v-if="roleList.length > 5">
<div
class="box"
@mousedown="move"
v-bind:style="{ width: activewidth + 'px' }"
></div>
</div>
样式
.scrollBar {
width: 500px;
height: 6px;
background: #d5dbf5;
margin: 0 auto;
margin-top: 72px;
border-radius: 4px;
position: relative;
.box {
width: 30px;
height: 100%;
background: #fff;
border-radius: 4px;
position: absolute;
left: 0;
}
.box:hover {
cursor: pointer;
}
}
滚动区域的样式这里就不写了
1 首先是滚动条滑块的宽度
mounted() {
let bgWidth = this.$refs.liList.clientWidth * this.roleList.length;
this.activewidth = 500 * (1065 / bgWidth);
},
2 给滑块添加鼠标事件
move(e) {
let odiv = e.target;
let disX = e.clientX - odiv.offsetLeft;
let viewArea = 500 - this.activewidth;
let bgWidth = this.$refs.liList.clientWidth * this.roleList.length;
document.onmousemove = (e) => {
let left = e.clientX - disX;
if (left < 0 || left > viewArea) {
document.onmousemove = null;
} else {
odiv.style.left = left + "px";
this.$refs.ScrollArea.style.left =
"-" + bgWidth * left / 500 + "px";
}
};
document.onmouseup = (e) => {
document.onmousemove = null;
document.onmouseup = null;
};
},
|