页面刷新时直接定位到某位置

方法一:

$(document).ready(function() {
   window.location.href ="#come here";
});

(注:用此方法页面链接后会多一个锚点类名)

方法二:

<script>
var timer, scrollHeight, viewHeight, step = 2000, sTop = 0,isScrict=document.compatMode=='CSS1Compat';
document.onclick = function () { clearInterval(timer); }
function Move() {
//设置滚动前获取当前的的滚动高度和sTop比较,如果小于sTop或者和sTop的差距大于step定义的,说明拖拽过滚动条了
var nowScrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
if (nowScrollTop < sTop || (nowScrollTop - top > step)) clearInterval(timer);
sTop += step;
document.documentElement.scrollTop = document.body.scrollTop = sTop;
if (sTop + viewHeight > scrollHeight) {//滚动到底部
clearInterval(timer);
document.documentElement.scrollTop = document.body.scrollTop = 700//跳转到顶部
}
}
window.onresize = function () {
viewHeight = document[isScrict?'documentElement':'body'].clientHeight;
scrollHeight = document[isScrict?'documentElement':'body'].scrollHeight;
}
window.onload = function () {
window.onresize();
timer = setInterval(Move, 0);
}
</script>

(虽然不会增加锚点类名,但是在页面元素全部加载完之后跳转到指定位置)

原文地址:https://www.cnblogs.com/liuyingself/p/7837864.html