微信小程序自定义tabbar组件点击对应区域显示凸起效果

本文介绍了如何在微信小程序中创建自定义tabbar组件,并实现点击区域显示凸起效果。通过创建template文件夹,包含wxml、wcss和js文件,以及在app.json和app.js中进行配置,实现tabbar组件的显示和隐藏。同时,提供了在不同页面引用该组件的步骤和代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

	今天呢,看到设计图,难搞啊,要自定义组件,是时候动动脑子了,哈哈。

我呢,方法用的比较笨,代码有点冗余了,还望体谅哈

在这里插入图片描述
在这里插入图片描述
首先呢,先创建template文件夹,里面包含wxml,wcss和 js

先来wxml代码吧

<view class='footer'>

  <view class='footer_list' data-current="{
  {0}}" bindtap="chooseImg">
    <image class="footer-image" hidden='{
  {curIdx===0}}' src="{
  {listInfo[0].imgUrl}}"></image>
    <view hidden='{
  {curIdx!==0}}' class="tab-img">
      <image class="footer-image2" src="{
  {listInfo[0].curUrl}}"></image>
    </view>
    <!-- 等于该id的话图标和名称就凸起来 -->
    <view class="footer-text">首页</view>
    <view hidden='{
  {curIdx!==0}}' class="footer-text2">首页</view>
  </view>

  <view class='footer_list' data-current="{
  {1}}" bindtap="chooseImg">
    <image class="footer-image" hidden='{
  {curIdx===1}}' src="{
  {listInfo[1].imgUrl}}"></image>
    <view hidden='{
  {curIdx!==1}}' class="tab-img">
      <image class="footer-image2" src="{
  {listInfo[1].curUrl}}"></image>
    </view>
    <view class="footer-text">分类</view>
    <view hidden='{
  {curIdx!==1}}' class="footer-text2">分类</view>
  </view>

  <view class='footer_list' data-current="{
  {2}}" bindtap="chooseImg">
    <image class="footer-image" hidden='{
  {curIdx===2}}' src="{
  {listInfo[2].img
### 实现中间凸起 Tabbar 导航栏 在 uni-app 开发的微信小程序中实现带有中间凸起按钮的底部导航栏,可以通过自定义 `tabBar` 并结合 CSS 和 JavaScript 来完成。以下是具体方法: #### HTML 结构 创建页面结构并引入必要的组件。 ```html <template> <view class="container"> <!-- 自定义 tabBar --> <view class="custom-tab-bar" :style="{ bottom: isTabFixed ? '0' : '-50px' }"> <navigator url="/pages/index/index" open-type="switchTab" hover-class="none" class="tab-item" :class="[currentPage === '/pages/index/index' ? 'active' : '']"> <image src="/static/tabbar/home.png"></image> <text>首页</text> </navigator> <navigator url="/pages/cart/cart" open-type="switchTab" hover-class="none" class="tab-item raised-button" @click="handleRaisedButtonClick()"> <image src="/static/tabbar/add.png"></image> </navigator> <navigator url="/pages/user/user" open-type="switchTab" hover-class="none" class="tab-item" :class="[currentPage === '/pages/user/user' ? 'active' : '']"> <image src="/static/tabbar/user.png"></image> <text>我的</text> </navigator> </view> <!-- 页面主体内容 --> <view class="page-content"> <!-- 这里放置页面的主要内容 --> </view> </view> </template> ``` #### 样式设计 通过 CSS 定义样式来使中间按钮呈现为圆形且突出显示。 ```css <style scoped> .custom-tab-bar { position: fixed; width: 100%; height: 50px; background-color: white; display: flex; justify-content: space-around; align-items: center; box-shadow: 0 -2px 8px rgba(0, 0, 0, .1); } .tab-item { text-align: center; font-size: 12px; } .raised-button { position: absolute; left: 50%; transform: translateX(-50%) translateY(-50%); border-radius: 50%; padding: 10px; background-color: #ff6f61; /* 颜色可根据需求调整 */ color: white; } </style> ``` #### JS逻辑处理 编写脚本控制当前选中的页面以及响应点击事件。 ```javascript <script> export default { data() { return { currentPage: getCurrentPages().pop().route, isTabFixed: true // 控制 tabBar 是否固定到底部 }; }, methods: { handleRaisedButtonClick() { console.log('Middle button clicked'); // 可在此处添加更多交互逻辑 } }, onLoad(options) { this.currentPage = options.page || '/'; } }; </script> ``` #### pages.json配置 确保在项目的 `pages.json` 文件内正确设置 tabBar 的路径和其他属性。 ```json { "path": "pages/index/index", "style": { "navigationBarTitleText": "首页", ... }, "tabBar": { "list": [ { ... }, // 对应于各个选项卡的信息对象数组 { "pagePath": "pages/middleButtonPage/middleButtonPage", // 中间按钮关联的页面路径 "iconPath": "/static/tabbar/add.png", "selectedIconPath": "/static/tabbar/add-active.png", "text": "" }, { ... } ], "color": "#9B9B9B", "selectedColor": "#3cc51f", "borderStyle": "black", "backgroundColor": "#ffffff" } } ``` 上述代片段展示了如何构建一个具有独特视觉效果的 tab bar,在其中央位置有一个特别设计过的按钮[^1]。此方案不仅适用于微信小程序环境下的应用开发,同时也能够很好地适配其他平台的需求[^2]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值