vue实现滑动解锁功能


Posted in Vue.js onMarch 03, 2022

本文实例为大家分享了vue实现滑动解锁功能的具体代码,供大家参考,具体内容如下

vue实现滑动解锁功能

vue实现滑动解锁功能

vue实现滑动解锁功能

话不多说,直接上代码;

<template>
  <div>
    <div id="box">
      <div class="bgColor"></div>
      <div class="txt">滑动解锁</div>
      <!--给i标签添加上相应字体图标的类名即可-->
      <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;
    //一、定义了一个获取元素的方法
    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, //解锁需要滑动的距离
      downX; //用于存放鼠标按下时的位置
    //三、给滑块添加鼠标按下事件
    slider.onmousedown = mousedownHandler;
    slider.ontouchstart = mousedownHandler; //移动端加touchstart事件
    //3.1鼠标按下事件的方法实现
    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) {
        //在鼠标按下时,分别给鼠标添加移动和松开事件
        document.onmousemove = mousemoveHandler;
        document.onmouseup = mouseupHandler;
        //添加移动端对应事件
        document.ontouchmove = mousemoveHandler;
        document.ontouchend = mouseupHandler;
      }
    }
    //四、定义一个获取鼠标当前需要移动多少距离的方法
    function getOffsetX(offset, min, max) {
      if (offset < min) {
        offset = min;
      } else if (offset > max) {
        offset = max;
      }
      return offset;
    }
    //3.1.1鼠标移动事件的方法实现
    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();
      }
      //如果不设置滑块滑动时会出现问题(目前还不知道为什么)
      e.preventDefault();
    }
    //3.1.2鼠标松开事件的方法实现
    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;
      //移除移动端事件
      document.ontouchmove = null;
      document.ontouchend = null;
    }
    //五、定义一个滑块解锁成功的方法
    function success() {
      self.isSuccess = true;
      txt.innerHTML = "解锁成功";
      bgColor.style.backgroundColor = "lightgreen";
      //滑动成功时,移除鼠标按下事件和鼠标移动事件
      slider.onmousedown = null;
      document.onmousemove = null;
      //移除移动端事件
      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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Vue.js 相关文章推荐
Vue.js桌面端自定义滚动条组件之美化滚动条VScroll
Dec 01 Vue.js
Vue-router中hash模式与history模式的区别详解
Dec 15 Vue.js
vue组件是如何解析及渲染的?
Jan 13 Vue.js
vue 递归组件的简单使用示例
Jan 14 Vue.js
vue 中this.$set 动态绑定数据的案例讲解
Jan 29 Vue.js
Vite和Vue CLI的优劣
Jan 30 Vue.js
vue路由实现登录拦截
Mar 24 Vue.js
如何使用vue3打造一个物料库
May 08 Vue.js
vue+springboot实现登录验证码
May 27 Vue.js
Vue-Element-Admin集成自己的接口实现登录跳转
Jun 23 Vue.js
Vue中Object.assign清空数据报错的解决方案
Mar 03 Vue.js
vue组件vue-esign实现电子签名
Apr 21 Vue.js
Vue全局事件总线你了解吗
Feb 24 #Vue.js
Vue的生命周期一起来看看
Vue的过滤器你真了解吗
Feb 24 #Vue.js
Vue监视数据的原理详解
Feb 24 #Vue.js
Vue的列表之渲染,排序,过滤详解
Vue3如何理解ref toRef和toRefs的区别
Feb 18 #Vue.js
Vue h函数的使用详解
Feb 18 #Vue.js
You might like
DOTA2 1月28日更新:监管系统降临刀塔世界
2021/01/28 DOTA
推荐5款跨平台的PHP编辑器
2014/12/25 PHP
PHP中使用xmlreader读取xml数据示例
2014/12/29 PHP
php实现excel中rank函数功能的方法
2015/01/20 PHP
php实现RSA加密类实例
2015/03/26 PHP
CodeIgniter分页类pagination使用方法示例
2016/03/28 PHP
基于PHP实现短信验证码接口(容联运通讯)
2016/09/06 PHP
利用PHP抓取百度阅读的方法示例
2016/12/18 PHP
php实现微信扫码支付
2017/03/26 PHP
php数据序列化测试实例详解
2017/08/12 PHP
PHP迭代与递归实现无限级分类
2017/08/28 PHP
PHP中md5()函数的用法讲解
2019/03/30 PHP
用JavaScript页面不刷新时全选择,全删除(GridView)
2009/04/14 Javascript
javascript简易缓动插件(源码打包)
2012/02/16 Javascript
Ajax异步提交表单数据的说明及方法实例
2013/06/22 Javascript
jquery next nextAll nextUntil siblings的区别介绍
2013/10/05 Javascript
nodejs实现遍历文件夹并统计文件大小
2015/05/28 NodeJs
盘点javascript 正则表达式中 中括号的【坑】
2016/03/16 Javascript
Web制作验证码功能实例代码
2017/06/19 Javascript
在vue中实现嵌套页面(iframe)
2020/07/30 Javascript
[42:11]TNC vs Pain 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/20 DOTA
python实现倒计时的示例
2014/02/14 Python
Python中文件I/O高效操作处理的技巧分享
2017/02/04 Python
Python入门_浅谈for循环、while循环
2017/05/16 Python
Python实现自动登录百度空间的方法
2017/06/10 Python
Python控制键盘鼠标pynput的详细用法
2019/01/28 Python
python3 selenium自动化 frame表单嵌套的切换方法
2019/08/23 Python
Python logging模块写入中文出现乱码
2020/05/21 Python
深入浅析pycharm中 Make available to all projects的含义
2020/09/15 Python
美味咖啡的顶级烘焙师:Cafe Britt
2018/03/15 全球购物
自主招生自荐信范文
2013/12/04 职场文书
初中三好学生事迹材料
2014/01/13 职场文书
个人反四风对照检查材料思想汇报
2014/09/23 职场文书
解决golang post文件时Content-Type出现的问题
2021/05/02 Golang
Python数据类型最全知识总结
2021/05/31 Python
一文搞懂MySQL索引页结构
2022/02/28 MySQL