Using scrollIntoView with a fixed position header

前端 未结 8 1109
粉色の甜心
粉色の甜心 2020-12-08 01:50

I have a site with a header set to position: fixed. On one of my pages, I use scrollIntoView(true) on an element. My problem is that when scr

8条回答
  •  囚心锁ツ
    2020-12-08 02:23

    If anybody runs into issues with the top margin of your container div being ignored after a scrollIntoView, then instead of scrolling your element into view, just do a scrollTop relative to its parent scrolling container, as such:

    var topOfElementToView= $('#elementToScroll').position().top;
    $('#parentScrollingContainer').scrollTop(topOfElementToView);
    

    Got the answer from user113716 on this thread: How to go to a specific element on page?

提交回复
热议问题