uniapp小步伐自界说中心凸起样式底部tabbar

打印 上一主题 下一主题

主题 889|帖子 889|积分 2667

我自己写的自界说的tabbar结果图

废话少说咱们直接上代码,一步一步来
第一步:

找到根目次下的 pages.json 文件,在 tabBar 中把 custom 设置为 true,默认值是 false。list 中设置自界说的相关信息,
pagePath: 页面路径;
iconPath: 点击前显示的图片;
selectedIconPath: 点击后显示的图片;
text: 底部导航的名称。

第二步

在根目次下创建 components 文件夹用来存放自界说tabBar组件,小伙伴们可以自界说文件夹和文件的名称。记得命名要规范哦!!!

第三步

咱也不截图了,看了那么多的文章全他妈截图,咱直接上代码方便有必要的小伙伴利用!!!!
  1. <template>
  2.         <view class="tabbar-container flex items-center">
  3.                 <!-- isRound是中间凸出样式的标志,用来判断当前子级是否是凸出样式的 -->
  4.                 <view :class="!item.isRound ? 'square' : 'is-square'" v-for="(item, index) in tabbarList" :key="index" @click="tabbarChange(index, item)">
  5.                         <!-- 不是凸出的子级 -->
  6.                         <template v-if="!item.isRound">
  7.                                 <view class="item-top flex justify-center flex-col">
  8.                                         <u-image :src="active == index ? item.selectedIconPath : item.iconPath" :width="item.width"
  9.                                                 :height="item.height"></u-image>
  10.                                 </view>
  11.                                 <view class="item-bottom" :class="{'active': active === index}">
  12.                                         <text>{{ item.text }}</text>
  13.                                 </view>
  14.                         </template>
  15.                         <!-- 凸出的子级 -->
  16.                         <template v-else>
  17.                                 <view class="flex flex-col justify-center items-center center-round">
  18.                                         <view class="flex flex-col justify-center items-center">
  19.                                                 <u-image :src="active == index ? item.selectedIconPath : item.iconPath" :width="item.width"
  20.                                                         :height="item.height"></u-image>
  21.                                                 <text :class="{'round-active': active === index}">{{ item.text }}</text>
  22.                                         </view>
  23.                                 </view>
  24.                         </template>
  25.                 </view>
  26.         </view>
  27. </template>
  28. <script>
  29.         export default {
  30.                 name: 'Tabbar',
  31.                 props: {
  32.                         tabbarIndex: {
  33.                                 type: Number,
  34.                                 default: 0
  35.                         }
  36.                 },
  37.                 data() {
  38.                         return {
  39.                                 // tabbar列表,配置自定义用到的属性字段
  40.                                 tabbarList: [{
  41.                                                 pagePath: "/pages/homePage/index",
  42.                                                 iconPath: "/static/images/tabbar/index.png",
  43.                                                 selectedIconPath: "/static/images/tabbar/index-selected.png",
  44.                                                 text: "首页",
  45.                                                 width: '45rpx',
  46.                                                 height: '41rpx',
  47.                                                 isRound: false
  48.                                         },
  49.                                         {
  50.                                                 pagePath: "/pages/information/index",
  51.                                                 iconPath: "/static/images/tabbar/data.png",
  52.                                                 selectedIconPath: "/static/images/tabbar/data-selected.png",
  53.                                                 text: "数据",
  54.                                                 width: '44rpx',
  55.                                                 height: '43rpx',
  56.                                                 isRound: false
  57.                                         },
  58.                                         {
  59.                                                 pagePath: "/pages/aiRecommend/index",
  60.                                                 iconPath: "/static/images/tabbar/ai-recommend.png",
  61.                                                 selectedIconPath: "/static/images/tabbar/ai-recommend.png",
  62.                                                 text: "智能推荐",
  63.                                                 width: '120rpx',
  64.                                                 height: '120rpx',
  65.                                                 isRound: true
  66.                                         },
  67.                                         {
  68.                                                 pagePath: "/pages/vip/index",
  69.                                                 iconPath: "/static/images/tabbar/vip.png",
  70.                                                 selectedIconPath: "/static/images/tabbar/vip-selected.png",
  71.                                                 text: "会员",
  72.                                                 width: '39rpx',
  73.                                                 height: '37rpx',
  74.                                                 isRound: false
  75.                                         },
  76.                                         {
  77.                                                 pagePath: "/pages/center/index",
  78.                                                 iconPath: "/static/images/tabbar/center.png",
  79.                                                 selectedIconPath: "/static/images/tabbar/center-selected.png",
  80.                                                 text: "我的",
  81.                                                 width: '40rpx',
  82.                                                 height: '41rpx',
  83.                                                 isRound: false
  84.                                         },
  85.                                 ],
  86.                                 active: 0,
  87.                                 isRound: false,
  88.                         }
  89.                 },
  90.                 mounted() {
  91.                         this.active = this.tabbarIndex
  92.                 },
  93.                 methods: {
  94.                         tabbarChange(index, item) {
  95.                                 this.$emit('updateTabbar', index)
  96.                                 uni.switchTab({
  97.                                         url: item.pagePath
  98.                                 });
  99.                         }
  100.                 }
  101.         }
  102. </script>
  103. <style lang="scss" scoped>
  104.         .tabbar-container {
  105.                 width: 100%;
  106.                 height: 132rpx;
  107.                 position: fixed;
  108.                 bottom: 0;
  109.                 .square {
  110.                         width: 100%;
  111.                         height: 130rpx;
  112.                         background: #FFFFFF;
  113.                         display: flex;
  114.                         flex-direction: column;
  115.                         align-items: center;
  116.                         justify-content: center;
  117.                         .round-active {
  118.                                 color: #207BDB;
  119.                         }
  120.                 }
  121.                 .square .item-top {
  122.                         width: 45rpx;
  123.                         height: 45rpx;
  124.                 }
  125.                 .is-square {
  126.                         width: 100%;
  127.                         height: 130rpx;
  128.                         background: #FFFFFF;
  129.                         display: flex;
  130.                         flex-direction: column;
  131.                         align-items: center;
  132.                         justify-content: center;
  133.                         .round-active {
  134.                                 color: #207BDB;
  135.                         }
  136.                 }
  137.                 .center-round {
  138.                         width: 203rpx;
  139.                         height: 130rpx;
  140.                         background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMsAAAAlCAYAAADstR3BAAADx0lEQVR4nO3deajnUxjH8dc1Y2zDzFjGvkZkKcofpNGgTCl/oGRfyto0iJJQypa1KJT9H7KUJYSsDRFlGqQUyi4kDVe2y1wdnqmZ3HSv+1vOOd/zrl/3d3/3j/s8p+/n9zzPOc85Z2R8fFxjIGyHvbEXdsC28dkcrIeNMAM/4s8waDbWxk/4HSvwDT7HZ3gfb+O9+HujjzSx9IcR7IGDsRAHYpM+/r8xvIOleAmvhMAaPaSJpXekqHAAjsCRETWGxW94EY/hcXxXwfgOnSaW6bMbTsGJ2CpD+1LUeRr3xs+xDGwqkiaW/8eMiCDnYEFBdn+Le3ArvsjAnqJoYpkaqeA+M0QyzDRruoxFinYdlhXsx0BpYpkcacZqCc7rc6E+DFJqdgXeqMyvntPE8t/MDpFciLk5G9oDnsXFWF68J32iiWVi1oqi/UpsmaOBfWIlHsJFsY7TWI0mln+zfxTA++Rm2AD5BVfjxnjfeTSxrMH8eDiOj0XFBp/EZMaTbSz+STe6zkikXKl15IQmlDVIbTlP4OGOpaMT0vXIkh6Gu3BIBrbkTupLuyDWaTpJVyNLih5n4N0mlEmTZgPvxlNdjTJdjCzzo/XjsAxsKZXvcToe7ZLTXYssi6I7twllemyMR3A71i/ZkanQFbHMxDV4BltkYE8tpFT2LezZBWe7kIZtjQcKa3gsjZ9DOPfX7GTtkWVBtG80ofSXlIrdh9tiZ2eV1CyWVIC+gM0ysKUrnI3nKmw2/ZsaxZL2mtyEOzArA3u6RtpG/WZsq66K2mqWeXgQh2ZgS9cZxXGxLlMFNUWWnWJPRhNKHmwY+//Pr8WhWiJLmrp8vk0LZ8u10fZfNDWIZVe82gr57LkKl5bsQOlp2FF4rQmlCC6JdZg5pTpQamRJ+fDNODUDWxpT41OcFAcBFkWJkWVRdAs3oZTJ9ng5vuw2KMmDkiLLprgBJ2dgS6M3fIzF0bOXPSVEllmx6ejDJpTq2DGOYkpi2T1353KOLGmD1jG4HDtnYE+jv/wRXReX5Xo2c65iOShOF9kvA1sagyVduXF91DSjOY19TmJZB8fi3LjHpNFtVsSRVLfg6xxGIgexpP0mp+GstgLfmIBV5zLfGXfPrBzWIA1LLNvgcBwdF/20I5kak+GrOJYpief1qHMGxqDEkmY99o1NWAvjqrhGYzr8EOs1S0M4ae3t136OaBLLvNV+H52iWufGavqqVzrIYPO4LzEJZJdocqz9UO3G8EnP7Qfx+iju3fwyZtbSc51qoDR5kN6nm9EmS3quZ2LdvwC4r+EYTWev0AAAAABJRU5ErkJggg==');
  141.                         background-position: center top;
  142.                         background-size: 100% 37rpx;
  143.                         background-repeat: no-repeat;
  144.                         position: absolute;
  145.                         margin-top: -72rpx;
  146.                         text {
  147.                                 font-weight: 500;
  148.                                 font-size: 20rpx;
  149.                                 color: #555B66;
  150.                                 position: absolute;
  151.                                 top: 117rpx;
  152.                         }
  153.                 }
  154.                 .item-bottom {
  155.                         font-weight: 500;
  156.                         font-size: 20rpx;
  157.                         color: #555B66;
  158.                         margin-top: 14rpx;
  159.                 }
  160.                 .active {
  161.                         color: #207BDB;
  162.                 }
  163.         }
  164. </style>
