indexSubsection.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <template>
  2. <view class="nav-subsection ">
  3. <view class="nav-subsection-active" :style="{left: subsectionLeft + 'px', width: subsectionWidth + 'px'}"></view>
  4. <view class="nav-subsection-item x-c" :style="{color: topNavId != 0 ? '#fff':''}" @click="handleTopNav(0)">首页</view>
  5. <view class="nav-subsection-item x-c" :style="{color: topNavId != 1 ? '#fff':''}" @click="handleTopNav(1)">健康</view>
  6. </view>
  7. </template>
  8. <script>
  9. export default {
  10. data() {
  11. return {
  12. topNavId: 0,
  13. subsectionLeft: 0,
  14. subsectionWidth: 0,
  15. navSubSize: {},
  16. }
  17. },
  18. methods: {
  19. handleTopNav(id) {
  20. this.topNavId = id
  21. uni.setStorageSync("indexTemplate",this.topNavId)
  22. this.updateIndicator(this.navSubSize[this.topNavId].width, this.navSubSize[this.topNavId].width);
  23. },
  24. selectorQuery() {
  25. this.topNavId = uni.getStorageSync("indexTemplate") || 0
  26. uni.createSelectorQuery().in(this).selectAll('.nav-subsection-item').boundingClientRect((rects) => {
  27. rects.forEach((rect,index) => {
  28. this.navSubSize[index] = rect;
  29. })
  30. this.updateIndicator(this.navSubSize[this.topNavId].width, this.navSubSize[this.topNavId].width);
  31. }).exec();
  32. },
  33. updateIndicator(left, width) {
  34. this.$emit('onChange',this.topNavId)
  35. this.subsectionLeft = this.topNavId * left + uni.upx2px(6);
  36. this.subsectionWidth = width;
  37. },
  38. }
  39. }
  40. </script>
  41. <style lang="scss" scoped>
  42. @mixin u-flex($flexD, $alignI, $justifyC) {
  43. display: flex;
  44. flex-direction: $flexD;
  45. align-items: $alignI;
  46. justify-content: $justifyC;
  47. }
  48. .nav-subsection {
  49. @include u-flex(row, center, center);
  50. // background-color: rgba(255, 255, 255, 0.3);
  51. background-color: rgba(0, 0, 0, 0.1);
  52. padding: 6rpx;
  53. box-sizing: border-box;
  54. border-radius: 50rpx;
  55. font-family: PingFang SC, PingFang SC;
  56. font-weight: 400;
  57. font-size: 24rpx;
  58. color: #FFFFFF;
  59. position: relative;
  60. z-index: 2;
  61. &-item {
  62. min-width: 96rpx;
  63. min-height: 50rpx;
  64. padding: 0 16rpx;
  65. box-sizing: border-box;
  66. // line-height: 50rpx;
  67. text-align: center;
  68. border-radius: 26rpx 26rpx 26rpx 26rpx;
  69. color: #fff;
  70. color: #2583EB;
  71. }
  72. &-active {
  73. background: #fff;
  74. position: absolute;
  75. top: 6rpx;
  76. left: 6rpx;
  77. z-index: -1;
  78. // min-width: 96rpx;
  79. min-height: 50rpx;
  80. border-radius: 26rpx 26rpx 26rpx 26rpx;
  81. transition-duration: 0.2s;
  82. transition-property: left;
  83. }
  84. }
  85. </style>