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 的 v-model用法实例
Nov 23 Vue.js
vue element实现表格合并行数据
Nov 30 Vue.js
Vue router安装及使用方法解析
Dec 02 Vue.js
vue 动态生成拓扑图的示例
Jan 03 Vue.js
vue-router路由懒加载及实现的3种方式
Feb 28 Vue.js
详解vue3中组件的非兼容变更
Mar 03 Vue.js
vue打开新窗口并实现传参的图文实例
Mar 04 Vue.js
Vue Element UI自定义描述列表组件
May 18 Vue.js
vue+springboot实现登录验证码
May 27 Vue.js
vue3使用vuedraggable实现拖拽功能
Apr 06 Vue.js
vue组件冲突之引用另一个组件出现组件不显示的问题
Apr 13 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
这部番真是良心,画质好到像风景区,剧情让人跟着小公会热血沸腾
2020/03/10 日漫
雄兵连:第三季确定会出,不过时间未定,鹤熙是第三季的主角!
2020/03/13 国漫
PHP 5.0对象模型深度探索之绑定
2006/09/05 PHP
探讨php中header的用法详解
2013/06/07 PHP
php预定义变量使用帮助(带实例)
2013/10/30 PHP
thinkphp使用literal防止模板标签被解析的方法
2014/11/22 PHP
PHP弱类型的安全问题详细总结
2016/09/25 PHP
php大小写转换函数(strtolower、strtoupper)用法介绍
2017/11/17 PHP
gearman中任务的优先级和返回状态实例分析
2020/02/27 PHP
强制设为首页代码
2006/06/19 Javascript
jQuery 使用手册(二)
2009/09/23 Javascript
如何在一个页面显示多个百度地图
2013/04/07 Javascript
Java/JS获取flash高宽的具体方法
2013/12/27 Javascript
JQuery自适应窗口大小导航菜单附源码下载
2015/09/01 Javascript
jquery插件jquery.confirm弹出确认消息
2015/12/22 Javascript
javascript 初学教程及五子棋小程序的简单实现
2017/07/04 Javascript
前端开发不得不知的10个最佳ES6特性
2017/08/30 Javascript
jquery实现Ajax请求的几种常见方式总结
2019/05/28 jQuery
vue中的v-if和v-show的区别详解
2019/09/01 Javascript
[02:10]2018DOTA2亚洲邀请赛赛前采访-Liquid
2018/04/03 DOTA
[54:33]2018DOTA2亚洲邀请赛小组赛 A组加赛 Liquid vs Optic
2018/04/03 DOTA
[00:38]TI珍贵瞬间系列(二):笑
2020/08/26 DOTA
python编写分类决策树的代码
2017/12/21 Python
Python3实现建造者模式的示例代码
2020/06/28 Python
使用html2canvas将页面转成图并使用用canvas2image下载
2019/04/04 HTML / CSS
Mixbook加拿大:照片书,照片卡,剪贴簿,年历和日历
2017/02/21 全球购物
澳大利亚香水在线:Price Rite Mart
2017/12/28 全球购物
捷克家电和家具购物网站:OKAY.cz
2020/07/23 全球购物
偷看我的初中毕业鉴定
2014/01/29 职场文书
岗位职责说明书
2014/05/07 职场文书
质量承诺书格式
2014/05/20 职场文书
学会感恩主题班会
2015/08/12 职场文书
Nginx 根据URL带的参数转发的实现
2021/04/01 Servers
使用Python的开发框架Brownie部署以太坊智能合约
2021/05/28 Python
vue项目打包后路由错误的解决方法
2022/04/13 Vue.js
详解flex:1什么意思
2022/07/23 HTML / CSS