indexSubsection.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <template>
  2. <view class="nav-subsection " :style="{backgroundColor:!$isIos()?'rgba(0, 0, 0, 0.1)':''}">
  3. <view class="nav-subsection-active1" v-if="$isIos()" :style="{left: subsectionLeft + 'px', width: subsectionWidth + 'px'}"></view>
  4. <view class="nav-subsection-active" v-else :style="{left: subsectionLeft + 'px', width: subsectionWidth + 'px'}"></view>
  5. <view class="nav-subsection-item x-c" v-if="$isIos()" :style="{color: topNavId != 0 ? '#fff':'#fff',fontSize:'18px'}" @click="handleTopNav(0)">首页</view>
  6. <view class="nav-subsection-item x-c" v-else :style="{color: topNavId != 0 ? '#fff':''}" @click="handleTopNav(0)">首页</view>
  7. <view v-if="!$isIos()" class="nav-subsection-item x-c" :style="{color: topNavId != 1 ? '#fff':''}" @click="handleTopNav(1)">健康</view>
  8. </view>
  9. </template>
  10. <script>
  11. export default {
  12. data() {
  13. return {
  14. topNavId: 0,
  15. subsectionLeft: 0,
  16. subsectionWidth: 0,
  17. navSubSize: {},
  18. }
  19. },
  20. methods: {
  21. handleTopNav(id) {
  22. this.topNavId = id
  23. uni.setStorageSync("indexTemplate",this.topNavId)
  24. this.updateIndicator(this.navSubSize[this.topNavId].width, this.navSubSize[this.topNavId].width);
  25. },
  26. selectorQuery() {
  27. this.topNavId = uni.getStorageSync("indexTemplate") || 0
  28. uni.createSelectorQuery().in(this).selectAll('.nav-subsection-item').boundingClientRect((rects) => {
  29. rects.forEach((rect,index) => {
  30. this.navSubSize[index] = rect;
  31. })
  32. this.updateIndicator(this.navSubSize[this.topNavId].width, this.navSubSize[this.topNavId].width);
  33. }).exec();
  34. },
  35. updateIndicator(left, width) {
  36. this.$emit('onChange',this.topNavId)
  37. this.subsectionLeft = this.topNavId * left + uni.upx2px(6);
  38. this.subsectionWidth = width;
  39. },
  40. }
  41. }
  42. </script>
  43. <style lang="scss" scoped>
  44. @mixin u-flex($flexD, $alignI, $justifyC) {
  45. display: flex;
  46. flex-direction: $flexD;
  47. align-items: $alignI;
  48. justify-content: $justifyC;
  49. }
  50. .nav-subsection {
  51. @include u-flex(row, center, center)
  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: #FF7700;
  70. }
  71. &-active {
  72. background: #fff;
  73. position: absolute;
  74. top: 6rpx;
  75. left: 6rpx;
  76. z-index: -1;
  77. // min-width: 96rpx;
  78. min-height: 50rpx;
  79. border-radius: 26rpx 26rpx 26rpx 26rpx;
  80. transition-duration: 0.2s;
  81. transition-property: left;
  82. }
  83. &-active1 {
  84. position: absolute;
  85. top: 6rpx;
  86. left: 6rpx;
  87. z-index: -1;
  88. // min-width: 96rpx;
  89. min-height: 50rpx;
  90. border-radius: 26rpx 26rpx 26rpx 26rpx;
  91. transition-duration: 0.2s;
  92. transition-property: left;
  93. }
  94. }
  95. </style>