tabNav.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <view class="navTabBox">
  3. <view class="longTab">
  4. <scroll-view scroll-x="true" style="white-space: nowrap; display: flex;" scroll-with-animation :scroll-left="tabLeft" show-scrollbar="true">
  5. <view class="longItem" :style='"width:"+isWidth+"px"' :data-index="index" :class="index===tabClick?'click':''" v-for="(item,index) in tabTitle" :key="index" :id="'id'+index" @click="longClick(index)">{{item.cate_name}}</view>
  6. <view class="underlineBox" :style='"transform:translateX("+isLeft+"px);width:"+isWidth+"px"'>
  7. <view class="underline"></view>
  8. </view>
  9. </scroll-view>
  10. </view>
  11. <view class="child-box" v-if="tabClick>0 && tabTitle[tabClick].children.length>0">
  12. <scroll-view scroll-x="true" style="white-space: nowrap; display: flex;align-items: center; height: 100%;" scroll-with-animation :scroll-left="tabLeft" show-scrollbar="false">
  13. <view class="wrapper">
  14. <view v-for="(item,index) in tabTitle[tabClick].children" class="child-item" :class="{on:index == childIndex}" @click="childTab(tabClick,index)">
  15. <image :src="item.pic" mode=""></image>
  16. <view class="txt">{{item.cate_name}}</view>
  17. </view>
  18. </view>
  19. </scroll-view>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. import {
  25. getProductslist,
  26. getProductHot
  27. } from '@/api/store.js';
  28. export default {
  29. name: 'navTab',
  30. props: {
  31. tabTitle: {
  32. type: Array,
  33. default: []
  34. }
  35. },
  36. data() {
  37. return {
  38. tabClick: 0, //导航栏被点击
  39. isLeft: 0, //导航栏下划线位置
  40. isWidth: 0, //每个导航栏占位
  41. tabLeft:0,
  42. swiperIndex:0,
  43. childIndex:0,
  44. childID:0
  45. };
  46. },
  47. created() {
  48. var that = this
  49. // 获取设备宽度
  50. uni.getSystemInfo({
  51. success(e) {
  52. that.isWidth = e.windowWidth / 5
  53. }
  54. })
  55. },
  56. methods: {
  57. // 导航栏点击
  58. longClick(index){
  59. this.childIndex = 0;
  60. if(this.tabTitle.length>5){
  61. var tempIndex = index - 2;
  62. tempIndex = tempIndex<=0 ? 0 : tempIndex;
  63. this.tabLeft = (index-2) * this.isWidth //设置下划线位置
  64. }
  65. this.tabClick = index //设置导航点击了哪一个
  66. this.isLeft = index * this.isWidth //设置下划线位置
  67. let obj = {
  68. type:'big', //大标题
  69. index:index
  70. }
  71. this.parentEmit(obj)
  72. // this.$parent.currentTab = index //设置swiper的第几页
  73. },
  74. // 导航子类点击
  75. childTab(tabClick,index){
  76. this.childIndex = index
  77. let obj = {
  78. parentIndex:tabClick,
  79. childIndex:index,
  80. type:'small' //小标题
  81. }
  82. this.parentEmit(obj)
  83. },
  84. parentEmit(data){
  85. this.$emit('changeTab', data);
  86. }
  87. }
  88. }
  89. </script>
  90. <style lang="scss" scoped>
  91. .navTabBox {
  92. width: 100%;
  93. color: rgba(255, 255, 255, 1);
  94. .click {
  95. color: white;
  96. }
  97. .longTab {
  98. width: 100%;
  99. /* #ifdef H5 */
  100. padding-bottom: 20rpx;
  101. /* #endif */
  102. /* #ifdef MP */
  103. padding-top: 12rpx;
  104. padding-bottom: 12rpx;
  105. /* #endif */
  106. .longItem{
  107. height: 50upx;
  108. display: inline-block;
  109. line-height: 50upx;
  110. text-align: center;
  111. font-size: 30rpx;
  112. &.click{
  113. font-weight: bold;
  114. }
  115. }
  116. .underlineBox {
  117. height: 3px;
  118. width: 20%;
  119. display: flex;
  120. align-content: center;
  121. justify-content: center;
  122. transition: .5s;
  123. .underline {
  124. width: 33rpx;
  125. height: 4rpx;
  126. background-color: white;
  127. }
  128. }
  129. }
  130. }
  131. .child-box{
  132. width: 100%;
  133. position: relative;
  134. // height: 152rpx;
  135. background-color: #fff;
  136. /* #ifdef H5 */
  137. box-shadow: 0 2px 5px 1px rgba(0, 0, 0, 0.02);
  138. /* #endif */
  139. /* #ifdef MP */
  140. box-shadow: 0 2rpx 3rpx 1rpx #f9f9f9;
  141. /* #endif */
  142. .wrapper{
  143. display: flex;
  144. align-items: center;
  145. padding: 20rpx 0;
  146. background: #fff;
  147. /* #ifdef H5 */
  148. //box-shadow: 0 2px 5px 1px rgba(0, 0, 0, 0.06);
  149. /* #endif */
  150. }
  151. .child-item{
  152. flex-shrink: 0;
  153. width:140rpx;
  154. display: flex;
  155. flex-direction: column;
  156. align-items: center;
  157. justify-content: center;
  158. margin-left: 10rpx;
  159. image{
  160. width: 90rpx;
  161. height: 90rpx;
  162. border-radius: 50%;
  163. }
  164. .txt{
  165. font-size: 24rpx;
  166. color: #282828;
  167. text-align: center;
  168. margin-top: 10rpx;
  169. }
  170. &.on{
  171. image{
  172. border: 1px solid $theme-color-opacity;
  173. }
  174. .txt{
  175. color: $theme-color;
  176. }
  177. }
  178. }
  179. }
  180. </style>