移動端圖片開啟及返回客戶端

weixin_33978044發表於2017-07-11

場景:客戶端進入webview,開啟H5頁面,頁面中有圖片,點選圖片全屏開啟,點返回鍵,返回H5頁面,再點返回鍵,返回客戶端。頁面中有同這個頁面一樣url的跳轉連結

中間折騰了一會,現在把思路記錄下來

1,進入H5頁面,開啟圖片

$('.screenhots').on('click', '.screenshot-img', function () {
              window.history.replaceState({page : 'detail'}, '', ''); //返回時用到
              window.history.pushState('forward', null, 'detail.html?style=orange#showpic');
})
增加pushState 和replaceState
2,監聽返回
// 監聽手機回退
      if (window.history && window.history.pushState) {
        $(window).on('popstate', function (e) {
          if (e.state.page == 'detail') {
            $('#showpic').css('zIndex', '0').hide();
          }else{
            $('.back').click();
          }
        });
      }
3,為解決返回時,有很多歷史記錄,使用replace解決
 window.location.replace('detail.html');

相關文章