SliderCaptcha.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <template>
  2. <div class="slider-captcha" :class="statusClass">
  3. <div
  4. class="slider-track"
  5. ref="track"
  6. @mousedown.prevent="onDragStart"
  7. @touchstart.prevent="onDragStart"
  8. >
  9. <div class="slider-fill" :style="{ width: fillPercent + '%' }"></div>
  10. <div
  11. class="slider-thumb"
  12. :style="{ left: thumbLeft + 'px' }"
  13. :class="{ 'is-dragging': dragging }"
  14. >
  15. <i class="el-icon-d-arrow-right"></i>
  16. </div>
  17. </div>
  18. <div class="captcha-hint">
  19. <template v-if="status === 'pending'">
  20. <span class="hint-arrow">»</span>
  21. <span>请按住滑块拖动到最右侧完成验证</span>
  22. </template>
  23. <template v-else-if="status === 'success'">
  24. <i class="el-icon-success" style="color:#4CAF50"></i>
  25. <span style="color:#4CAF50">验证通过</span>
  26. </template>
  27. <template v-else-if="status === 'fail'">
  28. <i class="el-icon-error" style="color:#EF5350"></i>
  29. <span style="color:#EF5350">验证失败,请重试</span>
  30. </template>
  31. </div>
  32. </div>
  33. </template>
  34. <script>
  35. export default {
  36. name: 'SliderCaptcha',
  37. data() {
  38. return {
  39. trackWidth: 0,
  40. currentOffset: 0,
  41. dragging: false,
  42. status: 'pending' // pending | dragging | success | fail
  43. }
  44. },
  45. computed: {
  46. fillPercent() {
  47. if (this.trackWidth === 0) return 0
  48. return (this.currentOffset / this.trackWidth) * 100
  49. },
  50. thumbLeft() { return this.currentOffset },
  51. statusClass() {
  52. return {
  53. 'is-success': this.status === 'success',
  54. 'is-fail': this.status === 'fail',
  55. 'is-dragging': this.status === 'dragging'
  56. }
  57. }
  58. },
  59. mounted() {
  60. this.$nextTick(this.initCaptcha)
  61. },
  62. methods: {
  63. initCaptcha() {
  64. if (!this.$refs.track) return
  65. this.trackWidth = this.$refs.track.offsetWidth
  66. this.currentOffset = 0
  67. this.status = 'pending'
  68. },
  69. onDragStart(e) {
  70. if (this.status === 'success') return
  71. this.dragging = true
  72. this.status = 'dragging'
  73. var startX = e.clientX || (e.touches && e.touches[0].clientX)
  74. var startOffset = this.currentOffset
  75. var self = this
  76. var onMove = function(ev) {
  77. var clientX = ev.clientX || (ev.touches && ev.touches[0].clientX)
  78. if (clientX === undefined) return
  79. var newOffset = startOffset + (clientX - startX)
  80. if (newOffset < 0) newOffset = 0
  81. if (newOffset > self.trackWidth) newOffset = self.trackWidth
  82. self.currentOffset = newOffset
  83. }
  84. var onEnd = function() {
  85. document.removeEventListener('mousemove', onMove)
  86. document.removeEventListener('mouseup', onEnd)
  87. document.removeEventListener('touchmove', onMove)
  88. document.removeEventListener('touchend', onEnd)
  89. self.dragging = false
  90. self.onDragEnd()
  91. }
  92. document.addEventListener('mousemove', onMove)
  93. document.addEventListener('mouseup', onEnd)
  94. document.addEventListener('touchmove', onMove, { passive: true })
  95. document.addEventListener('touchend', onEnd)
  96. },
  97. onDragEnd() {
  98. if (this.currentOffset >= this.trackWidth * 0.88) {
  99. this.status = 'success'
  100. this.currentOffset = this.trackWidth
  101. this.$emit('success')
  102. } else {
  103. this.status = 'fail'
  104. this.$emit('fail')
  105. var self = this
  106. setTimeout(function() { self.initCaptcha() }, 1000)
  107. }
  108. },
  109. reset() {
  110. this.initCaptcha()
  111. }
  112. }
  113. }
  114. </script>
  115. <style scoped>
  116. .slider-captcha {
  117. user-select: none;
  118. -webkit-user-select: none;
  119. }
  120. .slider-track {
  121. position: relative;
  122. height: 40px;
  123. background: #EDF2F7;
  124. border-radius: 20px;
  125. cursor: pointer;
  126. overflow: hidden;
  127. border: 1px solid #E2E8F0;
  128. transition: border-color 0.3s;
  129. }
  130. .slider-track:hover { border-color: #5B9BD5; }
  131. .slider-fill {
  132. position: absolute;
  133. left: 0;
  134. top: 0;
  135. height: 100%;
  136. background: linear-gradient(90deg, #8FC5E8, #5B9BD5);
  137. border-radius: 20px;
  138. pointer-events: none;
  139. }
  140. .slider-thumb {
  141. position: absolute;
  142. top: -2px;
  143. width: 44px;
  144. height: 44px;
  145. background: linear-gradient(135deg, #5B9BD5, #3A7CC4);
  146. border-radius: 50%;
  147. display: flex;
  148. align-items: center;
  149. justify-content: center;
  150. cursor: grab;
  151. box-shadow: 0 2px 8px rgba(91, 155, 213, 0.4);
  152. z-index: 2;
  153. margin-left: -22px;
  154. transition: box-shadow 0.2s;
  155. }
  156. .slider-thumb i {
  157. color: #fff;
  158. font-size: 18px;
  159. font-weight: bold;
  160. }
  161. .slider-thumb:hover { box-shadow: 0 3px 12px rgba(91, 155, 213, 0.6); }
  162. .slider-thumb.is-dragging {
  163. cursor: grabbing;
  164. box-shadow: 0 3px 16px rgba(91, 155, 213, 0.7);
  165. }
  166. .captcha-hint {
  167. display: flex;
  168. align-items: center;
  169. gap: 6px;
  170. font-size: 13px;
  171. color: #909399;
  172. margin-top: 8px;
  173. justify-content: center;
  174. }
  175. .hint-arrow {
  176. color: #5B9BD5;
  177. font-weight: bold;
  178. animation: hintPulse 1.6s ease-in-out infinite;
  179. }
  180. @keyframes hintPulse {
  181. 0%, 100% { opacity: 0.3; transform: translateX(0); }
  182. 50% { opacity: 1; transform: translateX(4px); }
  183. }
  184. .is-success .slider-track { border-color: #4CAF50; }
  185. .is-success .slider-fill { background: linear-gradient(90deg, #81C784, #4CAF50); }
  186. .is-success .slider-thumb {
  187. background: linear-gradient(135deg, #81C784, #4CAF50);
  188. box-shadow: 0 2px 8px rgba(76, 175, 80, 0.4);
  189. cursor: default;
  190. }
  191. .is-success .slider-thumb i { display: none; }
  192. .is-success .slider-thumb::before {
  193. content: '✓';
  194. color: #fff;
  195. font-size: 20px;
  196. font-weight: bold;
  197. position: absolute;
  198. }
  199. .is-fail .slider-track { border-color: #EF5350; }
  200. .is-fail .slider-fill { background: linear-gradient(90deg, #E57373, #EF5350); }
  201. .is-fail .slider-thumb {
  202. background: linear-gradient(135deg, #E57373, #EF5350);
  203. box-shadow: 0 2px 8px rgba(239, 83, 80, 0.4);
  204. }
  205. </style>