Vue3-滑动到最右验证功能

1、思路

1、在登录页面需要启动向右滑块验证

2、效果图

3、文章地址:滑动验证码的实现-vue-simple-verify

2、成分分析

1、由三块构成,分别是底部条、拖动条、拖动移动部分

2、底部条:整体容器,包括背景、边框和文字(请按住滑块,拖动到最右边)

3、拖动条:图片+边框+背景色即可

4、完成部分:背景、边框和文字(验证完成)

3、事件分析

1、鼠标按下事件:记录鼠标位置以及状态

2、鼠标移动事件:计算滑块位置

3、鼠标离开事件:更新状态至初始状态

4、鼠标抬起事件:更新状态至初始状态

4、效果图

5、代码

1、SlideVerify.vue文件

<template>
  <div
    ref="sliderContainer"
    @mousemove="onMouseMove"
    @mouseup="onMouseUp"
    class="slider-verify-container"
    @mouseleave="onMouseLeave"
  >
    <span v-if="blockState === 0">请按住滑块,拖动到最右边</span>
    <div
      @mousedown="onMouseDown"
      :style="{ left: leftP }"
      class="slider-verify-block"
    />
    <div :style="{ width: leftP }" class="slider-verify-complete">
      <span v-if="blockState === 2">验证成功</span>
    </div>
  </div>
</template>

<script setup>
import { ref, defineEmits } from "vue";

const emit = defineEmits(["success", "failed"]);

const leftP = ref("0");

const blockState = ref(0);

const startP = ref(undefined);

const sliderContainer = ref(undefined);
const onMouseDown = (e) => {
  if (blockState.value !== 2) {
    leftP.value = "0";
    blockState.value = 1;
    startP.value = {
      clientX: e.clientX,
    };
  } else {
    leftP.value = "0";
    blockState.value = 0;
  }
};
const onMouseMove = (e) => {
  if (blockState.value === 1) {
    let width = e.clientX - startP.value.clientX;
    if (width + 56 > 400) {
      leftP.value = 400 - 56 + "px";
      blockState.value = 2;
      emit("success");
    } else {
      leftP.value = width + "px";
    }
  }
};
const onMouseUp = (e) => {
  if (blockState.value !== 2) {
    leftP.value = "0";
    blockState.value = 0;
    emit("failed");
  }
};
const onMouseLeave = (e) => {
  if (blockState.value !== 2) {
    leftP.value = "0";
    blockState.value = 0;
    emit("failed");
  }
};
</script>

<style lang="scss" scoped>
.slider-verify-container {
  width: 100%;
  height: 56px;
  background-color: transparent;
  position: relative;
  border: solid 1px #20cccf;
  text-align: center;
  color: #20cccf;
  line-height: 56px;
  user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  -webkit-user-select: none;
}
.slider-verify-complete {
  width: 0;
  height: 56px;
  position: absolute;
  background-color: #00e6f14f;
  left: 0;
  top: 0;
}
.slider-verify-block {
  width: 56px;
  height: 56px;
  background-image: url("@/assets/images/login6/arrow.png");
  background-color: white;
  position: absolute;
  left: 0;
  top: -1px;
  border: solid 1px #20cccf;
  background-size: 50%;
  background-repeat: no-repeat;
  background-position: center;
}
</style>

2、调用代码

<slide-verify @success="onVerifySuccess" @failed="onVerifyFailed" />

相关推荐

  1. 实现抖音视频滑动功能vue3+swiper

    2024-06-14 11:38:03       11 阅读
  2. vue router 左过渡动画

    2024-06-14 11:38:03       36 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-14 11:38:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-14 11:38:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-14 11:38:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-14 11:38:03       20 阅读

热门阅读

  1. apex触发器满足条件时弹出对话框

    2024-06-14 11:38:03       5 阅读
  2. TCP标志

    2024-06-14 11:38:03       9 阅读
  3. ISBN查询图书api接口

    2024-06-14 11:38:03       8 阅读
  4. XXE漏洞详解:从基础到防御

    2024-06-14 11:38:03       8 阅读
  5. Python爬虫获取古诗文网中的古诗文

    2024-06-14 11:38:03       7 阅读
  6. vsto excel 快速查找所有标黄的格子

    2024-06-14 11:38:03       8 阅读
  7. C++面向对象程序设计之类与对象(1)

    2024-06-14 11:38:03       9 阅读
  8. SQL 入门教程

    2024-06-14 11:38:03       6 阅读
  9. Go基础编程 - 07 - 字典(map)及其约束

    2024-06-14 11:38:03       9 阅读
  10. unity2d Ugui--Image城市道路汽车行驶

    2024-06-14 11:38:03       7 阅读