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
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745221972a4617289.html
评论列表(0条)