index.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.selected_icon ? item.selected_icon : item.selectedIconPath) : (item.icon ? item.icon : 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. this.getData()
  75. },
  76. methods: {
  77. handleSwitch(index) {
  78. if (index === this.active) {
  79. return
  80. }
  81. this.$store.dispatch('tab/index', index)
  82. const item = this.list[index]
  83. uni.switchTab({
  84. url: item.pagePath
  85. })
  86. },
  87. calc() {
  88. let active = 1
  89. const page = uni.$u.page().replace('//', '/')
  90. this.list.forEach((obj, index) => {
  91. if (obj.pagePath === page) {
  92. active = index
  93. }
  94. })
  95. if (active !== this.active) {
  96. this.$store.dispatch('tab/index', active)
  97. }
  98. },
  99. getData() {
  100. this.$api.setting.tabBar().then(res => {
  101. const data = res.data
  102. data.forEach(obj => {
  103. Object.assign(obj, this.types[obj.type])
  104. })
  105. console.log('-->data', data)
  106. this.list = data
  107. })
  108. }
  109. }
  110. }
  111. </script>
  112. <style lang="scss" scoped>
  113. .tab-bar{
  114. position: fixed;
  115. bottom: 0;
  116. left: 0;
  117. width: 100%;
  118. .content{
  119. background: $bg-color;
  120. position: absolute;
  121. bottom: 0;
  122. width: 100%;
  123. height: 120rpx;
  124. background-size: 110% 100%;
  125. .tab-item{
  126. flex: 1;
  127. color: #8a8a8a;
  128. transition: .3s;
  129. font-size: 24rpx;
  130. position: relative;
  131. &.active{
  132. color: #6EEBE8;
  133. }
  134. .icon{
  135. width: 42rpx;
  136. height: 42rpx;
  137. image{
  138. height: 100%;
  139. width: 100%;
  140. }
  141. }
  142. }
  143. }
  144. }
  145. </style>