index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. var t = require("../../utils/index");
  2. Component({
  3. externalClasses: ["i-class"],
  4. properties: {
  5. lineBgColor: {
  6. type: String
  7. },
  8. fontColor: {
  9. type: String
  10. },
  11. tabs: {
  12. type: Array,
  13. value: []
  14. },
  15. activeIndex: {
  16. type: Number,
  17. value: 0,
  18. observer: function (e) {
  19. if(-1 === this.properties.activeIndex) {
  20. this.resetIndicator();
  21. } else {
  22. this.executeAnimcation(e)
  23. }
  24. }
  25. }
  26. },
  27. data: {
  28. indicatorAnamationData: {},
  29. scrollLeft: 0
  30. },
  31. ready: function () {
  32. this.tabsWidth = [], this.tabsLeft = [], this.screenWidth = wx.getSystemInfoSync().screenWidth,
  33. this.setData({
  34. fontColor: this.data.fontColor ? this.data.fontColor : "#000"
  35. }), this.data.lineColor && this.setData({
  36. lineBgColor: this.data.lineBgColor
  37. });
  38. },
  39. methods: {
  40. handleTabItemTap: function (t) {
  41. var e = t.target.dataset.index, a = t.target.dataset.id;
  42. e !== this.data.activeIndex && (this.setData({
  43. activeIndex: e
  44. }), this.triggerEvent("activeIndexChange", {a,e}));
  45. },
  46. executeAnimcation: function (t) {
  47. var e = this;
  48. this.getLeftAndWidth(t).then(function (a) {
  49. var i = e.generateAnimationData(a.left, a.width, a.firstTabLeft);
  50. e.centerTheTab(t, a), e.setData({
  51. indicatorAnamationData: i
  52. });
  53. });
  54. },
  55. centerTheTab: function (t, e) {
  56. var a = e.width, i = (e.left, this.tabsWidth.slice(0, t).reduce(function (t, e) {
  57. return t + e + 25;
  58. }, 15));
  59. this.setData({
  60. scrollLeft: i - (this.screenWidth - a) / 2
  61. });
  62. },
  63. getLeftAndWidth: function (e) {
  64. var a = this;
  65. return new Promise(function (i) {
  66. (0, t.getRect)(a, ".tabs__nav", !0).then(function (t) {
  67. a.tabsWidth = t.map(function (t) {
  68. return t.width;
  69. }), a.tabsLeft = t.map(function (t) {
  70. return t.left;
  71. }), i({
  72. width: a.tabsWidth[e],
  73. left: a.tabsLeft[e],
  74. firstTabLeft: a.tabsLeft[0]
  75. });
  76. });
  77. });
  78. },
  79. generateAnimationData: function (t, e) {
  80. var a = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : 0, i = wx.createAnimation({
  81. duration: 200,
  82. timingFunction: "ease"
  83. });
  84. return [{
  85. width: e,
  86. left: t - a
  87. }].forEach(function (t) {
  88. var e = t.width, a = t.left;
  89. i.translateX(a).width(e).step();
  90. }), i.export();
  91. },
  92. resetIndicator: function () {
  93. let tabs = this.data.tabs || [];
  94. let initWidth = 28;
  95. if (tabs.length) initWidth = (tabs[0].name && tabs[0].name.length*14);
  96. var t = wx.createAnimation({
  97. duration: 200,
  98. timingFunction: "ease"
  99. });
  100. t.translateX(0).width(initWidth).step(), this.setData({
  101. scrollLeft: 0,
  102. indicatorAnamationData: t.export()
  103. });
  104. }
  105. }
  106. });