Vue如何拖動滑塊
更新時間:2024年07月27日 09:55:22 作者:三次元挨踢汪
這篇文章主要介紹了Vue如何拖動滑塊問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
Vue拖動滑塊
拖動進(jìn)度條
- Vue
div頁面數(shù)據(jù)樣式
<div class="bg">
<p class="txt0">0</p>
<i class="A" :style="{ width: Avalue + '%' }">
<p class="Aptxt">A:{{ Avalue }}</p>
</i>
<i class="B" :style="{ width: Bvalue + '%' }">
<p class="Bptxt">B:{{ Bvalue }}</p>
</i>
<i class="C" :style="{ width: Cvalue + '%' }">
<p class="Cptxt">C:{{ Cvalue }}</p>
</i>
<p class="txt100">100</p>
<span
class="btnA"
:style="{ left: positionX_A + 'px' }"
@mousedown="moveA"
></span>
<span
class="btnB"
:style="{ left: positionX_B + 'px' }"
@mousedown="moveB"></span>
<span
class="btnC"
:style="{ left: positionX_C + 'px' }"
@mousedown="moveC"
></span>
</div>
- data數(shù)據(jù)
Avalue: 0,
Bvalue: 0,
Cvalue: 0,
positionX_A: 0,
positionX_B: 0,
positionX_C: 0,
//接口返回的數(shù)據(jù)
mormal_level: null,
minor_level: null,
major_level: null,監(jiān)聽事件的發(fā)生改變時改變對應(yīng)的數(shù)值
watch: {
templateIndex(val) {
this.index = val ? val : 0;
},
positionX_A(val) {
this.Avalue = Math.ceil(((val + 10) / 435) * 100);
},
positionX_B(val) {
this.Bvalue = Math.ceil(((val + 10) / 435) * 100);
},
positionX_C(val) {
this.Cvalue = Math.ceil(((val + 10) / 435) * 100);
},
},查詢時重新給賦值到滑塊上
//該方法主要用于后端返回數(shù)據(jù)分別賦給的Avalue,Bvalue,Cvalue,之后重新計算樣式寬度
//可以不用調(diào)
setPosition() {
this.positionX_A = parseInt((this.Avalue / 100) * 435 - 10);
this.positionX_B = parseInt((this.Bvalue / 100) * 435 - 10);
this.positionX_C = parseInt((this.Cvalue / 100) * 435 - 10);
},//移動滑塊時的方法
moveA(e) {
let odiv = e.target; //獲取目標(biāo)元素
console.log(e,'測試數(shù)據(jù)')
//算出鼠標(biāo)相對元素的位置
let disX = e.clientX - odiv.offsetLeft;
document.onmousemove = (e) => {
let left = e.clientX - disX;
if (left <= this.positionX_B && left >= -10 && left <= 425) {
this.positionX_A = left;
odiv.style.left = left + "px";
}
};
document.onmouseup = (e) => {
document.onmousemove = null;
document.onmouseup = null;
};
},
moveB(e) {
let odiv = e.target; //獲取目標(biāo)元素
//算出鼠標(biāo)相對元素的位置
let disX = e.clientX - odiv.offsetLeft;
document.onmousemove = (e) => {
let left = e.clientX - disX;
if (
left >= this.positionX_A &&
left <= this.positionX_C &&
left >= -10 &&
left <= 425
) {
this.positionX_B = left;
odiv.style.left = left + "px";
}
};
document.onmouseup = (e) => {
document.onmousemove = null;
document.onmouseup = null;
};
},
moveC(e) {
let odiv = e.target; //獲取目標(biāo)元素
//算出鼠標(biāo)相對元素的位置
let disX = e.clientX - odiv.offsetLeft;
document.onmousemove = (e) => {
let left = e.clientX - disX;
if (left >= this.positionX_B && left >= -10 && left <= 425) {
this.positionX_C = left;
odiv.style.left = left + "px";
}
};
document.onmouseup = (e) => {
document.onmousemove = null;
document.onmouseup = null;
};
},Css對應(yīng)的樣式
.bg {
position: relative;
display: flex;
width: 435px;
height: 10px;
margin-top: 10px;
background-color: #53bf6d;
.txt0 {
position: absolute;
left: 0;
top: 15px;
}
.txt100 {
position: absolute;
top: 15px;
right: 0;
}
i {
position: absolute;
display: inline-block;
height: 10px;
}
.A {
background: #ff5757;
z-index: 3;
.Aptxt {
position: absolute;
top: 15px;
right: -5px;
}
}
.btnA {
position: absolute;
top: -5px;
width: 20px;
height: 20px;
background: #fff;
border-radius: 50%;
border: solid 2px #0065bc;
z-index: 3;
cursor: ew-resize;
}
.btnB {
content: "";
position: absolute;
top: -5px;
width: 20px;
height: 20px;
background: #fff;
border-radius: 50%;
border: solid 2px #0065bc;
z-index: 4;
cursor: ew-resize;
}
.btnC {
content: "";
position: absolute;
top: -5px;
width: 20px;
height: 20px;
background: #fff;
border-radius: 50%;
border: solid 2px #0065bc;
z-index: 4;
cursor: ew-resize;
}
.B {
background: #ffec58;
z-index: 2 !important;
.Bptxt {
position: absolute;
top: 15px;
right: -5px;
}
}
.C {
background: #ffba00;
z-index: 1 !important;
.Cptxt {
position: absolute;
top: 15px;
right: -5px;
}
}
}總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決antd 表單設(shè)置默認(rèn)值initialValue后驗(yàn)證失效的問題
這篇文章主要介紹了解決antd 表單設(shè)置默認(rèn)值initialValue后驗(yàn)證失效的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Vue實(shí)現(xiàn)登錄功能全套超詳細(xì)講解(含封裝axios)
這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)登錄功能(含封裝axios)的相關(guān)資料,Vue是現(xiàn)在前端最流行的框架之一,作為前端開發(fā)人員應(yīng)該要熟練的掌握它,需要的朋友可以參考下2023-10-10
在Vue中實(shí)現(xiàn)隨hash改變響應(yīng)菜單高亮
這篇文章主要介紹了在Vue中實(shí)現(xiàn)隨hash改變響應(yīng)菜單高亮的方法,文中還通過實(shí)例代碼給大家介紹了vue關(guān)于點(diǎn)擊菜單高亮與組件切換的相關(guān)知識,需要的朋友可以參考下2020-03-03
詳解Vue的watch中的immediate與watch是什么意思
這篇文章主要介紹了詳解Vue的watch中的immediate與watch是什么意思,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
vue3中g(shù)etCurrentInstance獲取組件實(shí)例踩坑詳細(xì)記錄
getCurrentInstance()是Vue.js3?Composition?API中的一個函數(shù),它的作用是獲取當(dāng)前組件的實(shí)例對象,下面這篇文章主要給大家介紹了關(guān)于vue3中g(shù)etCurrentInstance獲取組件踩坑的相關(guān)資料,需要的朋友可以參考下2024-02-02
Vue 實(shí)現(xiàn)復(fù)制功能,不需要任何結(jié)構(gòu)內(nèi)容直接復(fù)制方式
今天小編就為大家分享一篇Vue 實(shí)現(xiàn)復(fù)制功能,不需要任何結(jié)構(gòu)內(nèi)容直接復(fù)制方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11

