scs-scroll-iconbar.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <!--
  2. * @Author: jmy
  3. * @Date: 2026-01-06 15:02:01
  4. * @LastEditors: Please set LastEditors
  5. * @LastEditTime: 2026-01-06 15:02:01
  6. * @Description: 滚动图标栏
  7. -->
  8. <template>
  9. <scroll-view class="scs-scroll-iconbar" scroll-x :scroll-left="scrollLeft" scroll-with-animation="true">
  10. <view v-for="(item, index) in tabsData" :key="index" class="nav-item"
  11. :style="{ 'margin-right': calcGap * 2 + 'rpx' }" :class="{ 'act-current': index === tabCurrentIndex, }"
  12. @tap="tabChange(index)" :id="'item-' + index">
  13. <view class="item-title">
  14. {{ item[nameKey] }}
  15. </view>
  16. <uni-transition type="fade" :duration="300" :show="index === tabCurrentIndex">
  17. <image src="/static/images/home/tj_tab_hover_icon20@2x.png"></image>
  18. </uni-transition>
  19. </view>
  20. </scroll-view>
  21. </template>
  22. <script>
  23. export default {
  24. name: 'scs-scroll-iconbar',
  25. props: {
  26. // 导航项数据
  27. tabsData: {
  28. type: Array,
  29. default() {
  30. return [];
  31. },
  32. },
  33. // 当前选中的导航项索引
  34. tabCurrentIndex: {
  35. type: Number,
  36. default: 0,
  37. },
  38. // 导航项名称键名
  39. nameKey: {
  40. type: String,
  41. default: 'name',
  42. },
  43. // 导航项间距rpx
  44. calcGap: {
  45. type: Number,
  46. default: 60,
  47. },
  48. // 是否开启滚动
  49. isScroll: {
  50. type: Boolean,
  51. default: false,
  52. }
  53. },
  54. data() {
  55. return {
  56. scrollLeft: 0,
  57. widthList: [],
  58. screenWidth: 0,
  59. };
  60. },
  61. watch: {
  62. tabCurrentIndex: function (val) {
  63. if (this.isScroll) {
  64. this.tabScroll(val);
  65. }
  66. },
  67. tabsData: function (val) {
  68. this.init();
  69. },
  70. },
  71. methods: {
  72. // 导航栏点击
  73. tabChange(index) {
  74. let self = this;
  75. self.$emit('tabChange', index);
  76. },
  77. // 导航栏滚动
  78. tabScroll(index) {
  79. var widthArr = this.widthList;
  80. var scrollWidth = 0;
  81. for (var i = 0; i < index + 1; i++) {
  82. scrollWidth += widthArr[i];
  83. }
  84. let currentWidth = widthArr[index];
  85. // scrollView 居中算法
  86. // 减去固定宽度位移
  87. // 减去选中的bar的宽度的一半
  88. scrollWidth -= this.screenWidth / 2;
  89. scrollWidth -= currentWidth / 2;
  90. this.scrollLeft = scrollWidth;
  91. },
  92. // 计算导航项宽度
  93. calculateItemWidth() {
  94. let widthArr = [];
  95. Promise.all(
  96. this.tabsData.map((_, index) =>
  97. new Promise((resolve) => {
  98. uni
  99. .createSelectorQuery()
  100. .in(this)
  101. .select(`#item-${index}`)
  102. .boundingClientRect((data) => {
  103. if (data) {
  104. // 在元素实际宽度基础上增加 this.calcGap 的左右 padding,保证滚动时两侧留白
  105. widthArr.push(data.width + this.calcGap);
  106. } else {
  107. // 兜底默认宽度
  108. widthArr.push(100);
  109. }
  110. resolve();
  111. })
  112. .exec();
  113. })
  114. )
  115. ).then(() => {
  116. // 所有节点查询完毕,一次性赋值
  117. this.widthList = widthArr;
  118. });
  119. },
  120. // 计算窗口宽度
  121. calculateWindowWidth() {
  122. var info = uni.getSystemInfoSync();
  123. this.screenWidth = info.screenWidth;
  124. },
  125. init() {
  126. this.calculateWindowWidth();
  127. // 组件挂载后计算,确保DOM已渲染
  128. this.$nextTick(() => {
  129. this.calculateItemWidth();
  130. });
  131. },
  132. },
  133. mounted() {
  134. this.init();
  135. },
  136. };
  137. </script>
  138. <style lang="scss" scoped>
  139. ::v-deep.uni-scroll-view::-webkit-scrollbar {
  140. display: none;
  141. }
  142. .scs-scroll-iconbar {
  143. width: 100%;
  144. height: 90rpx;
  145. white-space: nowrap;
  146. box-sizing: border-box;
  147. .nav-item {
  148. height: 100%;
  149. text-align: center;
  150. color: #333333;
  151. display: inline-block;
  152. position: relative;
  153. font-size: 32rpx;
  154. position: relative;
  155. font-weight: 400;
  156. image {
  157. width: 20px;
  158. height: 6px;
  159. position: absolute;
  160. bottom: 0;
  161. left: 50%;
  162. transform: translateX(-50%);
  163. transition: all 0.3s ease;
  164. }
  165. .item-title {
  166. line-height: 90rpx;
  167. display: inline-block;
  168. position: relative;
  169. z-index: 10;
  170. }
  171. }
  172. .act-current {
  173. color: #02B176;
  174. font-size: 36rpx;
  175. font-weight: 600;
  176. }
  177. }
  178. </style>