index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. let index = res.dataset.index,
  40. scrollLeft = res.left;
  41. if (index >= 6) scrollLeft += ((index - 4) * res.width);
  42. if (that.data.scrollLeft > scrollLeft && index >= 4) scrollLeft += (2 * res.width)
  43. that.setData({
  44. scrollLeft
  45. })
  46. }).exec();
  47. this.triggerEvent("onChenge", active)
  48. },
  49. myTouchStart(e) {
  50. return;
  51. this.setData({
  52. slipFlag: true,
  53. startPoint: e.touches[0]
  54. })
  55. },
  56. myTouchMove(e) {
  57. return;
  58. let active = this.data.active;
  59. if (((this.data.startPoint.clientX - e.touches[e.touches.length - 1].clientX) > 80) && this.data.slipFlag) {
  60. if (active != this.data.list.length - 1) active++;
  61. this.setData({
  62. slipFlag: false,
  63. active
  64. })
  65. } else if (((this.data.startPoint.clientX - e.touches[e.touches.length - 1].clientX) < -80) && this.data.slipFlag) {
  66. if (active != 0) active--;
  67. this.setData({
  68. slipFlag: false,
  69. active
  70. })
  71. }
  72. this.setActive();
  73. },
  74. }
  75. })