当前位置:   article > 正文

Vue3-滑动到最右验证功能

Vue3-滑动到最右验证功能

1、思路

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

2、效果图

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

2、成分分析

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

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

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

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

3、事件分析

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

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

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

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

4、效果图

5、代码

1、SlideVerify.vue文件

  1. <template>
  2. <div
  3. ref="sliderContainer"
  4. @mousemove="onMouseMove"
  5. @mouseup="onMouseUp"
  6. class="slider-verify-container"
  7. @mouseleave="onMouseLeave"
  8. >
  9. <span v-if="blockState === 0">请按住滑块,拖动到最右边</span>
  10. <div
  11. @mousedown="onMouseDown"
  12. :style="{ left: leftP }"
  13. class="slider-verify-block"
  14. />
  15. <div :style="{ width: leftP }" class="slider-verify-complete">
  16. <span v-if="blockState === 2">验证成功</span>
  17. </div>
  18. </div>
  19. </template>
  20. <script setup>
  21. import { ref, defineEmits } from "vue";
  22. const emit = defineEmits(["success", "failed"]);
  23. const leftP = ref("0");
  24. const blockState = ref(0);
  25. const startP = ref(undefined);
  26. const sliderContainer = ref(undefined);
  27. const onMouseDown = (e) => {
  28. if (blockState.value !== 2) {
  29. leftP.value = "0";
  30. blockState.value = 1;
  31. startP.value = {
  32. clientX: e.clientX,
  33. };
  34. } else {
  35. leftP.value = "0";
  36. blockState.value = 0;
  37. }
  38. };
  39. const onMouseMove = (e) => {
  40. if (blockState.value === 1) {
  41. let width = e.clientX - startP.value.clientX;
  42. if (width + 56 > 400) {
  43. leftP.value = 400 - 56 + "px";
  44. blockState.value = 2;
  45. emit("success");
  46. } else {
  47. leftP.value = width + "px";
  48. }
  49. }
  50. };
  51. const onMouseUp = (e) => {
  52. if (blockState.value !== 2) {
  53. leftP.value = "0";
  54. blockState.value = 0;
  55. emit("failed");
  56. }
  57. };
  58. const onMouseLeave = (e) => {
  59. if (blockState.value !== 2) {
  60. leftP.value = "0";
  61. blockState.value = 0;
  62. emit("failed");
  63. }
  64. };
  65. </script>
  66. <style lang="scss" scoped>
  67. .slider-verify-container {
  68. width: 100%;
  69. height: 56px;
  70. background-color: transparent;
  71. position: relative;
  72. border: solid 1px #20cccf;
  73. text-align: center;
  74. color: #20cccf;
  75. line-height: 56px;
  76. user-select: none;
  77. -moz-user-select: none;
  78. -ms-user-select: none;
  79. -webkit-user-select: none;
  80. }
  81. .slider-verify-complete {
  82. width: 0;
  83. height: 56px;
  84. position: absolute;
  85. background-color: #00e6f14f;
  86. left: 0;
  87. top: 0;
  88. }
  89. .slider-verify-block {
  90. width: 56px;
  91. height: 56px;
  92. background-image: url("@/assets/images/login6/arrow.png");
  93. background-color: white;
  94. position: absolute;
  95. left: 0;
  96. top: -1px;
  97. border: solid 1px #20cccf;
  98. background-size: 50%;
  99. background-repeat: no-repeat;
  100. background-position: center;
  101. }
  102. </style>

2、调用代码

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/737840
推荐阅读
相关标签
  

闽ICP备14008679号