vue的滾動(dòng)條插件實(shí)現(xiàn)代碼
這篇文章主要介紹了vue的滾動(dòng)條插件實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
效果如下

代碼如下
<template>
<div class="vue-scroll" ref="vueScrollW">
<div class="vue-scroll-w" ref="vueScroll" >
<div class="vue-scroll-c" :style="{width:cWidth}">
<slot></slot>
</div>
</div>
<div class="vue-scrollbar" v-if="rate < 1">
<div class="vue-scrollbar-thumb"
:style="{height:thumbH,top:thumbTop}"
@mousedown="onmousedown"
@mouseup="onmouseup"
></div>
</div>
</div>
</template>
<script>
export default {
name:"vue-scroll",
data(){
return {
thumb:0,
top:0,
rate:2,
moveTop:null,
isDrag:false,
cw:10,
observer:null
}
},
computed:{
thumbH(){
return this.thumb + "px";
},
thumbTop(){
return this.top + "px";
},
cWidth(){
return this.cw + "%";
}
},
updated(){
if(!window.MutationObserver){
this.refresh();
}
},
mounted(){
var me = this;
me.$refs.vueScroll.addEventListener("scroll",me.onscroll.bind(me));
window.addEventListener("mouseup",me.onmouseup.bind(me));
window.addEventListener("mousemove",me.onmousemove.bind(me));
if(window.MutationObserver){
//MutationObserver 最低只兼容 ie11
me.observer = new window.MutationObserver(me.mutationCallback.bind(me));
me.observer.observe(me.$refs.vueScroll, {
attributes: true,
childList: true,
subtree: true
});
}
me.refresh();
},
methods:{
mutationCallback(mutationsList){
this.refresh();
},
onscroll(){
this.top = this.$refs.vueScroll.scrollTop * this.rate; //計(jì)算滾動(dòng)條所在的高度
if(this.rate < 1){
this.eventTrigger(this.top);
}
},
refresh(){
var me = this;
var vueScroll = me.$refs.vueScroll;
var rate = vueScroll.clientHeight / vueScroll.scrollHeight; //滾動(dòng)條高度的比例,也是滾動(dòng)條top位置的比例
me.rate = rate;
if(rate < 1){
//需要出現(xiàn)滾動(dòng)條,并計(jì)算滾動(dòng)條的高度
me.thumb = rate * vueScroll.clientHeight; //滾動(dòng)條的 bar 的高度
//計(jì)算出原生的滾動(dòng)條的寬度
var w = me.$refs.vueScrollW.clientWidth;
//根據(jù)比例,轉(zhuǎn)換為內(nèi)容的百分比
me.cw = w/vueScroll.clientWidth *100;
}else{
//不需要出現(xiàn)滾動(dòng)條
me.thumb = 0;
me.cw = 10;
}
},
onmousedown(){
this.isDrag = true;
this.moveTop = null;
},
onmouseup(){
this.isDrag = false;
},
onmousemove(e){
if(this.isDrag){
if(this.moveTop !== null){
var speed = e.screenY - this.moveTop;
var top = this.top + speed;
this.scrollThumb(top);
}
this.moveTop = e.screenY;
e.preventDefault();
}
},
scrollThumb(top){
if(top < 0 ){
top = 0;
}
if(top > this.$refs.vueScroll.clientHeight-this.thumb){
top = this.$refs.vueScroll.clientHeight-this.thumb;
}
this.$refs.vueScroll.scrollTop = top/this.rate;
this.top = top;
},
eventTrigger(top){
if(top === 0){
this.$emit("reachTop"); //到達(dá)頂部
}
if(top === this.$refs.vueScroll.clientHeight-this.thumb){
this.$emit("reachBottom"); //到達(dá)底部與
}
this.$emit("vuescroll",this.$refs.vueScroll.scrollTop,this.top);//返回內(nèi)容滾動(dòng)的高度 和 滾動(dòng)條所在的高度
},
scrollTo(scrollTop){
//對(duì)外的api,滾動(dòng)的內(nèi)容的哪里
this.$refs.vueScroll.scrollTop = scrollTop;
this.$nextTick(()=>{
this.onscroll();
})
}
},
destroyed(){
var me = this;
me.$refs.vueScroll && me.$refs.vueScroll.removeEventListener("scroll",me.onscroll.bind(me));
window.removeEventListener("mouseup",me.onmouseup.bind(me));
window.removeEventListener("mousemove",me.onmousemove.bind(me));
me.observer&&me.observer.disconnect();
}
}
</script>
<style lang="scss" scoped>
.vue-scroll{
height: 100%;
width: 100%;
overflow: hidden;
position: relative;
.vue-scroll-w{
width: 1000%;
height: 100%;
overflow: auto;
.vue-scroll-c{
position: relative;
width: 10%;
}
}
.vue-scrollbar{
position: absolute;
z-index: 1;
right: 0;
top: 0;
width: 4px;
height: 100%;
background: #EEEEEE;
opacity: 0.6;
.vue-scrollbar-thumb{
position: absolute;
top: 0;
right: 0;
width: 4px;
border-radius: 4px;
background: #D3D3D3;
&:hover{
background: #bbb;
}
&:active{
background: #aaa;
}
}
}
}
</style>
使用
<template>
<div class="scroll">
<vueScroll>
<ul>
<li v-for="item in 60" :key="item">{{item}}</li>
</ul>
</vueScroll>
</div>
</template>
<script>
import vueScroll from "@/components/vue-scroll.vue"
export default {
data(){
return {
count:60
}
},
components:{
vueScroll
},
mounted(){
}
}
</script>
<style lang="less" scoped>
.scroll{
width: 400px;
height: 600px;
margin: 0 auto;
border: 1px solid red;
ul{
li{
line-height: 30px;
border-bottom: 1px solid #ddd;
}
}
}
</style>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue中使用vue-seamless-scroll插件實(shí)現(xiàn)列表無縫滾動(dòng)效果
- Vue 實(shí)現(xiàn)一個(gè)簡(jiǎn)單的鼠標(biāo)拖拽滾動(dòng)效果插件
- vue滾動(dòng)插件better-scroll使用詳解
- 簡(jiǎn)單方法實(shí)現(xiàn)Vue?無限滾動(dòng)組件示例
- Vue?transition組件簡(jiǎn)單實(shí)現(xiàn)數(shù)字滾動(dòng)
- vue3實(shí)現(xiàn)數(shù)字滾動(dòng)特效實(shí)例詳解
- vue 支持百萬量級(jí)的無限滾動(dòng)組件詳解
相關(guān)文章
vue resource發(fā)送請(qǐng)求的幾種方式
這篇文章主要介紹了vue resource發(fā)送請(qǐng)求的幾種方式,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
在 Vue 應(yīng)用中使用 Netlify 表單功能的方法詳解
Netlify 帶有內(nèi)置表單處理功能,可以用來存儲(chǔ)表單數(shù)據(jù),下載 csv 文件,同時(shí)可以在接收到新的提交時(shí)發(fā)送郵件通知或者通過配置 webhook 發(fā)送請(qǐng)求。這篇文章主要介紹了在 Vue 應(yīng)用中使用 Netlify 表單功能,需要的朋友可以參考下2019-06-06
關(guān)于Vue中使用alibaba的iconfont矢量圖標(biāo)的問題
這篇文章主要介紹了Vue使用alibaba的iconfont矢量圖標(biāo)的問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
Vue實(shí)現(xiàn)搜索 和新聞列表功能簡(jiǎn)單范例
本文通過實(shí)例代碼給大家介紹了Vue實(shí)現(xiàn)搜索 和新聞列表功能簡(jiǎn)單范例,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2018-03-03
用Vue.js方法創(chuàng)建模板并使用多個(gè)模板合成
在本篇文章中小編給大家分享了關(guān)于如何使用Vue.js方法創(chuàng)建模板并使用多個(gè)模板合成的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-06-06
vue3選項(xiàng)式api如何監(jiān)控?cái)?shù)組變化
這篇文章主要介紹了vue3選項(xiàng)式api如何監(jiān)控?cái)?shù)組變化問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06

