index.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <template>
  2. <view class="slider-verify" :style="'background-color:' + sBgColor" @touchend="touchend">
  3. <view class="slider-prompt" :style="success ? 'color:' + sColor : 'color:' + dColor">{{ success ? sliderText.successText : sliderText.startText }}</view>
  4. <view class="slider-bg" :style="{ 'transform': 'translateX(' + oldx + 'px)', backgroundColor: dBgColor }"></view>
  5. <movable-area class="slider-area" :animation="true">
  6. <movable-view
  7. :style="`background-color: ${sliderText.successColor};border-color: ${success ? sBgColor : dBgColor} `"
  8. :class="{ 'movable-btn': true, 'movable-success': success }" :x="x" direction="horizontal" @change="onMove"
  9. :disabled="isDisable">
  10. {{sliderText.btnText}}
  11. </movable-view>
  12. </movable-area>
  13. </view>
  14. </template>
  15. <script>
  16. export default {
  17. props: {
  18. //是否禁止拖动
  19. disabled: {
  20. type: Boolean,
  21. default: false
  22. },
  23. //偏移量
  24. offset: {
  25. type: Number,
  26. default: 3
  27. },
  28. //滑动轨道默认背景色
  29. dBgColor: {
  30. type: String,
  31. default: '#f0f0f0'
  32. },
  33. //滑动轨道滑过背景色
  34. sBgColor: {
  35. type: String,
  36. default: '#b1d7ff'
  37. },
  38. //默认文字颜色
  39. dColor: {
  40. type: String,
  41. default: '#8a8a8a'
  42. },
  43. //成功文字颜色
  44. sColor: {
  45. type: String,
  46. default: '#FFFFFF'
  47. },
  48. sliderText: {
  49. type: Object,
  50. default: {
  51. successText:'验证通过',
  52. startText:'拖动滑块验证',
  53. successColor: '#72c13f',
  54. btnText: '开始'
  55. }
  56. },
  57. },
  58. data() {
  59. return {
  60. x: 0,
  61. oldx: 0,
  62. success: false, //是否验证成功
  63. verification: 0, //验证次数
  64. isDisable: this.disabled,
  65. screenWidth: 0
  66. };
  67. },
  68. mounted() {
  69. const systemInfo = uni.getSystemInfoSync()
  70. this.screenWidth = systemInfo.screenWidth
  71. },
  72. methods: {
  73. onMove(e) {
  74. this.oldx = e.detail.x
  75. },
  76. touchend() {
  77. if (this.success || (this.oldx < 1 && this.oldx != 0.1)) return
  78. this.x = this.oldx
  79. var promptW = 0
  80. var onTrackW = 0
  81. uni.createSelectorQuery().in(this).select(".slider-prompt").boundingClientRect(data => {
  82. if (data.width > 0) {
  83. promptW = data.width
  84. uni.createSelectorQuery().in(this).select(".movable-btn").boundingClientRect(data => {
  85. if (data.width > 0) {
  86. onTrackW = data.width
  87. if (this.oldx != 0.1) this.verification++
  88. if (this.oldx > (promptW - onTrackW - this.offset)) {
  89. this.success = true
  90. this.isDisable = true
  91. this.verificationSuccess(true)
  92. } else {
  93. this.$nextTick(() => {
  94. this.x = 0
  95. this.oldx = 0
  96. })
  97. this.verificationSuccess(false)
  98. }
  99. }
  100. }).exec()
  101. }
  102. }).exec()
  103. },
  104. verificationSuccess(state) {
  105. let obj = {
  106. state: state,
  107. verification: this.verification
  108. }
  109. this.$emit("change", obj)
  110. },
  111. //重置初始化状态
  112. initialization() {
  113. this.x = 0
  114. this.oldx = 0.1
  115. this.verification = 0
  116. this.success = false
  117. this.isDisable = false
  118. this.touchend()
  119. }
  120. }
  121. }
  122. </script>
  123. <style scoped>
  124. .slider-verify {
  125. position: relative;
  126. width: 100%;
  127. height: 100rpx;
  128. overflow: hidden;
  129. }
  130. .slider-prompt {
  131. width: 100%;
  132. height: 100%;
  133. position: absolute;
  134. left: 0;
  135. top: 0;
  136. display: flex;
  137. align-items: center;
  138. justify-content: center;
  139. font-size: 32rpx;
  140. z-index: 99;
  141. }
  142. .slider-bg {
  143. width: 100%;
  144. height: 100%;
  145. position: absolute;
  146. top: 0;
  147. left: 0;
  148. }
  149. .slider-area {
  150. position: absolute;
  151. top: 0;
  152. left: 0;
  153. height: 100%;
  154. width: 100%;
  155. z-index: 999;
  156. }
  157. .movable-btn {
  158. width: 150rpx;
  159. height: 100%;
  160. border: solid 1px;
  161. display: flex;
  162. align-items: center;
  163. justify-content: center;
  164. font-size: 17px;
  165. color: #fff;
  166. /* width: 100rpx;
  167. height: 100%;
  168. box-sizing: border-box;
  169. background-color: #FFFFFF;
  170. border: solid 1px;
  171. background-image: url(/static/img/go-right.png);
  172. background-position: center;
  173. background-size: 34rpx auto;
  174. background-repeat: no-repeat;
  175. display: flex;
  176. align-content: center;
  177. justify-content: center; */
  178. }
  179. .movable-success {
  180. /* border: solid 1px;
  181. background-image: url(/static/img/success.png);
  182. background-size: 40rpx auto; */
  183. }
  184. </style>