您好,登錄后才能下訂單哦!
本文將為大家詳細(xì)介紹“vue實(shí)現(xiàn)拖拽進(jìn)度條的方法”,內(nèi)容步驟清晰詳細(xì),細(xì)節(jié)處理妥當(dāng),而小編每天都會(huì)更新不同的知識(shí)點(diǎn),希望這篇“vue實(shí)現(xiàn)拖拽進(jìn)度條的方法”能夠給你意想不到的收獲,請(qǐng)大家跟著小編的思路慢慢深入,具體內(nèi)容如下,一起去收獲新知識(shí)吧。
具體內(nèi)容如下
組件代碼:
<template> <div> <div class="slider" ref="slider"> <div class="process" :></div> <div class="thunk" ref="trunk" :> <div class="block"></div> <div class="tips"> <!-- <span>{{scale*100}}</span> --> <i class="fas fa-caret-down"></i> </div> </div> </div> <div> <button @click=" () => { this.per++; } " > +</button >{{ per }}%<button @click=" () => { if (this.per > 0) { this.per--; } } " > - </button> </div> </div> </template> <script> /* * min 進(jìn)度條最小值 * max 進(jìn)度條最大值 * v-model 對(duì)當(dāng)前值進(jìn)行雙向綁定實(shí)時(shí)顯示拖拽進(jìn)度 * */ export default { props: ["min", "max", "value"], data() { return { slider: null, //滾動(dòng)條DOM元素 thunk: null, //拖拽DOM元素 per: this.value, //當(dāng)前值 }; }, //渲染到頁(yè)面的時(shí)候 mounted() { this.slider = this.$refs.slider; this.thunk = this.$refs.trunk; var _this = this; 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ì)通過(guò)計(jì)算屬性修改left,width // 拖拽的時(shí)候獲取的新width var newWidth = e.clientX - disX + width; // 拖拽的時(shí)候得到新的百分比 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 () { document.onmousemove = document.onmouseup = null; }; return false; }; }, computed: { // 設(shè)置一個(gè)百分比,提供計(jì)算slider進(jìn)度寬度和trunk的left值 // 對(duì)應(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() { if (this.slider) { return this.slider.offsetWidth * this.scale + "px"; } else { return 0 + "px"; } }, left() { if (this.slider) { return ( this.slider.offsetWidth * this.scale - this.thunk.offsetWidth / 2 + "px" ); } else { return 0 + "px"; } }, }, }; </script> <style> .box { margin: 100px auto 0; width: 80%; } .clear:after { content: ""; display: block; clear: both; } .slider { user-select: none; position: relative; margin: 20px 0; width: 400px; height: 10px; background: #e4e7ed; border-radius: 5px; cursor: pointer; } .slider .process { position: absolute; left: 0; top: 0; width: 112px; height: 10px; border-radius: 5px; background: #81b159; } .slider .thunk { position: absolute; left: 100px; top: -7px; width: 20px; height: 20px; } .slider .block { width: 20px; height: 20px; border-radius: 50%; border: 2px solid #409eff; background: rgba(255, 255, 255, 1); transition: 0.2s all; } .slider .tips { position: absolute; left: -7px; bottom: 30px; min-width: 15px; text-align: center; padding: 4px 8px; /* background: #000; */ border-radius: 5px; height: 24px; color: #fff; } .slider .tips i { position: absolute; margin-left: -5px; left: 50%; bottom: -9px; font-size: 16px; color: #000; } .slider .block:hover { transform: scale(1.1); opacity: 0.6; } </style>
調(diào)用:
<template> <slider :min="0" :max="100" v-model="per"></slider> </template> <script> import slider from "@/components/slider"; export default { data() { return {}; }, computed: { per: { get() { return 0; }, set(val) { console.log(val); }, }, }, components: { slider }, mounted() {}, methods: {}, }; </script> <style > </style>
如果你能讀到這里,小編希望你對(duì)“vue實(shí)現(xiàn)拖拽進(jìn)度條的方法”這一關(guān)鍵問(wèn)題有了從實(shí)踐層面最深刻的體會(huì),具體使用情況還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想閱讀更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。