移动端导航返回按钮事件

popstate:返回事件 
 
 
  goPage(item) {
      this.show = true
        if (window.history && window.history.pushState) {
        this.preventDefault = true;
     
        // 往历史记录里面添加一条新的当前页面的url
        history.pushState(null, null, document.URL);
      }
    },
 
   hijack() {
      // 给 popstate 绑定一个方法
      window.addEventListener("popstate", this.backNative);
    },
    backNative(e) {
      if (this.preventDefault) {
        e.preventDefault();
          this.show = false
        //执行后清除preventDefault
        this.preventDefault = false;
      } else {
        console.log("默认");
      }
    },
    removeHijack() {
      window.removeEventListener("popstate", this.backNative);
    },
posted @ 2021-04-10 15:01  Alexander.L  阅读(149)  评论(0编辑  收藏  举报