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);    
  }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章