复制代码
  留意:小步伐背景图无法利用当地图片,要么转成base64,要么把图片存到服务器上,然后调用接口把图片路径返回。推荐第二种方法。
  我写的有点繁琐,中心凸起的样式应该为一整块,可是ui只给了顶部的白色图片导致写的代码有点多。不外大要的思绪差不多都是这样,小伙伴们也可以参考其他博主写的文章的思绪去编写自界说组件。

这是我用到的图片,小伙伴们可以下载下来共同代码进行利用。










第四步

在必要的页面中引用自界说组件
  1. <template>
  2.         <Tabbar :tabbar-index="tabbarIndex" @updateTabbar="updateTabbar"></Tabbar>
  3. </template>
  4. <script>
  5.         import Tabbar from "@/components/tabbar/tabbar.vue"
  6.         components: { Tabbar },
  7.         data() {
  8.                 return {
  9.                         tabbarIndex: 4,
  10.                 }
  11.         },
  12.         methods: {
  13.                 updateTabbar(e) {
  14.                         this.tabbarIndex = e
  15.                 },
  16.         }
  17. </script>
复制代码
有需求的小伙伴们可以试试,我写的并不是很完美但是大要的思绪是这样的,小伙伴们可以根据自己项目的需求进行改动。此文章可作为参考利用,希望能帮到有需求的小伙伴!!!!
假如有效的话就点击收藏起来吧!!!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

饭宝

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表