vue進(jìn)度條組件實(shí)現(xiàn)代碼(可拖拽可點(diǎn)擊)
1.詳情介紹
在日常的開發(fā)當(dāng)中,隨著項(xiàng)目的需求復(fù)雜化,自定義組件也越來越常見,而且擴(kuò)展性也比一些組件庫要更加全面,比如視頻播放器的進(jìn)度條。
可自定義設(shè)置以下屬性:
- 當(dāng)前進(jìn)度value,默認(rèn)50
- 是否可拖拽isDrag,默認(rèn)true
- 設(shè)置最小值min,默認(rèn)0
- 設(shè)置最大值max,默認(rèn)100
- 進(jìn)度條顏色bgColor,默認(rèn)#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 進(jìn)度條最小值
* max 進(jìn)度條最大值
* v-model 對當(dāng)前值進(jìn)行雙向綁定實(shí)時(shí)顯示拖拽進(jìn)度
* */
export default {
props: {
// 最小值
min: {
type: Number,
default: 0,
},
// 最大值
max: {
type: Number,
default: 100,
},
// 當(dāng)前值
value: {
type: Number,
default: 0,
},
// 進(jìn)度條顏色
bgColor: {
type: String,
default: "#4ab157",
},
// 是否可拖拽
isDrag: {
type: Boolean,
default: true,
},
},
data() {
return {
slider: null, //滾動(dòng)條DOM元素
thunk: null, //拖拽DOM元素
per: this.value, //當(dāng)前值
};
},
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
// 當(dāng)value變化的時(shí)候,會(huì)通過計(jì)算屬性修改left,width
// 拖拽的時(shí)候獲取的新width
var newWidth = e.clientX - disX + width;
// 計(jì)算百分比
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 () {
//當(dāng)拖拽停止發(fā)送事件
_this.$emit("stop", _this.per);
//清除拖拽事件
document.onmousemove = document.onmouseup = null;
};
};
},
methods: {
handelClickSlider(event) {
//禁止點(diǎn)擊
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;
//獲取當(dāng)前點(diǎn)擊位置的百分比
let scale = ((ev.offsetX / width) * 100).toFixed(2);
this.per = scale;
},
},
computed: {
// 設(shè)置一個(gè)百分比,提供計(jì)算slider進(jìn)度寬度和trunk的left值
// 對應(yīng)公式為 當(dāng)前值-最小值/最大值-最小值 = slider進(jìn)度width / slider總width
// trunk left = slider進(jìn)度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進(jìn)度條組件實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)vue進(jìn)度條組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實(shí)現(xiàn)時(shí)間倒計(jì)時(shí)功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)時(shí)間倒計(jì)時(shí)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
為Vue3?組件標(biāo)注?TS?類型實(shí)例詳解
這篇文章主要為大家介紹了為Vue3?組件標(biāo)注?TS?類型實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Vue腳手架配置代理服務(wù)器的兩種方式(小結(jié))
本文主要介紹了Vue腳手架配置代理服務(wù)器的兩種方式(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
vue項(xiàng)目實(shí)現(xiàn)文件下載進(jìn)度條功能
這篇文章主要介紹了vue項(xiàng)目實(shí)現(xiàn)文件下載進(jìn)度條功能,本文通過具體實(shí)現(xiàn)代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09
vue中實(shí)現(xiàn)div可編輯并插入指定元素與樣式
這篇文章主要給大家介紹了關(guān)于vue中實(shí)現(xiàn)div可編輯并插入指定元素與樣式的相關(guān)資料,文中通過代碼以及圖文將實(shí)現(xiàn)的方法介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用vue具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09
Vue中使用ElementUI使用第三方圖標(biāo)庫iconfont的示例
這篇文章主要介紹了Vue中使用ElementUI使用第三方圖標(biāo)庫iconfont的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10

