1.交互反馈
1.1 消息提示框
1)显示消息提示框
使用wx.showToast(OBJECT)显示消息提示框,参数说明如下:
icon的有效值如下:
- success:icon的默认值,用于显示成功的图标,title文本最多显示7个汉字长度
- loading:显示加载图标
- none:不显示图标,title文本最多可显示两行
示例代码如下:
wx.showToast({
title: '',
icon: '',
image: '',
duration: 0,
mask: true,
success: function(res) {},
fail: function(res) {},
complete: function(res) {},
})
2)关闭消息提示框
虽然消息提示框可在指定时间后自动消失,但小程序也可使用wx.hideToast()提前关闭消息提示框
1.2 加载提示框
1)显示加载提示框
使用wx.showLoading(OBJECT)显示加载提示框,参数说明如下:
示例代码如下:
wx.showLoading({
title: '',
mask: true,
success: function(res) {},
fail: function(res) {},
complete: function(res) {},
})
2)关闭加载提示框
使用wx.hideLoading()关闭加载提示框,示例代码如下:
setTimeout(function(){
wx.hideLoading()
},2000) //两秒内关闭提示框
1.3 模态弹窗
使用wx.showModal(OBJECT)显示模态弹窗,参数说明如下:
success()返回参数说明如下:
示例代码:
wx.showModal({
title: '',
content: '',
showCancel: true, //决定是否有“取消”按钮
cancelText: '',
cancelColor: '',
confirmText: '',
confirmColor: '',
success: function(res) {},
fail: function(res) {},
complete: function(res) {},
})
1.4 操作菜单
使用wx.showActionSheet(OBJECT)先是从底部浮出来的操作菜单,参数说明如下:
success()返回参数说明如下:
示例代码格式:
wx.showActionSheet({
itemList: [],
itemColor: '',
success: function(res) {},
fail: function(res) {},
complete: function(res) {},
})
2.导航条设置
2.1 当前页面标题设置
使用wx.setNavigationBarTitle(OBJECT)动态设置当前页面的标题,参数说明如下:
示例代码如下:
wx.setNavigationBarTitle({
title: '',
success: function(res) {},
fail: function(res) {},
complete: function(res) {},
})
2.2 导航条加载动画
小程序分别使用wx.showNavigationBarLoading()和wx.hideNavigationBarLoading()在当前页面显示或隐藏导航条加载动画。
2.3 导航条颜色设置
使用wx.setNavigationBarColor(OBJECT)设置导航条颜色,参数说明如下:
animation.timingFunc的有效值如下:
示例代码如下:
wx.setNavigationBarColor({
frontColor: '#ffffff',
backgroundColor: '#ff0000',
animation: {
duration:400,
timingFunc:'easeIn'
},
success: function(res) {},
fail: function(res) {},
complete: function(res) {},
})
3.tarBar设置
3.1 tabBar标记
1)设置tabBar标记
使用wx.setTabBarBadge(OBJECT)为tabBar某一项的右上角添加文本,参数说明如下:
示例代码如下:
wx.setTabBarBadge({
index: 0,
text: '',
success: function(res) {},
fail: function(res) {},
complete: function(res) {},
})
2)移除tabBar标记
使用wx.removeTabBarBadge(OBJECT)移除tabBar某一项右上角文本,参数说明如下:
示例代码如下:
wx.removeTabBarBadge({
index: 0,
success: function(res) {},
fail: function(res) {},
complete: function(res) {},
})
3.2 tabBar红点
1)显示tabBar红点
使用wx.showTabBarRedDot(OBJECT)显示tabBar某一项的右上角的红点,参数说明如下:
2)隐藏tabBar红点
使用wx.hideTabBarRedDot(OBJECT)隐藏tabBar某一项右上角的红点,参数说明如下:
3.3 onTabItemTap()
在小程序中,单击tabBar中的任一tab都会触发onTabItemTap(item),示例代码如下:
onTabItemTap(item){
console.log(item.index)
console.log(item.pagePath)
console.log(item.text)
}
3.4 设置tabBar样式
1)设置tabBar整体样式
使用wx.setTabBarStyle(OBJECT)动态设置tabBar的整体样式,参数说明如下:
示例代码如下:
wx.setTabBarStyle({
color: '',
selectedColor: '',
backgroundColor: '',
borderStyle: '',
success: function(res) {},
fail: function(res) {},
complete: function(res) {},
})
2)设置tabBar单项样式
使用wx.setTabBarItem(OBJECT)动态设置tabBar某一项的内容,参数说明如下:
示例代码如下:
wx.setTabBarItem({
index: 0,
text: '',
iconPath: '',
selectedIconPath: '',
success: function(res) {},
fail: function(res) {},
complete: function(res) {},
})
3.5 显示与隐藏tabBar
1)显示tabBar
使用wx.showTabBar(OBJECT)显示tabBar,参数说明如下:
2)隐藏tabBar
使用wx.hideTabBar(OBJECT)隐藏tabBar,参数说明如下:
综合应用示例代码:
<!--wxml-->
<view class="title">3.tabBar设置</view>
<view class="demo-box">
<view class="title">1)右上角文本设置</view>
<button type="primary" size="mini" bindtap="setText">添加文本</button>
<button type="primary" size="mini" bindtap="removeText">取消文本</button>
</view>
<view class="demo-box">
<view class="title">2)右上角红点设置</view>
<button type="primary" size="mini" bindtap="showRedDot">添加红点</button>
<button type="primary" size="mini" bindtap="hideRedDot">取消红点</button>
</view>
<view class="demo-box">
<view class="title">3)tabBar样式设置</view>
<button type="primary" size="mini" bindtap="setBarStyle">整体设置</button>
<button type="primary" size="mini" bindtap="setColor">单项设置</button>
<button type="primary" size="mini" bindtap="resetBarStyle">还原设置</button>
</view>
<view class="demo-box">
<view class="title">4)tabBar的显示与隐藏</view>
<button type="primary" size="mini" bindtap="showTabBar">显示tabBar</button>
<button type="primary" size="mini" bindtap="hideTabBar">隐藏tabBar</button>
</view>
/*wxss*/
button{
margin: 10rpx;
}
//js
//js
Page({
//设置文本
setText:function(){
wx.setTabBarBadge({
index: 1,
text: '99',
})
},
//取消文本
removeText:function(){
wx.removeTabBarBadge({
index: 1,
})
},
//显示红点
showRedDot:function(){
wx.showTabBarRedDot({
index: 1,
})
},
//隐藏红点
hideRedDot:function(){
wx.hideTabBarRedDot({
index: 1,
})
},
//设置整体样式
setBarStyle:function(){
wx.setTabBarStyle({
color: '#ff0000',
selectedColor: '#0000ff',
})
},
//设置单项样式
setColor:function(){
wx.setTabBarItem({
index: 1,
text: 'tabBar例题',
iconPath: '/icons/index.png',
selectedIconPath: '/icons/index_selected.png',
})
},
//还原tabBar样式
resetBarStyle:function(){
wx.setTabBarItem({
index: 1,
text: 'tabBar例题',
iconPath: "/icons/zujian.png",
selectedIconPath:"/icons/zujian_selected.png",
})
wx.setTabBarStyle({
color:"#000000",
selectedColor:"#laad19"
})
},
//显示tabBar
showTabBar:function(){
wx.showTabBar({})
},
//隐藏tabBar
hideTabBar:function(){
wx.hideTabBar({ })
}
})
4.页面导航
4.1 跳转到新页面
使用wx.navigateTo(OBJECT)保留当前页面,并在当前页面上方打开应用内指定的新页面。在这种打开方式下可单击新页面左上角返回按钮或使用wx.navigateBack()接口返回到原页面。参数说明如下:
示例代码如下:
wx.navigateTo({
url: 'test?id=123', //表示跳转到test页面,并且带参数id=123
})
//test页面
Page({
onLoad:function(option){
console.log(option.id) //在跳转的test页面可以通过onLoad()函数获得参数值
}
})
ps:小程序规定页面路径最多只能打开10层
4.2 返回指定页面
使用wx.navigateBack(OBJECT)关闭当前页面,返回上一页面或多级页面,参数说明如下:
举例理解delta:
4.3 当前页面重定向
使用wx.redirectTo(OBJECT)关闭当前页面,重定向到应用内的某个页面,参数说明如下:
示例代码如下:
wx.redirectTo({
url: 'test?id=1',
success: function(res) {},
fail: function(res) {},
complete: function(res) {},
})
ps:此时无法返回打开前的原页面
4.4 重启页面
使用wx.reLaunch(OBJECT)关闭所有页面,重新打开到应用内的某个页面,参数说明如下:
示例代码如下:
wx.reLaunch({
url: 'test?id=',
})
4.5 切换tabBar页面
使用wx.switchTab(OBJECT)跳转到指定的tabBar页面,并关闭其他页面,参数说明如下:
示例代码如下:
wx.switchTab({
url: '/index',
success: function(res) {},
fail: function(res) {},
complete: function(res) {},
})
ps:使用这个切换的页面是在app.json的tabBar属性中声明过的页面
6.页面位置
使用wx.pageScrollTo(OBJECT)将页面滚动到目标位置,参数说明如下:
示例代码如下:
wx.pageScrollTo({
scrollTop: 0,
duration: 0,
})
综合应用:
316页
8.下拉刷新
8.1 监听下拉刷新
在Page()中定义了onPullDownRefresh()函数用于监听当前页面的用户下拉刷新事件,示例代码如下:
onPullDownRefresh:function(){
console.log('正在下拉刷新')
}
测试时可在json文件中配置,相关代码如下:
{
"enablePullDownRefresh":true,
}
8.2 开始下拉刷新
使用wx.startPullDownRefresh(OBJECT)触发下拉刷新动画,效果与用户手动刷新一致,参数说明如下:
示例代码如下:
wx.startPullDownRefresh({
success: function(res) {},
fail: function(res) {},
complete: function(res) {},
})
8.3 停止下拉刷新
使用wx.stopPullDownRefresh()停止当前页面的下拉刷新,示例代码如下:
Page({
onPullDownRefresh:function(){ //当其监听并处理完数据后可使用该接口停止下拉刷新动作
wx.stopPullDownRefresh()
}
})