我正在使用html2canvas库,当我调用html2canvas.Parse()时,页面滚动到顶部。
我想如果我在调用html2canvas.Parse()之前能记住这个位置,那么我就可以回到原来的滚动位置了。
答案 0 :(得分:1)
要记住滚动,请使用此代码
$(document).ready(function (e) {
let UrlsObj = localStorage.getItem('rememberScroll');
let ParseUrlsObj = JSON.parse(UrlsObj);
let windowUrl = window.location.href;
if (ParseUrlsObj == null) {
return false;
}
ParseUrlsObj.forEach(function (el) {
if (el.url === windowUrl) {
let getPos = el.scroll;
$(window).scrollTop(getPos);
}
});
});
function RememberScrollPage(scrollPos) {
let UrlsObj = localStorage.getItem('rememberScroll');
let urlsArr = JSON.parse(UrlsObj);
if (urlsArr == null) {
urlsArr = [];
}
if (urlsArr.length == 0) {
urlsArr = [];
}
let urlWindow = window.location.href;
let urlScroll = scrollPos;
let urlObj = {url: urlWindow, scroll: scrollPos};
let matchedUrl = false;
let matchedIndex = 0;
if (urlsArr.length != 0) {
urlsArr.forEach(function (el, index) {
if (el.url === urlWindow) {
matchedUrl = true;
matchedIndex = index;
}
});
if (matchedUrl === true) {
urlsArr[matchedIndex].scroll = urlScroll;
} else {
urlsArr.push(urlObj);
}
} else {
urlsArr.push(urlObj);
}
localStorage.setItem('rememberScroll', JSON.stringify(urlsArr));
}
$(window).scroll(function (event) {
let topScroll = $(window).scrollTop();
console.log('Scrolling', topScroll);
RememberScrollPage(topScroll);
});
答案 1 :(得分:0)
在下面的示例中,我使用jQuery使事情变得容易,但是您可以在香草js中轻松地完成同样的事情。
var scrollPos;
$('.button').on('click', function() {
// save scroll position to a variable
scrollPos = $(window).scrollTop();
// do something
$('html, body').animate({
scrollTop: $("#cats").offset().top
}, 500);
// scroll back
setTimeout(
function() {
$('html, body').animate({
scrollTop: scrollPos
}, 500);
}, 1000);
});
.button {
position: fixed;
font-size: 12px;
margin: 10px;
}
#rainbow {
height: 2000px;
background: -webkit-linear-gradient(red, orange, yellow, green, cyan, blue, violet);
background: -o-linear-gradient(red, orange, yellow, green, cyan, blue, violet);
background: -moz-linear-gradient(red, orange, yellow, green, cyan, blue, violet);
background: linear-gradient(red, orange, yellow, green, cyan, blue, violet);
}
#cats {
width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<input class="button" value="Scroll down a bit then click here for 1 second of cats!" type="button">
<div id="rainbow"></div>
<img id="cats" title="By Alvesgaspar [CC BY-SA 3.0
(https://creativecommons.org/licenses/by-sa/3.0
)], via Wikimedia Commons" width="1024" alt="Cat poster 1" src="https://upload.wikimedia.org/wikipedia/commons/thumb/0/0b/Cat_poster_1.jpg/1024px-Cat_poster_1.jpg">
答案 2 :(得分:0)
我采用了现代的html5浏览器来处理此问题。它将最后一个滚动位置存储在客户端Web浏览器本身中,然后在重新加载页面时,将设置从浏览器读取回最后一个滚动位置。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
if (localStorage.getItem("my_app_name_here-quote-scroll") != null) {
$(window).scrollTop(localStorage.getItem("my_app_name_here-quote-scroll"));
}
$(window).on("scroll", function() {
localStorage.setItem("my_app_name_here-quote-scroll", $(window).scrollTop());
});
});
</script>
答案 3 :(得分:0)
这对我有用:
window.onbeforeunload = function () {
var scrollPos;
if (typeof window.pageYOffset != 'undefined') {
scrollPos = window.pageYOffset;
}
else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') {
scrollPos = document.documentElement.scrollTop;
}
else if (typeof document.body != 'undefined') {
scrollPos = document.body.scrollTop;
}
document.cookie = "scrollTop=" + scrollPos + "URL=" + window.location.href;
}
window.onload = function () {
if (document.cookie.includes(window.location.href)) {
if (document.cookie.match(/scrollTop=([^;]+)(;|$)/) != null) {
var arr = document.cookie.match(/scrollTop=([^;]+)(;|$)/);
document.documentElement.scrollTop = parseInt(arr[1]);
document.body.scrollTop = parseInt(arr[1]);
}
}
}
我在别处找到的大部分代码,不幸的是我找不到源代码了。只是添加了一个检查以查看 URL 是否相同,以便仅保存同一页面的滚动位置,而不保存其他页面。