index.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. lifetimes: {
  24. attached: function () {
  25. getApp().globalData.Language.getLanguagePackage(this)
  26. }
  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. that.setData({
  45. scrollLeft: res.right - res.width
  46. })
  47. }).exec();
  48. this.triggerEvent("onChenge", active)
  49. },
  50. myTouchStart(e) {
  51. return;
  52. this.setData({
  53. slipFlag: true,
  54. startPoint: e.touches[0]
  55. })
  56. },
  57. myTouchMove(e) {
  58. return;
  59. let active = this.data.active;
  60. if (((this.data.startPoint.clientX - e.touches[e.touches.length - 1].clientX) > 80) && this.data.slipFlag) {
  61. if (active != this.data.list.length - 1) active++;
  62. this.setData({
  63. slipFlag: false,
  64. active
  65. })
  66. } else if (((this.data.startPoint.clientX - e.touches[e.touches.length - 1].clientX) < -80) && this.data.slipFlag) {
  67. if (active != 0) active--;
  68. this.setData({
  69. slipFlag: false,
  70. active
  71. })
  72. }
  73. this.setActive();
  74. },
  75. }
  76. })