vtabs.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. Component({
  2. options: {
  3. addGlobalClass: true,
  4. pureDataPattern: /^_/,
  5. multipleSlots: true
  6. },
  7. properties: {
  8. vtabs: {type: Array, value: []},
  9. tabBarClass: {type: String, value: ''},
  10. activeClass: {type: String, value: ''},
  11. tabBarLineColor: {type: String, value: '#07c160'},
  12. tabBarInactiveTextColor: {type: String, value: '#000000'},
  13. tabBarActiveTextColor: {type: String, value: '#07c160'},
  14. tabBarInactiveBgColor: {type: String, value: '#eeeeee'},
  15. tabBarActiveBgColor: {type: String, value: '#ffffff'},
  16. activeTab: {type: Number, value: 0},
  17. animation: {type: Boolean, value: true}
  18. },
  19. data: {
  20. currentView: 0,
  21. contentScrollTop: 0,
  22. _heightRecords: [],
  23. _contentHeight: {}
  24. },
  25. observers: {
  26. activeTab: function activeTab(_activeTab) {
  27. this.scrollTabBar(_activeTab)
  28. }
  29. },
  30. relations: {
  31. '../vtabs-content/vtabs-content': {
  32. type: 'child',
  33. linked: function linked(target) {
  34. const _this = this
  35. target.calcHeight(function (rect) {
  36. _this.data._contentHeight[target.data.tabIndex] = rect.height
  37. if (_this._calcHeightTimer) {
  38. clearTimeout(_this._calcHeightTimer)
  39. }
  40. _this._calcHeightTimer = setTimeout(function () {
  41. _this.calcHeight()
  42. }, 100)
  43. })
  44. },
  45. unlinked: function unlinked(target) {
  46. delete this.data._contentHeight[target.data.tabIndex]
  47. }
  48. }
  49. },
  50. lifetimes: {
  51. attached: function attached() {}
  52. },
  53. methods: {
  54. calcHeight: function calcHeight() {
  55. const length = this.data.vtabs.length
  56. const _contentHeight = this.data._contentHeight
  57. const _heightRecords = []
  58. let temp = 0
  59. for (let i = 0; i < length; i++) {
  60. _heightRecords[i] = temp + (_contentHeight[i] || 0)
  61. temp = _heightRecords[i]
  62. }
  63. this.data._heightRecords = _heightRecords
  64. },
  65. scrollTabBar: function scrollTabBar(index) {
  66. const len = this.data.vtabs.length
  67. if (len === 0) return
  68. let currentView = index < 6 ? 0 : index - 5
  69. if (currentView >= len) currentView = len - 1
  70. this.setData({currentView})
  71. },
  72. handleTabClick: function handleTabClick(e) {
  73. const _heightRecords = this.data._heightRecords
  74. const index = e.currentTarget.dataset.index
  75. const contentScrollTop = _heightRecords[index - 1] || 0
  76. this.triggerEvent('tabclick', {index})
  77. this.setData({
  78. activeTab: index,
  79. contentScrollTop
  80. })
  81. },
  82. handleContentScroll: function handleContentScroll(e) {
  83. const _heightRecords = this.data._heightRecords
  84. if (_heightRecords.length === 0) return
  85. const length = this.data.vtabs.length
  86. const scrollTop = e.detail.scrollTop
  87. let index = 0
  88. if (scrollTop >= _heightRecords[0]) {
  89. for (let i = 1; i < length; i++) {
  90. if (scrollTop >= _heightRecords[i - 1] && scrollTop < _heightRecords[i]) {
  91. index = i
  92. break
  93. }
  94. }
  95. }
  96. if (index !== this.data.activeTab) {
  97. this.triggerEvent('change', {index})
  98. this.setData({activeTab: index})
  99. }
  100. }
  101. }
  102. })