index.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. Component({
  2. options: {
  3. multipleSlots: true,
  4. addGlobalClass: true
  5. },
  6. properties: {
  7. list: {
  8. type: Array
  9. },
  10. active: {
  11. type: Number,
  12. value: 0
  13. },
  14. onChenge: {
  15. type: Function
  16. }
  17. },
  18. data: {
  19. scrollLeft: 0,
  20. startPoint: 0, //记录滑动的初始位置
  21. slipFlag: false, //定义 滑动事件 节流阀, 防止一次滑动触发多次滑动事件
  22. },
  23. methods: {
  24. tabsChenge(e) {
  25. const {
  26. index
  27. } = e.currentTarget.dataset;
  28. if (this.data.active == index) return;
  29. this.setData({
  30. active: index
  31. });
  32. this.setActive();
  33. },
  34. setActive() {
  35. const that = this,
  36. active = this.data.active,
  37. query = wx.createSelectorQuery().in(this)
  38. query.select('#active' + active).boundingClientRect(function (res) {
  39. that.setData({
  40. scrollLeft: res.right - res.width
  41. })
  42. }).exec();
  43. this.triggerEvent("onChenge", active)
  44. },
  45. myTouchStart(e) {
  46. return;
  47. this.setData({
  48. slipFlag: true,
  49. startPoint: e.touches[0]
  50. })
  51. },
  52. myTouchMove(e) {
  53. return;
  54. let active = this.data.active;
  55. if (((this.data.startPoint.clientX - e.touches[e.touches.length - 1].clientX) > 80) && this.data.slipFlag) {
  56. if (active != this.data.list.length - 1) active++;
  57. this.setData({
  58. slipFlag: false,
  59. active
  60. })
  61. } else if (((this.data.startPoint.clientX - e.touches[e.touches.length - 1].clientX) < -80) && this.data.slipFlag) {
  62. if (active != 0) active--;
  63. this.setData({
  64. slipFlag: false,
  65. active
  66. })
  67. }
  68. this.setActive();
  69. },
  70. }
  71. })