简体   繁体   English

使锚链接在 id 上方一些像素

[英]Make anchor link go some pixels above id

I'm currently working on a website and I'm having a trouble with anchors.我目前正在开发一个网站,但在使用锚点时遇到了问题。 My header is fixed and when I click on anchor it sends me on other page how it is supposed to be, but I'm missing 80 pixels which is height of my fixed header.我的标题是固定的,当我点击锚点时,它会在其他页面上向我发送它应该如何,但我缺少 80 像素,这是我的固定标题的高度。 There is a script that made accordion opened on new page when I click on anchor but it should scroll 80px less... here is some code I have over there in my .jsp file当我单击锚点时,有一个脚本可以在新页面上打开手风琴,但它应该少滚动 80 像素……这是我在 .jsp 文件中的一些代码

 <a href="${parentLink}#${menuItem.name}" class="${menuItem.classes[anchorClasses]}">

and there is a .js that makes my accordion opened on the new page并且有一个 .js 可以让我的手风琴在新页面上打开

$(document).ready(function () {
    if (location.hash != null && location.hash != "") {
        $('.collapse').removeClass('in');
        $(location.hash + '.collapse').collapse('show');
    }
});

I think that you guys will need more info, so ask me anything that could help you.我想你们会需要更多信息,所以问我任何可以帮助你的事情。 I'm new in this and I don't even know which code should I post here to help you guys realize what the problem is... Thank you (:我是新来的,我什至不知道我应该在这里发布哪些代码来帮助你们意识到问题是什么......谢谢(:

One common way is to add an invisible pseudo element to the original target element of the link via CSS, like this:一种常见的方法是通过 CSS 向链接的原始目标元素添加一个不可见的伪元素,如下所示:

#your_anchor_id::before { 
  display: block; 
  content: " "; 
  margin-top: -80px; 
  height: 80px; 
  visibility: hidden; 
  pointer-events: none;
}

This will "extend" the element with that ID in a way which causes the anchor to be 80px above the main element, without causing any other visible changes.这将以某种方式“扩展”具有该 ID 的元素,使锚点位于主元素上方 80 像素,而不会导致任何其他可见变化。

Another idea is to use smooth scrolling with an offset.另一个想法是使用带有偏移量的平滑滚动。 (View the example "Full Page" by clicking that link at top right of the snippet window) (通过单击代码段窗口右上角的链接查看示例“整页”)

 $("nav ul li a").on('click', function(event) { if (this.hash !== "") { var myOffset = $('#myOff').val(); //get value from input (offset value) if (myOffset==='') $('input').addClass('alert'); event.preventDefault(); // Prevent default anchor click behavior var hash = this.hash; // Store hash // jQuery animate() method for smooth page scroll // 900 is the number of ms to scroll to the specified area $('html, body').animate({ scrollTop: $(hash).offset().top - myOffset }, 900); } // End if }); //$('div:contains(Section)').css('font-weight','bold');
 html,body{margin:0;padding:0;font-family:Calibri;} body{height:2500px;} ul,li{margin:0;padding:0;} *{box-sizing:border-box;} section{ display: grid; place-items: center; width: 100%; height: 500px; } nav{position:fixed;width:80vw;background:white;border:1px solid red;} ::placeholder{color:#ccc;} nav ul li{ display: inline-block; padding:0; border: 1px solid rgba(200,200,200,0.3); } nav ul li:hover{background: #ddd;} a{text-decoration:none;padding:10px 25px;display:inline-block;} #one{background:palegreen; padding:50px;} #two{background:palegoldenrod;} #twa{background:lightblue;} #fer{height:1500px;} .alert{border:1px solid red;background:#ffc0cb99;}
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <nav> <ul> <li>NAV / HEADER:</li> <li><a href="#one">One</a></li> <li><a href="#two">Two</a></li> <li><a href="#twa">Three</a></li> <li><input id="myOff" type="text" placeholder="Offset (eg 75):" /></li> </ul> </nav> <section id="one"> <div> <div style="text-align:center">Section One</div> <div>Directions:<br>(a) View as Full Page (link at top right)<br>(b) Enter offset number (for how many pixels the smooth-scroll will stop short)<br>(c) Click nav "Two" or "Three" and observe<br>(4) Repeat using a different offset value<br>Note: The fixed header is deliberately not full width in order to show the top of the next section scrolling UNDER the header (undesireable) The offset prevents that, and is what you are asking about.</div> </div> </section> <section id="two"> Section Two </section> <section id="twa"> Section Three </section> <section id="fer"> Section Four </section>

Example code ripped off from:示例代码来自:

w3schools Company Theme example w3schools 公司主题示例

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

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