DIV auto scroll

后端 未结 5 1033
情话喂你
情话喂你 2021-02-06 10:34

I have a div that has a large number of items in it. I select these items dynamically by clicking a button, overflow is auto in my case. What I want is that when an item that is

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-06 11:30

    You can use the scrollTop property of HTMLElement to set the amount its content is scrolled by.

    And you can get the amount you need to scroll by from offsetTop of the element to which you want to scroll.

    For example with this HTML:

    foo

    bar

    baz

    You could use this JavaScript to scroll the container div to third paragraph:

    document.getElementById("container").scrollTop = document.getElementById("item-3").offsetTop;
    

提交回复
热议问题