前端实现滑动解锁

上代码:

<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; } 

#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>

以上代码适配移动端和pc端,

可以根据自己的需要添加到页面,可以用mixin混入的方式引入,可以将整个mounted中的方法封装在一个方法内,这样不会显的乱,看自己情况引入使用就行,

style中下面的样式,是放置整体拖动

//  使用全局样式样式去掉
* { touch-action: pan-y; } 

原文地址:https://www.cnblogs.com/fqh123/p/11335434.html

时间: 2024-08-06 19:57:44

前端实现滑动解锁的相关文章

手机滑动解锁代码

#region==滑动解锁部分== private bool mousedown;//定义鼠标点击的bool值 private int curx;//定义鼠标点击时的位置X坐标 private void button11_MouseDown(object sender, MouseEventArgs e) { mousedown = true; curx = Cursor.Position.X;//获取点击时的X坐标 } private void button11_MouseUp(object

Swift: 打造滑动解锁文字动画

最近木事,找出来玩了玩facebook的paper.到处都是那个"slide to unlock your phone"的效果啊.忽闪忽闪的小有点炫酷的感觉.于是准备研究一下.木有想到的是居然可以用CAGradientLayer和一个小小的动画就可以实现这个效果."滑动解锁"的效果: 当然啦,首先你需要显示出这个"滑动解锁"的文本.这里咱们就用一个简单的UILabel来解决这个问题. var textExampleLabel: UILabel!

hihoCoder #1054 滑动解锁

#1054 : 滑动解锁 Time Limit:2000ms Case Time Limit:1000ms Memory Limit:256MB Description 滑动解锁是智能手机一项常用的功能.你需要在3x3的点阵上,从任意一个点开始,反复移动到一个尚未经过的"相邻"的点.这些划过的点所组成的有向折线,如果与预设的折线在图案.方向上都一致,那么手机将解锁.两个点相邻当且仅当以这两个点为端点的线段上不存在尚未经过的点.此外,这条折线还需要至少经过4个点. 为了描述方便,我们给这

hiho_1054_滑动解锁

题目大意 智能手机九点屏幕滑动解锁,如果给出某些连接线段,求出经过所有给出线段的合法的滑动解锁手势的总数.题目链接: 滑动解锁 题目分析 首先,尝试求解没有给定线段情况下,所有合法的路径的总数.可以使用dfs进行搜索.代码如下: void dfs(int row, int col, int cur_len) { visited[row][col] = true; if (cur_len >= 4) { //到达该点时,走过的路径长度大于等于4,则为合法的一个解锁手势 total_count++;

Selenium模拟JQuery滑动解锁

滑动解锁一直做UI自动化的难点之一,我补一篇滑动解锁的例子,希望能给初做Web UI自动化测试的同学一些思路. 首先先看个例子. https://www.helloweba.com/demo/2017/unlock/ 当我手动点击滑块时,改变的只是样式: 1.slide-to-unlock-handle 表示滑块,滑块的左边距在变大(因为它在向右移动嘛!) 2.Slide-tounlock-progress 表示滑过之后的背景黄色,黄色的宽度在增加,因为滑动经过的地方都变黄了. 除些之外,没其它

jQuery滑动解锁

unlock.js是一款jQuery滑动解锁插件.目前很多网站在用户登录和注册时,为防止恶意攻击,采用来滑动解锁的验证方式.用户需要滑动指定的滑块到指定位置,才能通过验证.unlock.js可以实现这种滑动解锁功能. 查看演示 下载源码 unlock.js插件具有以下特点: 滑动解锁. 尺寸.颜色.字体大小等都可以个性化定制. 完成解锁后会有回调函数,用来触发进一步的数据处理. 如何使用 1. 首先在页面中引入unlock.css和unlock.js文件. <link href="css/

hihocoder#1054 : 滑动解锁(深度优先搜索)

描述 滑动解锁是智能手机一项常用的功能.你需要在3x3的点阵上,从任意一个点开始,反复移动到一个尚未经过的"相邻"的点.这些划过的点所组成的有向折线,如果与预设的折线在图案.方向上都一致,那么手机将解锁.两个点相邻当且仅当以这两个点为端点的线段上不存在尚未经过的点.此外,这条折线还需要至少经过4个点. 为了描述方便,我们给这9个点从上到下.从左到右依次编号1-9.那么1->2->3是不合法的,因为长度不足.1->3->2->4也是合不法的,因为1->

iOS滑动解锁/滑动获取验证码效果实现

最近短信服务商要求公司的app在获取短信验证码时加上校验码,目前比较流行的是采用类似滑动解锁的方式,我们公司采取的就是这种方式,设计图如下所示: 这里校验内部的处理逻辑不作介绍,主要分享一下界面效果的实现, 下面贴出代码: 先子类化UISlider #import <UIKit/UIKit.h> #define SliderWidth 240 #define SliderHeight 40 #define SliderLabelTextColor [UIColor colorWithRed:1

Android通过指令模拟touch滑动解锁

 手机上有很多输入设备,电源键.音量加减键.触屏等等.这些设备的编号会因每个手机不同而不同,因此我们需要首先了解这个手机上都有哪些输入设备,然后通过脚本操作这些输入设备,实现一些想要的操作,进而通过循环实现自动化测试. $ adb shell cat /proc/bus/input/devices 这条命令就是显示系统的输入设备列表,自己需要去了解这些设备的含义,进而去操作这些设备!我们这里需要操作的是touch,从下图中可以看到touch的事件是event1. 下面我们以屏幕触摸输入为例.