index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. Component({
  2. options: {
  3. addGlobalClass: true,
  4. pureDataPattern: /^_/,
  5. multipleSlots: true
  6. },
  7. properties: {
  8. tabs: {
  9. type: Array,
  10. value: []
  11. },
  12. tabClass: {
  13. type: String,
  14. value: ''
  15. },
  16. swiperClass: {
  17. type: String,
  18. value: ''
  19. },
  20. activeClass: {
  21. type: String,
  22. value: ''
  23. },
  24. tabUnderlineColor: {
  25. type: String,
  26. value: '#07c160'
  27. },
  28. tabActiveTextColor: {
  29. type: String,
  30. value: '#000000'
  31. },
  32. tabInactiveTextColor: {
  33. type: String,
  34. value: '#000000'
  35. },
  36. tabBackgroundColor: {
  37. type: String,
  38. value: '#ffffff'
  39. },
  40. activeTab: {
  41. type: Number,
  42. value: 0
  43. },
  44. swipeable: {
  45. type: Boolean,
  46. value: true
  47. },
  48. animation: {
  49. type: Boolean,
  50. value: true
  51. },
  52. duration: {
  53. type: Number,
  54. value: 500
  55. }
  56. },
  57. data: {
  58. currentView: 0,
  59. scrollLeft: 0
  60. },
  61. observers: {
  62. activeTab(_activeTab) {
  63. this.init(_activeTab);
  64. },
  65. tabs() {
  66. this.init(this.data.activeTab);
  67. }
  68. },
  69. lifetimes: {
  70. ready() {
  71. }
  72. },
  73. methods: {
  74. init(_activeTab) {
  75. const len = this.data.tabs.length;
  76. if (len === 0) return;
  77. let currentView = _activeTab - 1;
  78. if (currentView < 0) currentView = 0;
  79. if (currentView > len - 1) currentView = len - 1;
  80. this.setData({
  81. currentView: currentView
  82. });
  83. const self = this;
  84. const query = this.createSelectorQuery();
  85. query.select('.weui-tabs-bar__item').boundingClientRect();
  86. query.selectViewport().scrollOffset()
  87. query.exec(function(res){
  88. const width = res[0].width;
  89. const num = Math.floor((_activeTab) / 6);
  90. self.setData({
  91. scrollLeft: width * num * 6
  92. });
  93. })
  94. },
  95. handleTabClick: function handleTabClick(e) {
  96. var index = e.currentTarget.dataset.index;
  97. this.setData({
  98. activeTab: index
  99. });
  100. this.triggerEvent('tabclick', {
  101. index: index
  102. });
  103. },
  104. handleSwiperChange: function handleSwiperChange(e) {
  105. var index = e.detail.current;
  106. this.setData({
  107. activeTab: index
  108. });
  109. this.triggerEvent('change', {
  110. index: index
  111. });
  112. }
  113. }
  114. });