tab.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. const themes = {
  2. smallBar: 'smallBar'
  3. }
  4. Component({
  5. /**
  6. * 组件的属性列表
  7. */
  8. properties: {
  9. items: {
  10. type: Array,
  11. value: ['item1', 'item2', 'item3', 'item4'],
  12. observer: function (newVal) {
  13. if (newVal && newVal.length < 5) {
  14. this.setData({
  15. itemWidth: (750 / newVal.length) - 60
  16. })
  17. }
  18. }
  19. },
  20. height: {
  21. type: String,
  22. value: '120'
  23. },
  24. textColor: {
  25. type: String,
  26. value: '#666666'
  27. },
  28. textSize: {
  29. type: String,
  30. value: '28'
  31. },
  32. selectColor: {
  33. type: String,
  34. value: '#426666'
  35. },
  36. selected: {
  37. type: String,
  38. value: '0',
  39. observer: function (newVal) {
  40. this.setData({
  41. mSelected: newVal
  42. })
  43. }
  44. },
  45. theme: {
  46. type: String,
  47. value: 'default',
  48. observer: function (newVal) {
  49. if (this.data.theme == themes.smallBar) {
  50. this.setData({
  51. bottom: this.data.height / 2 - this.data.textSize - 8,
  52. scrollStyle: ''
  53. })
  54. }
  55. }
  56. },
  57. dataCus: {
  58. type: Array,
  59. value: '',
  60. observer: function (newVal) {
  61. this.setData({
  62. mDataCus: newVal
  63. });
  64. }
  65. }
  66. },
  67. /**
  68. * 组件的初始数据
  69. */
  70. data: {
  71. itemWidth: 128,
  72. isScroll: true,
  73. scrollStyle: 'border-bottom: 1px solid #e5e5e5;',
  74. left: '0',
  75. right: '750',
  76. bottom: '0',
  77. mSelected: '0',
  78. lastIndex: 0,
  79. transition: 'left 0.5s, right 0.2s',
  80. windowWidth: 375,
  81. domData: [],
  82. textDomData: [],
  83. mDataCus: [],
  84. theme:"default"
  85. },
  86. externalClasses: ['cus'],
  87. /**
  88. * 组件的方法列表
  89. */
  90. methods: {
  91. barLeft: function(index, dom) {
  92. let that = this;
  93. this.setData({
  94. left: dom[index].left
  95. })
  96. },
  97. barRight: function (index, dom) {
  98. let that = this;
  99. this.setData({
  100. right: that.data.windowWidth - dom[index].right,
  101. })
  102. },
  103. onItemTap: function(e) {
  104. const index = e.currentTarget.dataset.index;
  105. let str = this.data.lastIndex < index ? 'left 0.5s, right 0.2s' : 'left 0.2s, right 0.5s';
  106. this.setData({
  107. transition: str,
  108. lastIndex: index,
  109. mSelected: index
  110. })
  111. if (this.data.theme == themes.smallBar) {
  112. this.barLeft(index, this.data.textDomData);
  113. this.barRight(index, this.data.textDomData);
  114. } else {
  115. this.barLeft(index, this.data.domData);
  116. this.barRight(index, this.data.domData);
  117. }
  118. this.triggerEvent('itemtap', e, { bubbles: true });
  119. }
  120. },
  121. ready: function() {
  122. let that = this;
  123. const sysInfo = wx.getSystemInfoSync();
  124. this.setData({
  125. windowWidth: sysInfo.screenWidth
  126. })
  127. const query = this.createSelectorQuery();
  128. query.in(this).selectAll('.item').fields({
  129. dataset: true,
  130. rect: true,
  131. size: true
  132. }, function (res) {
  133. that.setData({
  134. domData: res,
  135. })
  136. that.barLeft(that.data.mSelected, that.data.domData);
  137. that.barRight(that.data.mSelected, that.data.domData);
  138. // console.log(res)
  139. }).exec()
  140. query.in(this).selectAll('.text').fields({
  141. dataset: true,
  142. rect: true,
  143. size: true
  144. }, function (res) {
  145. that.setData({
  146. textDomData: res,
  147. })
  148. if (that.data.theme == themes.smallBar) {
  149. that.barLeft(that.data.mSelected, that.data.textDomData);
  150. that.barRight(that.data.mSelected, that.data.textDomData);
  151. }
  152. console.log(res)
  153. }).exec()
  154. },
  155. lifetimes: {
  156. ready: function () {
  157. let that = this;
  158. const sysInfo = wx.getSystemInfoSync();
  159. this.setData({
  160. windowWidth: sysInfo.screenWidth
  161. })
  162. const query = this.createSelectorQuery();
  163. query.in(this).selectAll('.item').fields({
  164. dataset: true,
  165. rect: true,
  166. size: true
  167. }, function (res) {
  168. that.setData({
  169. domData: res,
  170. })
  171. that.barLeft(that.data.mSelected, that.data.domData);
  172. that.barRight(that.data.mSelected, that.data.domData);
  173. // console.log(res)
  174. }).exec()
  175. query.in(this).selectAll('.text').fields({
  176. dataset: true,
  177. rect: true,
  178. size: true
  179. }, function (res) {
  180. that.setData({
  181. textDomData: res,
  182. })
  183. if (that.data.theme == themes.smallBar) {
  184. that.barLeft(that.data.mSelected, that.data.textDomData);
  185. that.barRight(that.data.mSelected, that.data.textDomData);
  186. }
  187. console.log(res)
  188. }).exec()
  189. },
  190. },
  191. })