index.js 2.0 KB

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