一,定时跳转
setTimeout(function () {
wx.navigateTo({
url: '/pages/index/login',
})
}, 1500)
二,米转千米
Changedistance:function(str){
if(str > 1000){
str = (str/1000).toFixed(1)+'千米';
}else{
str = str+'米';
}
return str;
},
三,分钟转小时
ChangeHourMinutestr:function(str) {
if(str != '0' && str !== "" && str !== null && str > 60){
str = ((Math.floor(str / 60)).toString().length < 2 ? "0" + (Math.floor(str / 60)).toString() :
(Math.floor(str / 60)).toString()) + ":" + ((str % 60).toString().length < 2 ? "0" + (str % 60).toString() : (str % 60).toString());
return str +'小时';
}else if (str < 60) {
return str+'分钟';
}else{
return "";
}
},
四,分享设置以及参数接收
//分享设置
onShareAppMessage() {
let _this = this;
let recommendNum = _this.data.recommendNum; //要传的参数
let ins = encodeURIComponent('yqm=' + _this.data.recommendNum);
let path = '/pages/map/index?scene=' + ins;
let appTitle = '邀请好友注册';
return {
title: appTitle,
path: path,
}
},
//参数接收
onLoad:function(e){
let scene = decodeURIComponent(e.scene);
let arr1 = scene.split('&');
let invite = '';
for (var i = 0; i < arr1.length; i++) {
let key = arr1[i].split("=")[0];
if (key == 'yqm'){
invite = arr1[i].split("=")[1];
}
}
if(invite != ''){
//设置全局邀请参数
wx.setStorageSync("inviteCode",invite);
}
},
五,navigateTo 路由跳转注意事项
wx.navigateTo({
url:'/pages/index/login'
})
异常情况:有的时候多次navigateTo跳转后,发现点击按钮没有响应,这个时候应该查一下连续性的页面跳转是否已经超过5层,因为navigateTo可以和navigateBack结合进行指定页面栈回跳的,所以当发现navigateTo无效的时候应检测这个。
六,小程序页面嵌入背景音乐
<!--html代码-->
<audio src="{{src}}" id="myAudio" style="display: none"></audio>
//js代码
Page({
data: {
src:'' //mp3文件名
},
onReady: function () {
var _this = this;
_this.audioCtx = wx.createAudioContext('myAudio')
}
//播放行为
play_mp3:function(type){
let _this = this;
let name = 'menu.mp3';
_this.setData({
src:app.static_path+name
})
_this.audioPlay();
},
audioPlay: function () {
this.audioCtx.play()
},
})
七,小程序之路线导航
go_to_purpos:function(){
wx.showToast({
title: '路线规划中',
icon: 'loading',
});
let _this = this,
site_name = '地名',
site_address = '详细地址',
to_latitude = _this.data.to_latitude,
to_longitude = _this.data.to_longitude;
wx.getLocation({
type: 'gcj02', //返回可以用于wx.openLocation的经纬度
success (res) {
wx.hideToast({});
wx.openLocation({
name: site_name,
address: site_address,
latitude: parseFloat(to_latitude),
longitude: parseFloat(to_longitude),
scale: 18
})
}
})
},
未完待续,持续更新最常用用法中。。。