Problem with scrollto offset javascript because of header

后端 未结 2 1421
醉话见心
醉话见心 2021-01-23 00:39

I need help with this scrollto code snippet. The problem is that I need to set an offset to account for my menu. Without the offset the header that I scroll to gets buried under

相关标签:
2条回答
  • 2021-01-23 01:20

    Just a suggestion have you tried something like:

    window.scrollTo({top: (jQuery('#2').position().top-jQuery('header').height()), behavior: 'smooth' })
    

    ?

    Where #2 would be taken from your this.hash target above?

    0 讨论(0)
  • 2021-01-23 01:36

    Should be relatively easy: If your header is for example 85px high, you can just add these 85px to the offset in your code, so this line

    scrollTop: target.offset().top
    

    becomes

    scrollTop: target.offset().top - 85
    

    that way the window will scroll 85px less than calculated, so the section will not be hidden behind the header.

    0 讨论(0)
提交回复
热议问题