javascript - Set initial scrollbar position - Stack Overflow

I am trying to set the initial position of a scrollbar in a div so that a particular element is display

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745221972a4617289.html

相关推荐

  • javascript - Set initial scrollbar position - Stack Overflow

    I am trying to set the initial position of a scrollbar in a div so that a particular element is display

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信