微信公眾號開發進入頁面A,如上圖;
點擊“查看做題結果”,進入虛擬頁面B(頁面B是覆蓋在頁面A上的fixed頁面),如下圖:
希望在頁面B點擊返回,不是直接退出,而是返回頁面A,在頁面A點返回才是退出。
//點擊“查看做題結果”,打開頁面B
vm.startPaper = function() {
...你的事件處理...
vm.doingPaper = true;
pushHistory();
};
//關閉頁面B,返回頁面A,需要執行的事件
vm.goHome = function() {
...你的事件處理...
vm.doingPaper = false;
};
////監聽popstate事件
var state = {};
window.addEventListener("popstate", function(e) { //popstate監聽返回按鈕
if(vm.doingPaper){
window.location.href = state.url;
vm.doingPaper = false;
$timeout(function(){
vm.goHome();
},100)
}else{
WeixinJSBridge.call('closeWindow');
}
}, false);
function pushHistory() {
state = {
title: vm.title,
url: location.href
};
history.pushState(state, state.title, state.url);
}
參考地址:
http://blog.csdn.net/xcqingfeng/article/details/70800118