cordova+vue搭建app实践笔记

2次阅读

共计 1238 个字符,预计需要花费 4 分钟才能阅读完成。

实现对原生物理返回键的监听:
var exitAppTicker = 0;
document.addEventListener(“deviceready”,function(){
document.addEventListener(“backbutton”, function(){
var pageUrl = window.location.href;
var n = pageUrl.lastIndexOf(‘?’);
var m = pageUrl.lastIndexOf(‘/’);
var str = ”
if (n > 0) {
str = pageUrl.substring(m+1,n); // 获取 pageName
} else {
str = pageUrl.substring(m+1); // 获取 pageName
}
if(str == ‘index’ || str == ” || str == ‘loginPhone’){
if(exitAppTicker == 0){
exitAppTicker++;
showToast(‘ 再次返回退出 ’, 2000)
setTimeout(function(){
exitAppTicker = 0;
},2000);
}else if(exitAppTicker == 1){
navigator.app.exitApp();
}
}else{
history.back();
}
}, false);
},false);
自定义 toast , js 实现 android 中 toast 效果
/**
* 自定义 toast,js 实现 android 中 toast 效果
* @param msg 显示文字
* @param duration 显示的时间长度
*/
function showToast(msg, duration) {
duration = isNaN(duration) ? 2000 : duration;
var m = document.createElement(‘div’);
m.innerHTML = msg;
m.style.cssText = “width:60%; min-width:150px; background:#000; opacity:0.5; height:40px; color:#fff; line-height:40px; text-align:center; border-radius:5px; position:fixed; top:70%; left:20%; z-index:999999; font-weight:bold;”;
document.body.appendChild(m);
setTimeout(function() {
var d = 0.5;
m.style.webkitTransition = ‘-webkit-transform ‘ + d
+ ‘s ease-in, opacity ‘ + d + ‘s ease-in’;
m.style.opacity = ‘0’;
setTimeout(function() {
document.body.removeChild(m)
}, d * 1000);
}, duration);
}

正文完
 0