tui-tabs.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. Component({
  2. properties: {
  3. //标签页
  4. tabs: {
  5. type: Array,
  6. value: []
  7. },
  8. //rpx
  9. height: {
  10. type: Number,
  11. value: 80
  12. },
  13. //rpx 只对左右padding起作用,上下为0
  14. padding: {
  15. type: Number,
  16. value: 30
  17. },
  18. //背景色
  19. bgColor: {
  20. type: String,
  21. value: "#FFFFFF"
  22. },
  23. //是否固定
  24. isFixed: {
  25. type: Boolean,
  26. value: false
  27. },
  28. //px
  29. top: {
  30. type: Number,
  31. value: 0
  32. },
  33. //是否去掉底部线条
  34. unlined: {
  35. type: Boolean,
  36. value: false
  37. },
  38. //当前选项卡
  39. currentTab: {
  40. type: Number,
  41. value: 0,
  42. observer(val) {
  43. this.checkCor();
  44. }
  45. },
  46. //滑块宽度
  47. sliderWidth: {
  48. type: Number,
  49. value: 68
  50. },
  51. //滑块高度
  52. sliderHeight: {
  53. type: Number,
  54. value: 6
  55. },
  56. //滑块背景颜色
  57. sliderBgColor: {
  58. type: String,
  59. value: "#5677fc"
  60. },
  61. //滑块bottom
  62. bottom: {
  63. type: String,
  64. value: "0"
  65. },
  66. //标签页宽度
  67. itemWidth: {
  68. type: String,
  69. value: "25%"
  70. },
  71. //字体颜色
  72. color: {
  73. type: String,
  74. value: "#666"
  75. },
  76. //选中后字体颜色
  77. selectedColor: {
  78. type: String,
  79. value: "#5677fc"
  80. },
  81. //字体大小
  82. size: {
  83. type: Number,
  84. value: 28
  85. },
  86. //选中后 是否加粗 ,未选中则无效
  87. bold: {
  88. type: Boolean,
  89. value: false
  90. }
  91. },
  92. lifetimes: {
  93. ready: function() {
  94. setTimeout(() => {
  95. wx.getSystemInfo({
  96. success: (res) => {
  97. this.setData({
  98. winWidth: res.windowWidth
  99. }, () => {
  100. this.checkCor()
  101. })
  102. }
  103. });
  104. }, 20);
  105. }
  106. },
  107. data: {
  108. winWidth: 0,
  109. scrollLeft: 0
  110. },
  111. methods: {
  112. checkCor: function() {
  113. let tabsNum = this.data.tabs.length
  114. let padding = this.data.winWidth / 750 * this.data.padding
  115. let width = this.data.winWidth - padding * 2
  116. let left = (width / tabsNum - (this.data.winWidth / 750 * this.data.sliderWidth)) / 2 + padding
  117. let scrollLeft = left
  118. if (this.data.currentTab > 0) {
  119. scrollLeft = scrollLeft + (width / tabsNum) * this.data.currentTab
  120. }
  121. this.setData({
  122. scrollLeft: scrollLeft
  123. })
  124. },
  125. // 点击标题切换当前页时改变样式
  126. swichTabs: function(e) {
  127. let index = Number(e.currentTarget.dataset.index)
  128. let item = this.data.tabs[index]
  129. if (item && item.disabled) return;
  130. if (this.data.currentTab == index) {
  131. return false;
  132. } else {
  133. this.triggerEvent("change", {
  134. index: Number(index)
  135. })
  136. }
  137. }
  138. }
  139. })