且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

滚动到带有偏移量的 div ID

更新时间:2023-02-26 12:39:55

我遇到了同样的问题,就我而言,我通过添加 padding-top 和相同值的负边距顶部解决了这个问题:

I was having the same issue and in my case I solved it by adding padding-top and a negative margin-top of the same value:

.some-class {
  padding-top: 4em;
  margin-top: -4em;
}

通过这样做,我的元素看起来像是在完全相同的位置,但浏览器在滚动时会更快地找到它.您可以将这些值设置为粘性标题的高度,或者四处看看以确保标题正好位于您想要的位置.

By doing this my element looks like it's on the exact same location but the browser finds it sooner while scrolling. You can set these values to the height of your sticky header or play around to make sure the heading is exactly where you want it to be.

我希望我能以一种可以理解的方式解释这一点......这在我的脑海中肯定是有道理的:D

I hope I'm explaining this in a way that's understandable... It sure makes sense in my head :D