move-verify.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <template>
  2. <view :data-theme="theme">
  3. <view class="move-path bg-color" @touchend="handleOnTouchEnd" @touchstart="start">
  4. <view class="move-content">
  5. <view v-if="vertify" class="success">{{this.tipWords}}</view>
  6. <view v-else>拖动滑块验证</view>
  7. </view>
  8. <movable-area :animation="true">
  9. <movable-view class="move-view" :x="x" direction="horizontal" @change="handleOnMoving" :disabled="vertify"
  10. :class="{'active':vertify}"></movable-view>
  11. </movable-area>
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. let app = getApp();
  17. export default {
  18. name: 'move-vertify',
  19. data() {
  20. return {
  21. x: 0,
  22. oldx: 0,
  23. vertify: false,
  24. size: {},
  25. isMove: false,
  26. tipWords: '', // 提示语
  27. startMoveTime: "", //移动开始的时间
  28. endMovetime: '', //移动结束的时间
  29. theme: app.globalData.theme,
  30. };
  31. },
  32. mounted() {
  33. this.init()
  34. },
  35. methods: {
  36. //鼠标按下
  37. start: function(e) {
  38. this.startMoveTime = new Date().getTime();
  39. },//开始滑动的时间
  40. //鼠标松开
  41. end: function() {
  42. this.endMovetime = new Date().getTime();
  43. },
  44. init() {
  45. this.getSize(".move-path").then((pathway) => {
  46. this.size.pathway = pathway;
  47. this.getSize(".move-view").then((track) => {
  48. this.size.track = track;
  49. });
  50. })
  51. },
  52. /**
  53. * 获取元素宽度
  54. */
  55. getSize(selector) {
  56. return new Promise((resolve, reject) => {
  57. const view = uni.createSelectorQuery().in(this).select(selector);
  58. view.fields({
  59. size: true,
  60. }, (res) => {
  61. resolve(res.width);
  62. }).exec();
  63. });
  64. },
  65. /**
  66. * 滑动过程
  67. */
  68. handleOnMoving(e) {
  69. this.oldx = e.detail.x;
  70. },
  71. /**
  72. * 滑动结束
  73. */
  74. handleOnTouchEnd() {
  75. this.endMovetime = new Date().getTime();
  76. if (this.vertify || this.oldx < 1) return;
  77. this.x = this.oldx;
  78. if ((this.oldx + 2) > (this.size.pathway - this.size.track)) this.vertify = true;
  79. else {
  80. this.$nextTick(() => {
  81. this.x = 0;
  82. this.oldx = 0;
  83. });
  84. }
  85. this.tipWords =`${((this.endMovetime-this.startMoveTime)/1000).toFixed(2)}s验证成功`;
  86. setTimeout(() => {
  87. this.$emit("vertify", this.vertify);
  88. }, 1000)
  89. },
  90. }
  91. }
  92. </script>
  93. <style scoped lang="scss">
  94. .move-path {
  95. margin: 20rpx auto;
  96. color: #333;
  97. height: 80rpx;
  98. border-radius: 80rpx;
  99. position: relative;
  100. overflow: hidden;
  101. .move-content {
  102. position: absolute;
  103. top: 0;
  104. left: 0;
  105. width: 100%;
  106. line-height: 80rpx;
  107. text-align: center;
  108. color: #fff;
  109. font-size: 32rpx;
  110. z-index: 3;
  111. .success {
  112. color: #fff;
  113. }
  114. }
  115. movable-area {
  116. position: absolute;
  117. top: 0;
  118. left: 0;
  119. height: 100%;
  120. width: 100%;
  121. background: none;
  122. z-index: 5;
  123. }
  124. movable-view {
  125. width: 120rpx;
  126. height: 80rpx;
  127. box-sizing: border-box;
  128. border-radius: 80rpx;
  129. background-color: #fff;
  130. @include coupons_border_color(theme);
  131. background-position: center;
  132. background-repeat: no-repeat;
  133. background-size: auto 32rpx;
  134. background-image: url('./arrow.png');
  135. }
  136. movable-view.active {
  137. background-size: 30% 30%;
  138. background-image: url('./success.png');
  139. }
  140. }
  141. </style>