Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
426 views
in Technique[技术] by (71.8m points)

javascript - 在HTML中刷新页面时强制页面滚动到顶部(Force page scroll position to top at page refresh in HTML)

I am building a website which I am publishing with div s.(我正在建立要与div发布的网站。)

When I refresh the page after it was scrolled to position X, then the page is loaded with the scroll position as X.(当页面滚动到位置X后刷新页面时,页面的滚动位置为X。) How can I force the page to be scrolled to the top on page refresh?(如何强制页面滚动到页面刷新的顶部?) What I can think of is of some JS or jQuery run as onLoad() function of the page to SET the pages scroll to top.(我能想到的是作为页面的onLoad()函数运行的某些JS或jQuery,以设置页面滚动到顶部。) But I don't know how I could do that.(但是我不知道该怎么办。) A better option would be if there is some property or something to have the page loaded with its scroll position as default (ie at the top) which will be kind of like page load , instead of page refresh .(一个更好的选择是,如果有一些属性或东西要让页面默认加载其滚动位置(即,顶部),这有点像页面加载 ,而不是页面刷新 。)   ask by Moon translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can do it using the scrollTop method on DOM ready :(您可以使用DOM ready上的scrollTop方法来做到这一点:)

$(document).ready(function(){ $(this).scrollTop(0); });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...