scrollIntoView with margin

后端 未结 5 1496
孤独总比滥情好
孤独总比滥情好 2021-02-19 08:14

I have a webpage on which I would like to scroll to a certain element.

That part works fine by using scrollIntoView; but I would like to add a bit of space

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-19 08:53

    In general it's not very straightforward (if we want behavior: smooth), and will require messing with javascript in one way or another. For example you could use window.scrollTo and calculate necessary top position manually.

    In some cases however you could visually achieve necessary effect by using CSS smartly. In your demo you can use padding-top instead of margin and wrap content of the block into additional helper container.

    Demo: https://codepen.io/anon/pen/OvKQLV

提交回复
热议问题