微信小程序进阶
前言:在开发微信小程序的过程中 微信小程序的底部与顶部默认自带的样式并不能满足日常所需,那么关于自定义顶部与自定义底部的需求也就来了。
1.自定义顶部navigation
1.1 全局自定义顶部
所有的页面 均使用自定义 顶部,自带顶部全局配置失效 app.json"window": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "Weixin",
"navigationBarTextStyle": "black",
//全局自定义顶部
"navigationStyle": "custom"
},1.2 单个页面自定义顶部
针对单个页面的自定义顶部配置 页面.json"navigationStyle": "custom"1.3 创建自定义 top-bar 组件
1.根目录下/components/top-bar 目录中 右键创建 topbar Component组件
2.在父页面 的 json文件中注册 自定义组件
{
"usingComponents": {
"top-bar":"/components/top-bar/top-bar"
},
"navigationStyle": "custom"
}3.在父页面的 wxml 文件中使用该组件
<!--index.wxml-->
<view class=&#34;container&#34;>
<top-bar title=&#34;首页&#34;></top-bar>
</view>4.完善 top-bar 组件样式,确定其高度与位置信息
[*]top-bar.js
// components/top-bar/top-bar.js
Component({
/**
* 组件的属性列表
*/
properties: {
title:{
type:String,
default:&#39;标题&#39;
}
},
/**
* 组件的初始数据
*/
data: {
topBarHeight:0,//顶部盒子总高度
},
/**
* 组件的方法列表
*/
methods: {
getNavHeight(){
// 获取设备信息
const sysInfo = wx.getSystemInfoSync();
// 导航栏总高度 = 状态栏+44px
const TopBarheight = sysInfo.statusBarHeight+44;
//异步更新数据
wx.nextTick(()=>{
this.setData({
topBarHeight:TopBarheight,
})
})
}
},
created(){
this.getNavHeight()
},
})
[*]top-bar.wxml
<!--components/top-bar/top-bar.wxml-->
<view class=&#34;nav&#34; style=&#34;height:{{topBarHeight}}px;padding: {{topBarHeight}}rpx 0 0 0;&#34;>
<view>
{{title}}
</view>
</view>
[*]top-bar.wxss
/* components/top-bar/top-bar.wxss */
.nav{
box-sizing: border-box;
background-color: #f1f1f1;
}5.完整父页面中的配置
[*]index.json
{
&#34;usingComponents&#34;: {
&#34;top-bar&#34;:&#34;/components/top-bar/top-bar&#34;
},
&#34;navigationStyle&#34;: &#34;custom&#34;
}
[*]index.wxml
<!--index.wxml-->
<view class=&#34;container&#34;>
<!-- 顶部组件 -->
<top-bar title=&#34;大众点评上首页&#34;></top-bar>
<!-- 内容区域 -->
<view class=&#34;content&#34; >
<viewwx:for=&#34;{{100}}&#34;>
{{item}}
</view>
</view>
</view>
[*]index.wxss
/**index.wxss**/
.container{
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
}
.content{
flex: 1;
width: 100%;
background-color: #f60;
overflow-y: scroll;
}6.最终展示效果
2.自定义底部TabBar
2.1 全局关闭 默认tabBar 配置,开启自定义tabBar
注意: 需要注意的是,就算开启了自定义 tabBar 配置,依旧需要保留全局的 tabBar 的list 配置【这样做是为了 可以通过wx.switchTab() 方法可以跳转到 该页面,该页面依旧为 tabBar 】&#34;tabBar&#34;: {
&#34;custom&#34;: true, //开启自定义tabBar默认tabBar 样式失效
&#34;color&#34;: &#34;#d4d4d2&#34;,
&#34;selectedColor&#34;: &#34;#e26c4f&#34;,
&#34;borderStyle&#34;: &#34;white&#34;,
&#34;list&#34;: [
{
&#34;pagePath&#34;: &#34;pages/index/index&#34;,
&#34;text&#34;: &#34;首页&#34;,
&#34;iconPath&#34;: &#34;/images/index.png&#34;,
&#34;selectedIconPath&#34;: &#34;/images/index_active.png&#34;
},
{
&#34;pagePath&#34;: &#34;pages/add/add&#34;,
&#34;text&#34;: &#34;发布&#34;,
&#34;isCenter&#34;:true, //这里是为了表明该子项 是显示在中间的圆里面
&#34;iconPath&#34;: &#34;/images/add.png&#34;,
&#34;selectedIconPath&#34;: &#34;/images/add.png&#34;
},
{
&#34;pagePath&#34;: &#34;pages/discount/discount&#34;,
&#34;text&#34;: &#34;找优惠&#34;,
&#34;iconPath&#34;: &#34;/images/discount.png&#34;,
&#34;selectedIconPath&#34;: &#34;/images/discount_active.png&#34;
},
{
&#34;pagePath&#34;: &#34;pages/shop/shop&#34;,
&#34;text&#34;: &#34;找好店&#34;,
&#34;iconPath&#34;: &#34;/images/shop.png&#34;,
&#34;selectedIconPath&#34;: &#34;/images/shop_active.png&#34;
},
{
&#34;pagePath&#34;: &#34;pages/mine/mine&#34;,
&#34;text&#34;: &#34;我的&#34;,
&#34;iconPath&#34;: &#34;/images/mine.png&#34;,
&#34;selectedIconPath&#34;: &#34;/images/mine_active.png&#34;
}
]
}2.2 创建公共组件components/tab-bar
[*]tab-bar.wxml
<!--components/tab-bar/tab-bar.wxml-->
<view class=&#34;bar&#34;>
<block wx:for=&#34;{{tabbar.list}}&#34; wx:key=&#34;index&#34;>
<!-- 如果是中间的 -->
<block wx:if=&#34;{{item.isCenter}}&#34;>
<view class=&#34;center&#34; style=&#34;background: {{tabbar.selectedColor}};&#34; data-page=&#34;{{item.pagePath}}&#34; bindtap=&#34;toPage&#34;>
<image class=&#34;add-icon&#34; src=&#34;{{item.iconPath}}&#34; mode=&#34;&#34; />
</view>
</block>
<!-- 如果不是中间的 处理 -->
<block wx:else>
<!-- 如果不选中 -->
<view wx:if=&#34;{{index!=active}}&#34; bindtap=&#34;toPage&#34; data-page=&#34;{{item.pagePath}}&#34; class=&#34;item&#34; style=&#34;color:{{tabbar.color}}&#34;>
<image class=&#34;icon&#34; src=&#34;{{item.iconPath}}&#34;></image>
<view>
{{item.text}}
</view>
</view>
<!-- 如果选中 -->
<view wx:else class=&#34;item&#34; bindtap=&#34;toPage&#34; data-page=&#34;{{item.pagePath}}&#34; style=&#34;color:{{tabbar.selectedColor}}&#34;>
<image class=&#34;icon&#34; src=&#34;{{item.selectedIconPath}}&#34;></image>
<view>
{{item.text}}
</view>
</view>
</block>
</block>
</view>
[*]tab-bar.js
这里的data 里面设置了一个默认数据 tabBar 就是直接复制 的 app.json 里面的 tabBar 配置的 json对象// components/tab-bar/tab-bar.js
Component({
/**
* 组件的属性列表
*/
properties: {
//当前选中的是哪个tabBar
active:{
type:Number,
default:0,
}
},
/**
* 组件的初始数据
*/
data: {
tabbar:{
&#34;color&#34;: &#34;#d4d4d2&#34;,
&#34;selectedColor&#34;: &#34;#e26c4f&#34;,
&#34;borderStyle&#34;: &#34;white&#34;,
&#34;list&#34;: [
{
&#34;pagePath&#34;: &#34;pages/index/index&#34;,
&#34;text&#34;:&#34;首页&#34;,
&#34;iconPath&#34;: &#34;/images/index.png&#34;,
&#34;selectedIconPath&#34;: &#34;/images/index_active.png&#34;
},
{
&#34;pagePath&#34;: &#34;pages/discount/discount&#34;,
&#34;text&#34;:&#34;找优惠&#34;,
&#34;iconPath&#34;: &#34;/images/discount.png&#34;,
&#34;selectedIconPath&#34;: &#34;/images/discount_active.png&#34;
},
{
&#34;pagePath&#34;: &#34;pages/add/add&#34;,
&#34;text&#34;: &#34;发布&#34;,
&#34;isCenter&#34;:true,
&#34;iconPath&#34;: &#34;/images/add.png&#34;,
&#34;selectedIconPath&#34;: &#34;/images/add.png&#34;
},
{
&#34;pagePath&#34;: &#34;pages/shop/shop&#34;,
&#34;text&#34;:&#34;找好店&#34;,
&#34;iconPath&#34;: &#34;/images/shop.png&#34;,
&#34;selectedIconPath&#34;: &#34;/images/shop_active.png&#34;
},
{
&#34;pagePath&#34;: &#34;pages/mine/mine&#34;,
&#34;text&#34;:&#34;我的&#34;,
&#34;iconPath&#34;: &#34;/images/mine.png&#34;,
&#34;selectedIconPath&#34;: &#34;/images/mine_active.png&#34;
}
]
}
},
/**
* 组件的方法列表
*/
methods: {
//跳转页面
toPage(event){
wx.switchTab({
url: &#39;/&#39;+event.currentTarget.dataset.page
})
}
}
})
[*]tab-bar.wxss
/* components/tab-bar/tab-bar.wxss */
.bar{
width: 100%;
height: 50px;
background: #fff;
position: fixed;
bottom: 0;
display: flex;
justify-content: space-around;
border-top: 1px solid #eee;
}
.item{
flex: 1;
height: 50px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
font-size: 14px;
}
.icon{
width: 25px;
height: 25px;
}
.center{
position: relative;
top: -20px;
width: 50px;
height: 50px;
text-align: center;
line-height: 50px;
border-radius: 50%;
color: #fff;
}
.add-icon{
margin:12.5px;
width: 25px;
height: 25px;
}2.3 需要 使用tabBar的页面 导入该子组件
页面.json
{
&#34;usingComponents&#34;: {
&#34;top-bar&#34;:&#34;/components/top-bar/top-bar&#34;,
&#34;tab-bar&#34;:&#34;/components/tab-bar/tab-bar&#34;
}
}页面.wxml
<!--index.wxml-->
<view class=&#34;container&#34;>
<!-- 顶部组件 -->
<top-bar title=&#34;大众点评上首页&#34;></top-bar>
<!-- 内容区域 -->
<view class=&#34;content&#34; >
<!-- <viewwx:for=&#34;{{100}}&#34;>
{{item}}
</view> -->
</view>
</view>
<!-- 自定义底部tabBar2:表示当前第几个被选中 进入这个页面时,需要选中的数组索引-->
<tab-bar active=&#34;2&#34;></tab-bar>页面.wxss
/**index.wxss**/
.container{
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
}
.content{
flex: 1;
width: 100%;
background-color: #f60;
overflow-y: scroll;
padding-bottom: 50px;
}2.4 效果展示
3.外联样式的导入
利用@import “导入的样式文件相对路径” 可以导入公共的外联样式@import &#34;../../common/common.wxss&#34;;4.WeUI-微信官方团队开发UI组件库
官网文档链接: https://wechat-miniprogram.github.io/weui/docs/quickstart.html4.1 下载并在小程序中使用 UI组件库
小程序支持使用npm安装第三方包,要求开发者工具>=1.02,基础库版本>=2.2.1
npm i weui-miniprogram
或
yarn add weui-miniprogram4.2 在 app.wxss 中导入 组件库 样式文件
/**app.wxss**/
@import &#34;./miniprogram_npm/weui-miniprogram/weui-wxss/dist/style/weui.wxss&#34;;4.3 如果出现 控制台报错:依赖项 未被使用的报错
[*]解决方案: 关闭依赖项检测提示,并重启项目 project.config.json
&#34;setting&#34;: {
&#34;ignoreDevUnusedFiles&#34;: false,
&#34;ignoreUploadUnusedFiles&#34;: false,
}4.4 使用 weui 组件
[*]index.json 注册该组件
{
&#34;usingComponents&#34;: {
&#34;mp-dialog&#34;: &#34;weui-miniprogram/dialog/dialog&#34;
}
}
[*]index.wxml
<mp-dialog title=&#34;test&#34; show=&#34;{{true}}&#34; bindbuttontap=&#34;tapDialogButton&#34; buttons=&#34;{{buttons}}&#34;>
<view>test content</view>
</mp-dialog>
[*]index.js
Page({
data: {
buttons: [{text: &#39;取消&#39;}, {text: &#39;确认&#39;}]
}
} 完整的组件的使用文档请参考具体的组件的文档。
页:
[1]