index.js 2.0 KB

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