电速宝
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

index.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. Component({
  2. properties: {
  3. width: {
  4. type: Number,
  5. value: 300
  6. },
  7. height: {
  8. type: Number,
  9. value: 50
  10. },
  11. blockSize: {
  12. type: Number,
  13. value: 48
  14. },
  15. defaultText: {
  16. type: String,
  17. value: "向右滑动完成验证"
  18. },
  19. successText: {
  20. type: String,
  21. value: "验证通过"
  22. },
  23. iconSize: {
  24. type: Number,
  25. value: 24
  26. }
  27. },
  28. data: {
  29. position: 0, // 滑块位置
  30. progressWidth: 0, // 已滑动宽度
  31. startX: 0, // 触摸起始X坐标
  32. isMoving: false, // 是否正在滑动
  33. isSuccess: false // 是否验证成功
  34. },
  35. methods: {
  36. // 触摸开始
  37. onTouchStart(e) {
  38. if (this.data.isSuccess) return;
  39. this.setData({
  40. startX: e.touches[0].clientX,
  41. isMoving: true
  42. });
  43. },
  44. // 触摸移动
  45. onTouchMove(e) {
  46. if (!this.data.isMoving || this.data.isSuccess) return;
  47. const moveX = e.touches[0].clientX;
  48. let position = moveX - this.data.startX;
  49. // 限制滑块范围
  50. if (position < 0) position = 0;
  51. const maxPosition = this.data.width - this.data.blockSize;
  52. if (position > maxPosition) position = maxPosition;
  53. this.setData({
  54. position,
  55. progressWidth: position + this.data.blockSize // 进度宽度 = 滑块位置 + 滑块宽度
  56. });
  57. },
  58. // 触摸结束
  59. onTouchEnd() {
  60. if (!this.data.isMoving || this.data.isSuccess) return;
  61. this.setData({ isMoving: false });
  62. // 判断是否滑动到终点
  63. const maxPosition = this.data.width - this.data.blockSize;
  64. if (this.data.position >= maxPosition * 0.95) {
  65. // 验证成功
  66. this.setData({
  67. position: maxPosition,
  68. progressWidth: this.data.width,
  69. isSuccess: true
  70. });
  71. // 触发成功事件
  72. this.triggerEvent('success');
  73. } else {
  74. // 滑动不足,重置
  75. this.setData({
  76. position: 0,
  77. progressWidth: 0
  78. });
  79. }
  80. },
  81. // 重置滑块
  82. reset() {
  83. this.setData({
  84. position: 0,
  85. progressWidth: 0,
  86. isSuccess: false
  87. });
  88. }
  89. }
  90. });