简体   繁体   English

从固定标题偏移页面锚

[英]Offsetting page anchor from fixed header

I have looked at other posts for this issue but alass none have worked so here i am. 我已经看过其他有关此问题的帖子,但是很遗憾,这些帖子都没有起作用,所以我在这里。

Im trying to make my header(position is fixed) link to a page anchor but when i press the link it goes past the top of the div and the header for it. 我试图使我的页眉(位置固定)链接到页面锚,但是当我按链接时,它越过了div的顶部和它的标题。 So to see the entire div including header then you need to scroll up. 因此,要查看包括标题的整个div,则需要向上滚动。

I have got a Fiddle of it here 在这里有一个小提琴

.wrapper {
width: 100%;
height: 1000px;
margin: 130px auto;
padding: 0;
background-color: aliceblue;}

You can just use CSS. 您可以只使用CSS。 Just make standalone hidden anchor elements and clip them the height of your header. 只需制作独立的隐藏锚元素,然后将其裁剪为标题的高度即可。

Here is the anchor class I used: 这是我使用的锚类:

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

JSFiddle (I only did the first few links): http://jsfiddle.net/pp9dg/29/ JSFiddle (我只做前几个链接): http : //jsfiddle.net/pp9dg/29/

Here's an updated fiddle: http://jsfiddle.net/pp9dg/28/ 这是更新的小提琴: http : //jsfiddle.net/pp9dg/28/

It uses jquery which offsets the anchor point by 200px. 它使用jquery将锚点偏移200像素。

$(window).on("hashchange", function () {
    window.scrollTo(window.scrollX, window.scrollY - 200);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM