tui-slide-verify.wxs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. var slideBarWidth = 200;
  2. var slideBlockWidth = 32;
  3. var errorRange = 2
  4. var disabled = false
  5. function bool(str) {
  6. return str === 'true' || str == true ? true : false
  7. }
  8. function touchstart(e, ins) {
  9. var state=e.instance.getState()
  10. var touch = e.touches[0] || e.changedTouches[0]
  11. var dataset = e.instance.getDataset()
  12. state.startX = touch.pageX
  13. slideBarWidth = +dataset.slidebarwidth
  14. slideBlockWidth = +dataset.slideblockwidth
  15. errorRange = +dataset.errorrange
  16. disabled = bool(dataset.disabled)
  17. }
  18. function styleChange(left, ins) {
  19. if (!ins) return;
  20. ins.selectComponent('.tui-slider-block').setStyle({
  21. transform: 'translate3d(' + left + 'px,0,0)'
  22. })
  23. ins.selectComponent('.tui-slide-glided').setStyle({
  24. width: left + 'px'
  25. })
  26. }
  27. function touchmove(e, ins) {
  28. if (disabled) return;
  29. var state=e.instance.getState()
  30. var touch = e.touches[0] || e.changedTouches[0]
  31. var pageX = touch.pageX;
  32. var left = pageX - state.startX + (state.lastLeft || 0);
  33. left = left < 0 ? 0 : left;
  34. var width = slideBarWidth - slideBlockWidth;
  35. left = left >= width ? width : left;
  36. state.startX = pageX
  37. state.lastLeft = left
  38. styleChange(left, ins)
  39. }
  40. function touchend(e, ins) {
  41. if (disabled) return;
  42. var state=e.instance.getState()
  43. let left = slideBarWidth - slideBlockWidth
  44. if (left - state.lastLeft <= errorRange) {
  45. styleChange(left, ins)
  46. ins.callMethod('success')
  47. } else {
  48. state.startX = 0;
  49. state.lastLeft = 0;
  50. styleChange(0, ins)
  51. }
  52. }
  53. function slidereset(reset, oldreset, owner, ins) {
  54. var state=ins.getState()
  55. if (reset > 0) {
  56. state.startX = 0;
  57. state.lastLeft = 0;
  58. styleChange(0, owner)
  59. }
  60. }
  61. module.exports = {
  62. touchstart: touchstart,
  63. touchmove: touchmove,
  64. touchend: touchend,
  65. slidereset: slidereset
  66. }