vue實(shí)現(xiàn)滑動(dòng)解鎖功能
更新時(shí)間:2022年03月03日 12:06:22 作者:Archer_yy
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)滑動(dòng)解鎖功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了vue實(shí)現(xiàn)滑動(dòng)解鎖功能的具體代碼,供大家參考,具體內(nèi)容如下
話不多說,直接上代碼;
<template> ? <div> ? ? <div id="box"> ? ? ? <div class="bgColor"></div> ? ? ? <div class="txt">滑動(dòng)解鎖</div> ? ? ? <!--給i標(biāo)簽添加上相應(yīng)字體圖標(biāo)的類名即可--> ? ? ? <div class="slider"> ? ? ? ? <i v-show="!isSuccess" class="el-icon-d-arrow-right"></i> ? ? ? ? <i v-show="isSuccess" class="el-icon-check"></i> ? ? ? </div> ? ? </div> ? </div> </template> <script> export default { ? mounted() { ? ? var self = this; ? ? //一、定義了一個(gè)獲取元素的方法 ? ? function getEle(selector) { ? ? ? return document.querySelector(selector); ? ? } ? ? //二、獲取到需要用到的DOM元素 ? ? var box = getEle("#box"), //容器 ? ? ? bgColor = getEle(".bgColor"), //背景色 ? ? ? txt = getEle(".txt"), //文本 ? ? ? slider = getEle(".slider"), //滑塊 ? ? ? icon = getEle(".slider>i"), ? ? ? successMoveDistance = box.offsetWidth - slider.offsetWidth, //解鎖需要滑動(dòng)的距離 ? ? ? downX; //用于存放鼠標(biāo)按下時(shí)的位置 ? ? //三、給滑塊添加鼠標(biāo)按下事件 ? ? slider.onmousedown = mousedownHandler; ? ? slider.ontouchstart = mousedownHandler; //移動(dòng)端加touchstart事件 ? ? //3.1鼠標(biāo)按下事件的方法實(shí)現(xiàn) ? ? function mousedownHandler(e) { ? ? ? bgColor.style.transition = ""; ? ? ? slider.style.transition = ""; ? ? ? var e = e || window.event || e.which; ? ? ? downX = e.clientX ? e.clientX : e.changedTouches[0].clientX; ? ? ? if (!self.isSuccess) { ? ? ? ? //在鼠標(biāo)按下時(shí),分別給鼠標(biāo)添加移動(dòng)和松開事件 ? ? ? ? document.onmousemove = mousemoveHandler; ? ? ? ? document.onmouseup = mouseupHandler; ? ? ? ? //添加移動(dòng)端對應(yīng)事件 ? ? ? ? document.ontouchmove = mousemoveHandler; ? ? ? ? document.ontouchend = mouseupHandler; ? ? ? } ? ? } ? ? //四、定義一個(gè)獲取鼠標(biāo)當(dāng)前需要移動(dòng)多少距離的方法 ? ? function getOffsetX(offset, min, max) { ? ? ? if (offset < min) { ? ? ? ? offset = min; ? ? ? } else if (offset > max) { ? ? ? ? offset = max; ? ? ? } ? ? ? return offset; ? ? } ? ? //3.1.1鼠標(biāo)移動(dòng)事件的方法實(shí)現(xiàn) ? ? function mousemoveHandler(e) { ? ? ? var e = e || window.event || e.which; ? ? ? var moveX = e.clientX ? e.clientX : e.changedTouches[0].clientX; ? ? ? console.log(moveX); ? ? ? var offsetX = getOffsetX(moveX - downX, 0, successMoveDistance); ? ? ? bgColor.style.width = offsetX + "px"; ? ? ? slider.style.left = offsetX + "px"; ? ? ? if (offsetX == successMoveDistance) { ? ? ? ? success(); ? ? ? } ? ? ? //如果不設(shè)置滑塊滑動(dòng)時(shí)會(huì)出現(xiàn)問題(目前還不知道為什么) ? ? ? e.preventDefault(); ? ? } ? ? //3.1.2鼠標(biāo)松開事件的方法實(shí)現(xiàn) ? ? function mouseupHandler(e) { ? ? ? if (!self.isSuccess) { ? ? ? ? bgColor.style.width = 0 + "px"; ? ? ? ? slider.style.left = 0 + "px"; ? ? ? ? bgColor.style.transition = "width 0.5s linear"; ? ? ? ? slider.style.transition = "left 0.5s linear"; ? ? ? } ? ? ? document.onmousemove = null; ? ? ? document.onmouseup = null; ? ? ? //移除移動(dòng)端事件 ? ? ? document.ontouchmove = null; ? ? ? document.ontouchend = null; ? ? } ? ? //五、定義一個(gè)滑塊解鎖成功的方法 ? ? function success() { ? ? ? self.isSuccess = true; ? ? ? txt.innerHTML = "解鎖成功"; ? ? ? bgColor.style.backgroundColor = "lightgreen"; ? ? ? //滑動(dòng)成功時(shí),移除鼠標(biāo)按下事件和鼠標(biāo)移動(dòng)事件 ? ? ? slider.onmousedown = null; ? ? ? document.onmousemove = null; ? ? ? //移除移動(dòng)端事件 ? ? ? document.ontouchstart = null; ? ? ? document.ontouchmove = null; ? ? } ? }, ? data() { ? ? return { ? ? ? isSuccess: false, ? ? }; ? }, }; </script> <style> /* ?使用全局樣式樣式去掉 */ * { touch-action: pan-y; }? </style> <style> #box { ? position: relative; ? width: 300px; ? height: 40px; ? margin: 0 auto; ? margin-top: 10px; ? background-color: #e8e8e8; ? box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2); } .bgColor { ? position: absolute; ? left: 0; ? top: 0; ? width: 40px; ? height: 40px; ? background-color: lightblue; } .txt { ? position: absolute; ? width: 100%; ? height: 40px; ? line-height: 40px; ? font-size: 14px; ? color: #000; ? text-align: center; } .slider { ? position: absolute; ? left: 0; ? top: 0; ? width: 50px; ? height: 40px; ? /* border: 1px solid #ccc; */ ? background: #fff; ? text-align: center; ? cursor: move; } .slider > i { ? position: absolute; ? top: 50%; ? left: 50%; ? transform: translate(-50%, -50%); } </style>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue keep-alive 動(dòng)態(tài)刪除組件緩存的例子
今天小編就為大家分享一篇vue keep-alive 動(dòng)態(tài)刪除組件緩存的例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11詳解如何制作并發(fā)布一個(gè)vue的組件的npm包
這篇文章主要介紹了詳解如何制作并發(fā)布一個(gè)vue的組件的npm包,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11Vue自定義指令上報(bào)Google Analytics事件統(tǒng)計(jì)的方法
我們經(jīng)常需要接入統(tǒng)計(jì)服務(wù)以方便運(yùn)營,這篇文章主要介紹了Vue自定義指令上報(bào)Google Analytics事件統(tǒng)計(jì)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02