tabNav.vue 5.1 KB

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