pc端、手機端瀏覽器、微信內.點選返回鍵,返回到上一個頁面瀏覽的位置的實現
阿新 • • 發佈:2018-12-10
第一步:需要注意引入的js
jquery.js jquery.cookie.js
第二部:在被返回的前一頁加入以下程式碼
<script type="text/javascript"> $(function () { var str = window.location.href; str = str.substring(str.lastIndexOf("/") ); if ($.cookie(str)) {//$.cookie():取cookie; $("html,body").animate({ scrollTop: $.cookie(str) }, 1000); }else { } }) $(window).scroll(function () {//滾動時,將滾動條的高度記錄到cookie中 var str = window.location.href; str = str.substring(str.lastIndexOf("/") ); var top = $(document).scrollTop(); $.cookie(str, top, { path: '/' });//儲存到cookie中 return $.cookie(str); }) </script>