index.js 2.5 KB

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