// index.js
// 获取应用实例
const app = getApp()
Page({
data: {
motto: 'Hello World',
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo'),
canIUseGetUserProfile: false,
canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
},
// 事件处理函数
bindViewTap() {
wx.navigateTo({
url: '../logs/logs'
})
},
onLoad() {
if (wx.getUserProfile) {
this.setData({
canIUseGetUserProfile: true
})
}
if (typeof this.getTabBar === 'function' &&
this.getTabBar()) {
this.getTabBar().setData({
selected: 0
})
}
},
getUserProfile(e) {
// 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
wx.getUserProfile({
desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
success: (res) => {
console.log(res)
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
})
},
getUserInfo(e) {
// 不推荐使用getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息
console.log(e)
this.setData({
userInfo: e.detail.userInfo,
hasUserInfo: true
})
}
})
微信小程序自定义tabbar中间突起样式
需积分: 0 177 浏览量
更新于2023-04-20
收藏 35KB ZIP 举报
微信小程序是一种轻量级的应用开发平台,由腾讯公司推出,主要应用于移动端,为用户提供便捷的本地服务和交互体验。在微信小程序中,`tabbar` 是一个基础且重要的组件,它通常位于页面底部,用于展示应用的主要功能模块,方便用户快速切换。本文将深入探讨如何在微信小程序中自定义`tabbar`,特别是实现中间突起的样式。
1. **自定义tabbar的基本步骤**
在微信小程序中,我们可以通过以下步骤来创建自定义的`tabbar`:
- 在`app.json`文件中配置全局的`tabBar`,指定其显示位置、颜色等基本属性。
- 接着,在`pages`目录下创建对应的页面,并在每个页面的`json`配置文件中设置页面的`navigationBarTitleText`,以显示在`tabbar`上。
- 编写`wxss`样式文件,实现自定义的`tabbar`样式,包括图标、文字、背景色等。
2. **中间突起的样式实现**
要实现`tabbar`中间一项突出的效果,我们可以利用CSS的伪类选择器和相对定位进行设计。为`tabbar`容器设置一个统一的高度和背景色,然后对每个`tab`项进行布局。通常情况下,`tabbar`有多个子项,可以使用`flex`布局进行水平分布。中间项可以通过以下方式突起:
- 使用`:nth-child()`或`:nth-of-type()`选择器选中中间项。
- 增加中间项的`z-index`值,使其在视觉上位于其他项之上。
- 设置`padding`或`margin`增加垂直间距,模拟突起效果。
- 对中间项的背景色、边框或其他样式进行特殊处理,增强视觉差异。
3. **代码示例**
以下是一个简单的代码示例,展示了如何创建一个包含中间突起`tabbar`的微信小程序页面:
在`app.json`:
```json
{
"tabBar": {
"custom": true,
"color": "#7A7E83",
"selectedColor": "#3cc51f",
"borderStyle": "black",
"list": [
{"pagePath": "pages/index/index", "text": "首页", "iconPath": "images/home.png", "selectedIconPath": "images/home-selected.png"},
{"pagePath": "pages/logs/logs", "text": "日志", "iconPath": "images/logs.png", "selectedIconPath": "images/logs-selected.png"},
{"pagePath": "pages/about/about", "text": "关于", "iconPath": "images/about.png", "selectedIconPath": "images/about-selected.png"}
]
}
}
```
在`custom-tabbar.wxss`:
```css
.custom-tabbar {
display: flex;
justify-content: space-around;
height: 50px;
background-color: #fff;
}
.custom-tabbar-item {
position: relative;
width: 33.3%;
text-align: center;
}
.custom-tabbar-item::before {
content: "";
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 2px;
border-radius: 50% 50% 0 0;
background-color: #3cc51f;
}
.custom-tabbar-item:nth-child(2) {
z-index: 2;
padding-bottom: 8px;
}
.custom-tabbar-item:nth-child(2)::before {
bottom: 0;
border-radius: 0;
}
.custom-tabbar-item img {
width: 24px;
height: 24px;
margin: 12px auto;
}
.custom-tabbar-item.selected {
color: #3cc51f;
}
```
4. **注意事项**
- 自定义`tabbar`时,需确保`app.json`中的`custom`属性设为`true`。
- 在不同尺寸的设备上,可能需要适配`tabbar`的样式,以保证在各种屏幕尺寸下的显示效果。
- 保持代码的可维护性和可扩展性,避免过度定制导致的复杂性。
通过以上介绍,你应该能理解如何在微信小程序中实现自定义的中间突起`tabbar`了。实际开发过程中,还需要根据具体需求调整细节,比如动态改变选中状态、添加动画效果等。记住,良好的用户体验是设计的核心,因此在自定义`tabbar`时,应确保其与整体应用风格一致,易于用户理解和操作。


woshiabc111
- 粉丝: 200
最新资源
- 基于BS架构的网站后台管理分析(1).docx
- 利用白板教学助推幼儿教育信息化探究(1).docx
- 网站规划与建设实训报告模板(1).doc
- 基于B2C电子商务背景的消费者信任度及购买行为研究(1).docx
- 企业软件新升级协议(标准版)(1).docx
- 信息化教育学习收获心得体会.doc
- (完整word版)VISION中文的使用说明(1).doc
- 计算机网络维护与管理方法(1).docx
- 大学毕业论文-—套类零件数控加工工艺分析、编程及数控仿真(1).doc
- 刍议工程机械中计算机技术的智能化、信息化运用(1).docx
- 浙江果品电子商务网站商业计划书(1).docx
- 基于TCPIP的网络文字聊天程序设计(1)(1).doc
- 认识计算机课件-PPT(1).ppt
- 电子商务职业规划(1).doc
- 对信息化时代下企业财务管理模式的探究(1).docx
- 试论基于PLC的液压传动组合机床电气控制系统设计方法(2).docx