vue進度條組件實現(xiàn)代碼(可拖拽可點擊)
1.詳情介紹
在日常的開發(fā)當中,隨著項目的需求復(fù)雜化,自定義組件也越來越常見,而且擴展性也比一些組件庫要更加全面,比如視頻播放器的進度條。
可自定義設(shè)置以下屬性:
- 當前進度value,默認50
- 是否可拖拽isDrag,默認true
- 設(shè)置最小值min,默認0
- 設(shè)置最大值max,默認100
- 進度條顏色bgColor,默認#4ab157
效果如下圖:

2.編碼介紹
template部分
<template>
<div class="slider" ref="slider" @click.stop="handelClickSlider">
<div class="process" :style="{ width,background:bgColor }"></div>
<div class="thunk" ref="trunk" :style="{ left }">
<div class="block" ref="dot"></div>
</div>
</div>
</template>
js部分
<script>
/*
* min 進度條最小值
* max 進度條最大值
* v-model 對當前值進行雙向綁定實時顯示拖拽進度
* */
export default {
props: {
// 最小值
min: {
type: Number,
default: 0,
},
// 最大值
max: {
type: Number,
default: 100,
},
// 當前值
value: {
type: Number,
default: 0,
},
// 進度條顏色
bgColor: {
type: String,
default: "#4ab157",
},
// 是否可拖拽
isDrag: {
type: Boolean,
default: true,
},
},
data() {
return {
slider: null, //滾動條DOM元素
thunk: null, //拖拽DOM元素
per: this.value, //當前值
};
},
mounted() {
this.slider = this.$refs.slider;
this.thunk = this.$refs.trunk;
var _this = this;
if (!this.isDrag) return;
this.thunk.onmousedown = function (e) {
var width = parseInt(_this.width);
var disX = e.clientX;
document.onmousemove = function (e) {
// value, left, width
// 當value變化的時候,會通過計算屬性修改left,width
// 拖拽的時候獲取的新width
var newWidth = e.clientX - disX + width;
// 計算百分比
var scale = newWidth / _this.slider.offsetWidth;
_this.per = Math.ceil((_this.max - _this.min) * scale + _this.min); //取整
// 限制值大小
_this.per = Math.max(_this.per, _this.min);
_this.per = Math.min(_this.per, _this.max);
_this.$emit("input", _this.per);
};
document.onmouseup = function () {
//當拖拽停止發(fā)送事件
_this.$emit("stop", _this.per);
//清除拖拽事件
document.onmousemove = document.onmouseup = null;
};
};
},
methods: {
handelClickSlider(event) {
//禁止點擊
if (!this.isDrag) return;
const dot = this.$refs.dot;
if (event.target == dot) return;
//獲取元素的寬度l
let width = this.slider.offsetWidth;
//獲取元素的左邊距
let ev = event || window.event;
//獲取當前點擊位置的百分比
let scale = ((ev.offsetX / width) * 100).toFixed(2);
this.per = scale;
},
},
computed: {
// 設(shè)置一個百分比,提供計算slider進度寬度和trunk的left值
// 對應(yīng)公式為 當前值-最小值/最大值-最小值 = slider進度width / slider總width
// trunk left = slider進度width + trunk寬度/2
scale() {
return (this.per - this.min) / (this.max - this.min);
},
width() {
return this.slider ? this.slider.offsetWidth * this.scale + "px" : "0px";
},
left() {
return this.slider ? this.slider.offsetWidth * this.scale - this.thunk.offsetWidth / 2 +"px" : "0px";
},
},
watch: {
value: {
handler: function () {
this.per = this.value;
},
},
},
};
</script>
css部分
<style scoped>
.box {
margin: 100px auto 0;
width: 80%;
}
.clear:after {
content: "";
display: block;
clear: both;
}
.slider {
position: relative;
margin: 20px 0;
width: 100%;
height: 10px;
top: 50%;
background: #747475;
border-radius: 5px;
cursor: pointer;
z-index: 99999;
}
.slider .process {
position: absolute;
left: 0;
top: 0;
width: 112px;
height: 10px;
border-radius: 5px;
background: #4ab157;
z-index: 111;
}
.slider .thunk {
position: absolute;
left: 100px;
top: -4px;
width: 10px;
height: 6px;
z-index: 122;
}
.slider .block {
width: 16px;
height: 16px;
border-radius: 50%;
background: rgba(255, 255, 255, 1);
transition: 0.2s all;
}
.slider .block:hover {
transform: scale(1.1);
opacity: 0.6;
}
</style>
組件使用
<slisd :min="0" :max="100" :value="50" :isDrag="true" bgColor="#4ab157"></slisd>
總結(jié)
到此這篇關(guān)于vue進度條組件實現(xiàn)的文章就介紹到這了,更多相關(guān)vue進度條組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue如何通過router-link或者button跳轉(zhuǎn)到一個新的頁面
這篇文章主要介紹了vue如何通過router-link或者button跳轉(zhuǎn)到一個新的頁面,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
vue+element-ui:使用el-dialog時彈框不出現(xiàn)的解決
這篇文章主要介紹了vue+element-ui:使用el-dialog時彈框不出現(xiàn)的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
vuejs如何清空表單數(shù)據(jù)、刪除對象中的空屬性公共方法
這篇文章主要介紹了vuejs如何清空表單數(shù)據(jù)、刪除對象中的空屬性公共方法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03
vue動態(tài)渲染svg、添加點擊事件的實現(xiàn)
這篇文章主要介紹了vue動態(tài)渲染svg、添加點擊事件的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2020-03-03
一步一步實現(xiàn)Vue的響應(yīng)式(對象觀測)
這篇文章主要介紹了一步一步實現(xiàn)Vue的響應(yīng)式(對象觀測),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2019-09-09

