index.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <template>
  2. <view class="tab-bar">
  3. <view class="content main-between cross-center">
  4. <view
  5. v-for="(item,index) in list"
  6. :key="index"
  7. class="tab-item dir-top-wrap cross-center main-center"
  8. :class="{active: active === index}"
  9. @click="handleSwitch(index)"
  10. >
  11. <view class="icon">
  12. <image :src="active === index ? item.selectedIconPath : item.iconPath" mode="aspectFit" />
  13. </view>
  14. <text>{{ item.text }}</text>
  15. </view>
  16. </view>
  17. </view>
  18. </template>
  19. <script>
  20. import { mapState } from 'vuex'
  21. export default {
  22. name: 'TabBar',
  23. data() {
  24. return {
  25. color: '#8a8a8a',
  26. selectedColor: '#6EEBE8',
  27. types: {
  28. 1: {
  29. 'pagePath': '/pages/index/index',
  30. 'iconPath': '/static/image/tab/home.png',
  31. 'selectedIconPath': '/static/image/tab/home-HL.png'
  32. },
  33. 2: {
  34. 'pagePath': '/pages/trace/index',
  35. 'iconPath': '/static/image/tab/trace.png',
  36. 'selectedIconPath': '/static/image/tab/trace-HL.png'
  37. },
  38. 3: {
  39. 'pagePath': '/pages/my/index',
  40. 'iconPath': '/static/image/tab/my.png',
  41. 'selectedIconPath': '/static/image/tab/my-HL.png'
  42. }
  43. },
  44. list: [
  45. {
  46. 'pagePath': '/pages/index/index',
  47. 'iconPath': '/static/image/tab/home.png',
  48. 'selectedIconPath': '/static/image/tab/home-HL.png',
  49. 'text': '首页'
  50. },
  51. {
  52. 'pagePath': '/pages/trace/index',
  53. 'iconPath': '/static/image/tab/trace.png',
  54. 'selectedIconPath': '/static/image/tab/trace-HL.png',
  55. 'text': '追剧'
  56. },
  57. {
  58. 'pagePath': '/pages/my/index',
  59. 'iconPath': '/static/image/tab/my.png',
  60. 'selectedIconPath': '/static/image/tab/my-HL.png',
  61. 'text': '我的'
  62. }
  63. ]
  64. }
  65. },
  66. computed: {
  67. ...mapState({
  68. active: seate => seate.tab.index
  69. })
  70. },
  71. created() {
  72. this.calc()
  73. uni.hideTabBar()
  74. },
  75. methods: {
  76. handleSwitch(index) {
  77. if (index === this.active) {
  78. return
  79. }
  80. this.$store.dispatch('tab/index', index)
  81. const item = this.list[index]
  82. uni.switchTab({
  83. url: item.pagePath
  84. })
  85. },
  86. calc() {
  87. let active = 1
  88. const page = uni.$u.page()
  89. this.list.forEach((obj, index) => {
  90. if (obj.pagePath === page) {
  91. active = index
  92. }
  93. })
  94. if (active !== this.active) {
  95. this.$store.dispatch('tab/index', active)
  96. }
  97. },
  98. getData() {
  99. this.$api.setting.tabBar().then(res => {
  100. const data = res.data
  101. data.forEach(obj => {
  102. Object.assign(obj, this.types[obj.type])
  103. })
  104. console.log('-->data', data)
  105. this.list = data
  106. })
  107. }
  108. }
  109. }
  110. </script>
  111. <style lang="scss" scoped>
  112. .tab-bar{
  113. position: fixed;
  114. bottom: 0;
  115. left: 0;
  116. width: 100%;
  117. .content{
  118. background: $bg-color;
  119. position: absolute;
  120. bottom: 0;
  121. width: 100%;
  122. height: 120rpx;
  123. background-size: 110% 100%;
  124. .tab-item{
  125. flex: 1;
  126. color: #8a8a8a;
  127. transition: .3s;
  128. font-size: 24rpx;
  129. position: relative;
  130. &.active{
  131. color: #6EEBE8;
  132. }
  133. .icon{
  134. width: 42rpx;
  135. height: 42rpx;
  136. image{
  137. height: 100%;
  138. width: 100%;
  139. }
  140. }
  141. }
  142. }
  143. }
  144. </style>