vue實(shí)現(xiàn)移動(dòng)端原生小球滑塊
本文實(shí)例為大家分享了vue實(shí)現(xiàn)移動(dòng)端原生小球滑塊的具體代碼,供大家參考,具體內(nèi)容如下
效果
用到的一些事件
阻止默認(rèn)事件:ev.preventDefault && ev.preventDefault();
獲取寬度:getBoundingClientRect().width
點(diǎn)擊位置在元素的位置:ev.changedTouches[0].pageX
<template> ? <div id="app"> ? ? <div class="slider"> ? ? ? <div class="line"></div> ? ? ? <div class="line ac"></div> ? ? ? <div class="box" @touchstart="fnStart"></div> ? ? ? <div class="box as" @touchstart="fnStart"></div> ? ? </div> ? </div> </template>
js
<script> export default { ? methods: { ? ? fnStart(ev) { ? ? ? // 計(jì)算點(diǎn)擊位置在元素的坐標(biāo) ? ? ? this.disX = ev.changedTouches[0].pageX - ev.target.offsetLeft; ? ? ? // 獲取父節(jié)點(diǎn) ? ? ? this.parent = ev.target.parentNode; ? ? ? // 獲取元素的寬 ? ? ? this.ow = this.parent.getBoundingClientRect().width; ? ? ? // 計(jì)算除了元素的寬盒子還剩下多寬 ? ? ? this.clienw = this.ow - ev.target.getBoundingClientRect().width; ? ? ? ? // 獲取左邊小圓球 ? ? ? this.lcircle = this.parent.children[2]; ? ? ? // 獲取右邊小圓球 ? ? ? this.rcircle = this.parent.children[3]; ? ? ? ? // 獲取變色條 ? ? ? this.line = this.parent.children[1]; ? ? ? ? document.ontouchmove = this.fnmove; ? ? ? document.ontouchend = this.fnend; ? ? }, ? ? fnmove(ev) { ? ? ? // 計(jì)算移動(dòng)的距離 ? ? ? this.ll = ev.changedTouches[0].pageX - this.disX; ? ? ? // 判斷不能讓小圓球到盒子外面 ? ? ? if (this.ll < 0) this.ll = 0; ? ? ? if (this.ll > this.clienw) this.ll = this.clienw; ? ? ? // 右邊線條 ? ? ? if (ev.target.className.indexOf("as") != -1) { ? ? ? ? this.line.style.width =this.ll - this.parent.children[2].offsetLeft + "px"; ? ? ? ? // 右邊推動(dòng)左邊小圓球 ? ? ? ? // 判斷如果右邊小球移動(dòng)到左邊小于左邊小球offsetLeft的距離 如果當(dāng)前為0 加一個(gè)小圓球的寬他們就不會(huì)重疊 ? ? ? ? console.log(this.ll) ? ? ? ? if (this.ll < this.lcircle.offsetLeft + 30) { ? ? ? ? ? // 如果this.ll大于左邊小球的值 當(dāng)前this.ll-30就是左邊小球left的值 ? ? ? ? ? this.ind = this.ll - 30; ? ? ? ? ?? ? ? ? ? ? console.log(this.ind) ? ? ? ? ? // 判斷當(dāng)前左邊位置過等于0 就讓他左邊的位置等于0 就不會(huì)到盒子外面 ? ? ? ? ? if (this.ind <= 0) { ? ? ? ? ? ? this.ind = 0; ? ? ? ? ? } ? ? ? ? ? // 如果this.ll的值小于小圓球的寬 兩個(gè)圓就會(huì)重疊 ?所以讓右邊圓的left值為30 ? ? ? ? ? if (this.ll < 30) { ? ? ? ? ? ? this.ll = 30; ? ? ? ? ? } ? ? ? ? ? ? this.line.style.left = this.ind + "px"; ? ? ? ? ? this.lcircle.style.left = this.ind + "px"; ? ? ? ? } ? ? ? } else { ? ? ? ? // 左線條 ? ? ? ? // 獲取左邊的距離 ? ? ? ? this.line.style.left = this.ll + "px"; ? ? ? ? // 當(dāng)前this.ll就是line多出來的寬 ? 如果左邊不動(dòng) offsetleft的值是300 ? this.ll是移動(dòng)的距離 ? ? ? ? this.line.style.width = ? ? ? ? ? this.parent.children[3].offsetLeft - this.ll + "px"; ? ? ? ? // 左邊推動(dòng)右邊小圓球 ?要把右邊小球+30 不然兩個(gè)小球就會(huì)重合到一起 ? ? ? ? if (this.ll + 30 > this.rcircle.offsetLeft) { ? ? ? ? ? this.indX = this.ll + 30; ? ? ? ? ? ? if (this.indX > this.clienw) { ? ? ? ? ? ? this.indX = this.clienw; ? ? ? ? ? } ? ? ? ? ? ? ? ? // 判斷當(dāng)前移動(dòng)的距離不能超過 this.clienw-30如果超過就會(huì)重疊 ? ? ? ? ? if(this.ll>this.clienw-30){ ? ? ? ? ? ? this.ll=this.clienw-30 ? ? ? ? ? } ? ? ? ? ? ? this.line.style.left=this.indX+'px' ? ? ? ? ? this.rcircle.style.left=this.indX+'px' ? ? ? ? } ? ? ? } ? ? ? ? ev.target.style.left = this.ll + "px"; ? ? }, ? ? fnend() { ? ? ? document.ontouchmove = null; ? ? ? document.ontouchend = null; ? ? }, ? }, }; </script>
css樣式
<style scoped lang="less"> .slider { ? height: 30px; ? width: 300px; ? background: #999; ? position: relative; ? margin: auto; ? .box { ? ? width: 30px; ? ? height: 30px; ? ? border-radius: 50%; ? ? background: pink; ? ? position: absolute; ? } ? .box.as { ? ? background: blueviolet; ? ? right: 0; ? } ? // 線條 ? .line { ? ? width: 300px; ? ? height: 5px; ? ? background: #eee; ? ? position: absolute; ? } ? .line.ac { ? ? background: rgb(247, 151, 25); ? } } </style>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue滑塊解鎖組件使用方法詳解
- vue實(shí)現(xiàn)登錄時(shí)滑塊驗(yàn)證
- Vue集成阿里云做滑塊驗(yàn)證的實(shí)踐
- Vue3+Vue-cli4項(xiàng)目中使用騰訊滑塊驗(yàn)證碼的方法
- vue實(shí)現(xiàn)簡(jiǎn)單滑塊驗(yàn)證
- 使用konva和vue-konva庫(kù)實(shí)現(xiàn)拖拽滑塊驗(yàn)證功能
- VUE接入騰訊驗(yàn)證碼功能(滑塊驗(yàn)證)備忘
- Vue 實(shí)現(xiàn)拖動(dòng)滑塊驗(yàn)證功能(只有css+js沒有后臺(tái)驗(yàn)證步驟)
- vue-awesome-swiper滑塊插件使用方法詳解
- 基于Vue.js實(shí)現(xiàn)tab滑塊效果
相關(guān)文章
vue引入elementUi后打開頁(yè)面報(bào)錯(cuò)Uncaught?TypeError的解決方式
這篇文章主要給大家介紹了關(guān)于vue引入elementUi后打開頁(yè)面報(bào)錯(cuò)Uncaught?TypeError:?Cannot?read?properties?of?undefined(reading?‘prototype‘)的解決方式,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08結(jié)合axios對(duì)項(xiàng)目中的api請(qǐng)求進(jìn)行封裝操作
這篇文章主要介紹了結(jié)合axios對(duì)項(xiàng)目中的api請(qǐng)求進(jìn)行封裝操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09vue如何實(shí)現(xiàn)對(duì)請(qǐng)求參數(shù)進(jìn)行簽名
這篇文章主要介紹了vue如何實(shí)現(xiàn)對(duì)請(qǐng)求參數(shù)進(jìn)行簽名問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01