Recently I have made some H5 pages (article pages), and users have put forward some common requirements: go back to the previous page and stay at the last browsing position
After searching for some information and copying the code of the front-end girl, the implementation is as follows:
//Return to the last browsed location
var sortId = $("#sortId").val(); //The unique id of this article
var str = sortId + 'scrollTop';
//// The data stored by the localStorage method has no time limit ( get location data from localStorage, similar to memcached )
var scrollTop = localStorage.getItem(str);
// alert(scrollTop);
if(scrollTop){
$('body').scrollTop(scrollTop);
}else{
$('body').scrollTop(0);
}
// save the browsing position
$('body').on('touchmove', function(event) {
// event.preventDefault();
console.log($('body').scrollTop());
var scrollTop = $('body').scrollTop();
var sortId = $ ("# sortId"). val ();
var str = sortId + 'scrollTop';
localStorage.setItem(str,scrollTop); //Store location data
});
//The test has been tuned and the code is put into the article page.