admin管理员组文章数量:1415093
I am trying to set the initial position of a scrollbar in a div so that a particular element is displayed at the top.
I tried the following but it does not set the scrollbar to the correct position.
scrollingDiv.scrollTop = element.offsetTop;
Suggestions are appreciated.
I am trying to set the initial position of a scrollbar in a div so that a particular element is displayed at the top.
I tried the following but it does not set the scrollbar to the correct position.
scrollingDiv.scrollTop = element.offsetTop;
Suggestions are appreciated.
Share Improve this question edited Apr 29, 2018 at 12:20 Julius Dzidzevičius 11k11 gold badges41 silver badges84 bronze badges asked Apr 29, 2018 at 12:11 Phil PreenPhil Preen 6376 silver badges22 bronze badges5 Answers
Reset to default 1I added position:relative; to the scrollingDiv style and it now works.
try using
.scrollTop(0);
if you want to move to the intial position
This can be done by adding position relative to scrollbar. Try adding
position: relative
in css file to scrollbar.
You can use scrollTo(x: number, y: number) function to set to initial position like that:
window.scrollTo(0,0)
.content {
position: relative;
}
<div>
<div classname='content'>
<div>
<div>
const el = document.querySelector('.content')
el?.scrollTop = 0
本文标签: javascriptSet initial scrollbar positionStack Overflow
版权声明:本文标题:javascript - Set initial scrollbar position - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745221972a2648435.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论