cordova+vue搭建app实践笔记

实现对原生物理返回键的监听:

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);    
  }
阅读 808

推荐阅读
yunsiyu
用户专栏

保持学习的热情

0 人关注
4 篇文章
专栏主页