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 badges
Add a ment  | 

5 Answers 5

Reset to default 1

I 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