tabNav.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <template>
  2. <view>
  3. <!-- #ifdef MP || APP-PLUS -->
  4. <view style="visibility: hidden;" :style="{ height: navHeight + 'rpx' }" v-if="isFixed"></view>
  5. <!-- #endif -->
  6. <view class="navTabBox tabNav" :class="{isFixed:isFixed}" :style="'background:'+bgColor+';margin-top:'+mbConfig+'rpx;color:'+txtColor+';top:'+isTop">
  7. <view class="longTab" :style='"width:"+mainWidth+"px"'>
  8. <scroll-view scroll-x="true" style="white-space: nowrap; display: flex;" scroll-with-animation :scroll-left="tabLeft" show-scrollbar="true">
  9. <view class="longItem" :style="'width:'+isWidth+'px;color:'+(index===tabClick ? activeColor : txtColor)" :data-index="index" :class="index===tabClick?'click':''" v-for="(item,index) in tabTitle" :key="index" :id="'id'+index" @click="longClick(index,item.store_category_id,item.pid)">{{item.cate_name}}</view>
  10. <view class="underlineBox" :style='"transform:translateX("+isLeft+"px);width:"+isWidth+"px"'>
  11. <view class="underline" :style="'background-color:'+activeColor"></view>
  12. </view>
  13. </scroll-view>
  14. </view>
  15. <navigator v-if="merId" :url="'/pages/store/home/index?id='+merId+'&type=2'" class="category" hover-class="none" :style="'background-color:'+bgColor+';color:'+txtColor">
  16. <text class="iconfont icon-fenlei3"></text>分类
  17. </navigator>
  18. <navigator v-else open-type='switchTab' url="/pages/goods_cate/goods_cate" class="category" hover-class="none" :style="'background-color:'+bgColor+';color:'+txtColor">
  19. <text class="iconfont icon-fenlei3"></text>分类
  20. </navigator>
  21. </view>
  22. </view>
  23. </template>
  24. <script>
  25. // +----------------------------------------------------------------------
  26. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  27. // +----------------------------------------------------------------------
  28. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  29. // +----------------------------------------------------------------------
  30. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  31. // +----------------------------------------------------------------------
  32. // | Author: CRMEB Team <admin@crmeb.com>
  33. // +----------------------------------------------------------------------
  34. import { getCateData } from '@/api/api.js';
  35. let app = getApp();
  36. export default {
  37. name: 'navTab',
  38. props: {
  39. // tabTitle: {
  40. // type: Array,
  41. // default: []
  42. // },
  43. dataConfig: {
  44. type: Object,
  45. default: () => {}
  46. },
  47. isFixed: {
  48. type: Boolean,
  49. default: false
  50. },
  51. merId: {
  52. type: String || Number,
  53. default: ''
  54. }
  55. },
  56. data() {
  57. return {
  58. tabTitle: [],
  59. tabClick: 0, //导航栏被点击
  60. isLeft: 0, //导航栏下划线位置
  61. isWidth: 0, //每个导航栏占位
  62. mainWidth: 0,
  63. tabLeft:0,
  64. swiperIndex:0,
  65. childIndex:0,
  66. childID:0,
  67. bgColor:this.dataConfig.bgColor.color[0].item,
  68. mbConfig:this.dataConfig.mbConfig.val * 2,
  69. txtColor:this.dataConfig.txtColor.color[0].item,
  70. activeColor: this.dataConfig.activeColor.color[0].item,
  71. fixedTop: 0,
  72. isTop: 0,
  73. navHeight: 38,
  74. diy_id: this.dataConfig.did,
  75. unique: this.dataConfig.timestamp,
  76. };
  77. },
  78. watch:{
  79. // #ifdef MP || APP-PLUS
  80. isFixed(nVal,oVal){
  81. if(nVal){
  82. this.isTop = (uni.getSystemInfoSync().statusBarHeight + 43) + 'px'
  83. }else{
  84. this.isTop = 0
  85. }
  86. }
  87. // #endif
  88. },
  89. created() {
  90. var that = this
  91. that.getCateData()
  92. // 获取设备宽度
  93. uni.getSystemInfo({
  94. success(e) {
  95. that.mainWidth = e.windowWidth
  96. that.isWidth = (e.windowWidth-65) / 4
  97. }
  98. })
  99. setTimeout((e) => {
  100. const query = uni.createSelectorQuery().in(this);
  101. query.select('.navTabBox').boundingClientRect(data => {
  102. that.navHeight = data.height * 2
  103. that.$emit('bindHeight', data)
  104. }).exec();
  105. }, 300)
  106. that.$nextTick(function() {
  107. uni.getSystemInfo({
  108. success: function(res) {
  109. that.windowHeight = res.windowHeight;
  110. }
  111. });
  112. })
  113. // #ifdef MP || APP-PLUS
  114. // this.isTop = (uni.getSystemInfoSync().statusBarHeight + 43) + 'px'
  115. // #endif
  116. // #ifdef H5
  117. this.isTop = 0
  118. // #endif
  119. },
  120. methods: {
  121. getCateData(){
  122. getCateData({
  123. diy_id: this.diy_id,
  124. unique: this.unique,
  125. }).then(res => {
  126. res.data.unshift({
  127. cate_name: '首页'
  128. });
  129. this.$set(this, 'tabTitle', res.data);
  130. });
  131. },
  132. // 导航栏点击
  133. longClick(index,id,fid){
  134. app.globalData.fid = fid;
  135. this.childIndex = 0;
  136. if(this.tabTitle.length>3){
  137. var tempIndex = index - 2;
  138. tempIndex = tempIndex<=0 ? 0 : tempIndex;
  139. this.tabLeft = (index-2) * this.isWidth //设置下划线位置
  140. }
  141. this.tabClick = index //设置导航点击了哪一个
  142. this.isLeft = index * this.isWidth //设置下划线位置
  143. let obj = {
  144. index:index,
  145. pid:id//自己的id
  146. }
  147. if(!this.merId){
  148. this.parentEmit(obj);
  149. }else{
  150. if(id){
  151. uni.navigateTo({
  152. url: '/pages/store/list/index?id='+id+'&mer_id='+this.merId
  153. });
  154. }else{
  155. uni.navigateTo({
  156. url: '/pages/store/home/index?id='+this.merId
  157. });
  158. }
  159. }
  160. },
  161. parentEmit(obj){
  162. this.$emit('changeTab', obj,this.tabTitle);
  163. }
  164. }
  165. }
  166. </script>
  167. <style lang="scss" scoped>
  168. .tabNav {
  169. padding-top: 10rpx;
  170. }
  171. .navTabBox {
  172. width: 100%;
  173. color: rgba(255, 255, 255, 1);
  174. position: relative;
  175. padding-bottom: 10rpx;
  176. &.isFixed {
  177. z-index: 10;
  178. position: fixed;
  179. left: 0;
  180. width: 100%;
  181. /* #ifdef H5 */
  182. top: 0;
  183. /* #endif */
  184. }
  185. .click {
  186. color: white;
  187. }
  188. .longTab {
  189. /* #ifdef H5 */
  190. padding-right: 106rpx;
  191. /* #endif */
  192. /* #ifdef MP || APP-PLUS */
  193. padding-right: 116rpx;
  194. /* #endif */
  195. .longItem{
  196. height: 50upx;
  197. display: inline-block;
  198. line-height: 50upx;
  199. text-align: center;
  200. font-size: 28rpx;
  201. color: #333333;
  202. max-width: 160rpx;
  203. white-space: nowrap;
  204. overflow: hidden;
  205. text-overflow: ellipsis;
  206. &.click{
  207. font-weight: bold;
  208. font-size: 30rpx;
  209. color: #E93323;
  210. }
  211. }
  212. .underlineBox {
  213. height: 3px;
  214. width: 20%;
  215. display: flex;
  216. align-content: center;
  217. justify-content: center;
  218. transition: .5s;
  219. .underline {
  220. width: 33rpx;
  221. height: 4rpx;
  222. }
  223. }
  224. }
  225. .category{
  226. position: absolute;
  227. right: 0;
  228. top: 10rpx;
  229. /* #ifdef H5 */
  230. width: 132rpx;
  231. /* #endif */
  232. /* #ifdef MP */
  233. width: 120rpx;
  234. /* #endif */
  235. /* #ifdef APP-PLUS */
  236. width: 150rpx;
  237. /* #endif */
  238. height: 50upx;
  239. line-height: 50upx;
  240. z-index: 3;
  241. padding: 0 15rpx 0 25rpx;
  242. .iconfont{
  243. font-size: 30rpx;
  244. margin-right:6rpx;
  245. }
  246. }
  247. }
  248. .child-box{
  249. width: 100%;
  250. position: relative;
  251. background-color: #fff;
  252. /* #ifdef H5 */
  253. box-shadow: 0 2px 5px 1px rgba(0, 0, 0, 0.02);
  254. /* #endif */
  255. /* #ifdef MP */
  256. box-shadow: 0 2rpx 3rpx 1rpx #f9f9f9;
  257. /* #endif */
  258. .wrapper{
  259. display: flex;
  260. align-items: center;
  261. padding: 20rpx 0;
  262. background: #fff;
  263. }
  264. .child-item{
  265. flex-shrink: 0;
  266. width:140rpx;
  267. display: flex;
  268. flex-direction: column;
  269. align-items: center;
  270. justify-content: center;
  271. margin-left: 10rpx;
  272. image{
  273. width: 90rpx;
  274. height: 90rpx;
  275. border-radius: 50%;
  276. }
  277. .txt{
  278. font-size: 24rpx;
  279. color: #282828;
  280. text-align: center;
  281. margin-top: 10rpx;
  282. }
  283. &.on{
  284. image{
  285. border: 1px solid $theme-color-opacity;
  286. }
  287. .txt{
  288. color: $theme-color;
  289. }
  290. }
  291. }
  292. }
  293. </style>