scroll-navbar.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <template>
  2. <scroll-view class="ss-scroll-navbar" scroll-x :scroll-left="scrollLeft" scroll-with-animation="true">
  3. <view v-for="(item, index) in navArr" :key="item.category_id" class="nav-item" :class="{current: index === tabCurrentIndex}"
  4. @click="tabChange(index)" :id="'item-' + index">
  5. <text class="title">{{item.name}}</text>
  6. </view>
  7. </scroll-view>
  8. </template>
  9. <script>
  10. export default {
  11. name: 'ss-scroll-navbar',
  12. props: {
  13. navArr: {
  14. type: Array,
  15. default () {
  16. return [{
  17. name: '推荐',
  18. category_id: 'recent'
  19. }]
  20. }
  21. },
  22. tabCurrentIndex: {
  23. type: Number,
  24. default: 0
  25. },
  26. scrollChangeIndex: {
  27. type: Number,
  28. default: 0
  29. },
  30. },
  31. data() {
  32. return {
  33. scrollLeft: 0,
  34. widthList: [],
  35. screenWidth: 0,
  36. }
  37. },
  38. methods: {
  39. /**
  40. * 导航栏navbar
  41. * 点击事件
  42. */
  43. tabChange(index) {
  44. this.$emit('navbarTap', index);
  45. var widthArr = this.widthList;
  46. var scrollWidth = 0;
  47. for (var i = 0; i < index + 1; i++) {
  48. scrollWidth += widthArr[i];
  49. }
  50. let currentWidth = widthArr[index];
  51. // scrollView 居中算法
  52. // 减去固定宽度位移
  53. // 减去选中的bar的宽度的一半
  54. scrollWidth -= this.screenWidth / 2;
  55. scrollWidth -= currentWidth / 2;
  56. this.scrollLeft = scrollWidth;
  57. },
  58. calculateItemWidth() {
  59. var that = this;
  60. var arr = [];
  61. let w = 0;
  62. this.navArr.forEach((item, index) => {
  63. let view = uni.createSelectorQuery().in(this).select("#item-" + index);
  64. view.fields({
  65. size: true
  66. }, data => {
  67. arr.push(data.width);
  68. }).exec();
  69. })
  70. this.widthList = arr;
  71. },
  72. calculateWindowWidth() {
  73. var info = uni.getSystemInfoSync();
  74. this.screenWidth = info.screenWidth;
  75. }
  76. },
  77. created() {
  78. var that = this;
  79. this.calculateWindowWidth();
  80. setTimeout(function() {
  81. that.calculateItemWidth();
  82. }, 1000);
  83. },
  84. watch: {
  85. scrollChangeIndex(val) {
  86. this.tabChange(val)
  87. },
  88. },
  89. }
  90. </script>
  91. <style lang="scss">
  92. .ss-scroll-navbar {
  93. width: 100%;
  94. height: 80rpx;
  95. box-shadow: 0 2upx 8upx rgba(0, 0, 0, .06);
  96. white-space: nowrap;
  97. text-align: start;
  98. .nav-item {
  99. height: 80rpx;
  100. text-align: center;
  101. padding: 0upx 20upx;
  102. color: #FFFFFF;
  103. display: inline-block;
  104. position: relative;
  105. font-size: 32upx;
  106. .title {
  107. line-height: 80rpx;
  108. }
  109. &:after {
  110. content: '';
  111. width: 0;
  112. height: 0;
  113. border-bottom: 4upx solid #ffffff;
  114. position: absolute;
  115. left: 50%;
  116. bottom: 0;
  117. transform: translateX(-50%);
  118. transition: .3s;
  119. }
  120. }
  121. .current {
  122. color: #ffffff;
  123. font-weight: bold;
  124. &:after {
  125. width: 50%;
  126. }
  127. }
  128. }
  129. </style>