indexSubsection.vue 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 ? '#626469':''}" @click="handleTopNav(0)">首页</view>
  5. <view class="nav-subsection-item x-c" :style="{color: topNavId != 1 ? '#626469':''}" @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.06);
  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: #626469;
  59. position: relative;
  60. z-index: 2;
  61. &-item {
  62. min-width: 120rpx;
  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: #FF7700;
  71. }
  72. &-active {
  73. // background: #fff;
  74. background: linear-gradient( 136deg, #EE7D4E 0%, #E86235 100%);
  75. position: absolute;
  76. top: 6rpx;
  77. left: 6rpx;
  78. z-index: -1;
  79. min-width: 120rpx;
  80. min-height: 50rpx;
  81. border-radius: 26rpx 26rpx 26rpx 26rpx;
  82. transition-duration: 0.2s;
  83. transition-property: left;
  84. }
  85. }
  86. </style>