tabBar.nvue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <template>
  2. <div>
  3. <div v-if="drag" class="wrap tab-bar-scroll">
  4. <scroller class="scroll" scrollDirection="horizontal" showScrollbar="false">
  5. <div
  6. class="tab-bar-item tab-bar-scroll-width"
  7. v-for="(tabBar, t) in tabBars"
  8. :key="t"
  9. :ref="tabBar.id + t"
  10. @click="change(t)"
  11. >
  12. <text class="tab-bar-title" :class="[tabIndex === t ? 'active' : '']">{{
  13. tabBar.name
  14. }}</text>
  15. </div>
  16. </scroller>
  17. </div>
  18. <div v-else class="wrap tab-bar">
  19. <div
  20. class="tab-bar-item"
  21. v-for="(tabBar, t) in tabBars"
  22. :key="t"
  23. :ref="tabBar.id + t"
  24. @click="change(t)"
  25. >
  26. <text class="tab-bar-title" :class="[tabIndex === t ? 'active' : '']">{{
  27. tabBar.name
  28. }}</text>
  29. </div>
  30. </div>
  31. </div>
  32. </template>
  33. <script>
  34. const dom = weex.requireModule('dom');
  35. export default {
  36. props: {
  37. drag: {
  38. type: Boolean,
  39. default: true
  40. },
  41. tabBars: {
  42. type: Array,
  43. default: function(e) {
  44. return [];
  45. }
  46. },
  47. tabIndex: {
  48. type: Number,
  49. default: 0
  50. }
  51. },
  52. watch:{
  53. tabIndex(newVal){
  54. this.change(newVal)
  55. }
  56. },
  57. methods: {
  58. async change(index, e) {
  59. let ret = {
  60. index: index
  61. };
  62. // if (e.type === 'click') {
  63. // let target = e.target;
  64. // ret.index = target.parentNode.children.findIndex(node => node === target);
  65. // }
  66. this.$emit('_tabBarClick', ret);
  67. const el = this.$refs[this.tabBars[index].id + index][0]
  68. let elSize = await this.getElSize(el);
  69. if (elSize.left + elSize.width > 750) {
  70. let idx = index - 4;
  71. let newEl = this.$refs[this.tabBars[idx].id + idx][0]
  72. dom.scrollToElement(newEl, {});
  73. return;
  74. }
  75. if (elSize.left < 0) {
  76. dom.scrollToElement(el, {});
  77. }
  78. },
  79. getElSize(el) { //得到元素的size
  80. return new Promise((res, rej) => {
  81. const result = dom.getComponentRect(el, option => {
  82. res(option.size);
  83. })
  84. })
  85. }
  86. }
  87. };
  88. </script>
  89. <style>
  90. .wrap {
  91. height: 100px;
  92. width: 750px;
  93. flex-direction: row;
  94. border-width: 1px;
  95. border-color: #c8c7cc;
  96. border-style: solid;
  97. font-size: 28px;
  98. }
  99. .tab-bar {
  100. justify-content: space-between;
  101. padding-left: 30px;
  102. padding-right: 30px;
  103. }
  104. .scroll {
  105. height: 100px;
  106. width: 750px;
  107. flex-direction: row;
  108. }
  109. .tab-bar-item {
  110. height: 100px;
  111. flex-direction: column;
  112. align-items: center;
  113. justify-content: center;
  114. }
  115. .tab-bar-scroll-width {
  116. width: 150px;
  117. }
  118. .tab-bar-title {
  119. height: 100px;
  120. line-height: 100px;
  121. font-size: 30px;
  122. color: #555;
  123. }
  124. .active {
  125. color: #007aff;
  126. }
  127. </style>