Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

javascript - 偏移html锚以调整为固定的标头[重复](offsetting an html anchor to adjust for fixed header [duplicate])

This question already has an answer here:

(这个问题已经在这里有了答案:)

I am trying to clean up the way my anchors work.

(我正在尝试清理锚点的工作方式。)

I have a header that is fixed to the top of the page, so when you link to an anchor elsewhere in the page, the page jumps so the anchor is at the top of the page, leaving the content behind the fixed header (I hope that makes sense).

(我有一个固定在页面顶部的标题,因此当您链接到页面其他位置的锚点时,页面会跳转,因此该锚点位于页面顶部,而将内容留在固定的标题后面(我希望那讲得通)。)

I need a way to offset the anchor by the 25px from the height of the header.

(我需要一种将锚点从标头高度偏移25像素的方法。)

I would prefer HTML or CSS, but Javascript would be acceptable as well.

(我更喜欢HTML或CSS,但是Javascript也可以接受。)

  ask by Matt Dryden translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could just use CSS without any javascript.

(您可以只使用CSS而无需任何JavaScript。)

Give your anchor a class:

(给您的锚课程:)

<a class="anchor" id="top"></a>

You can then position the anchor an offset higher or lower than where it actually appears on the page, by making it a block element and relatively positioning it.

(然后,可以通过将锚定为一个块元素并将其相对定位,来将锚定的位置比页面上实际显示的位置高或低。)

-250px will position the anchor up 250px

(-250px将锚定位置上移250px)

a.anchor {
    display: block;
    position: relative;
    top: -250px;
    visibility: hidden;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...