index.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. lifetimes: {
  19. attached() {
  20. getApp().globalData.Language.getLanguagePackage(this)
  21. }
  22. },
  23. data: {
  24. scrollLeft: 0,
  25. startPoint: 0, //记录滑动的初始位置
  26. slipFlag: false, //定义 滑动事件 节流阀, 防止一次滑动触发多次滑动事件
  27. },
  28. methods: {
  29. tabsChenge(e) {
  30. const {
  31. index
  32. } = e.currentTarget.dataset;
  33. if (this.data.active == index) return;
  34. this.setData({
  35. active: index
  36. });
  37. this.setActive();
  38. },
  39. setActive() {
  40. const that = this,
  41. active = this.data.active,
  42. query = wx.createSelectorQuery().in(this)
  43. query.select('#active' + active).boundingClientRect(function (res) {
  44. let index = res.dataset.index,
  45. scrollLeft = res.left;
  46. if (index >= 6) scrollLeft += ((index - 4) * res.width);
  47. if (that.data.scrollLeft > scrollLeft && index >= 4) scrollLeft += (2 * res.width)
  48. that.setData({
  49. scrollLeft
  50. })
  51. }).exec();
  52. this.triggerEvent("onChenge", active)
  53. },
  54. myTouchStart(e) {
  55. return;
  56. this.setData({
  57. slipFlag: true,
  58. startPoint: e.touches[0]
  59. })
  60. },
  61. myTouchMove(e) {
  62. return;
  63. let active = this.data.active;
  64. if (((this.data.startPoint.clientX - e.touches[e.touches.length - 1].clientX) > 80) && this.data.slipFlag) {
  65. if (active != this.data.list.length - 1) active++;
  66. this.setData({
  67. slipFlag: false,
  68. active
  69. })
  70. } else if (((this.data.startPoint.clientX - e.touches[e.touches.length - 1].clientX) < -80) && this.data.slipFlag) {
  71. if (active != 0) active--;
  72. this.setData({
  73. slipFlag: false,
  74. active
  75. })
  76. }
  77. this.setActive();
  78. },
  79. }
  80. })