简体   繁体   English

同一页面内的链接

[英]Link within the same page

I am linking the table of content with the heading of a section in the same page. 我将目录与同一页面中某个部分的标题链接在一起。 However, when the screen scrolls down to the desired section the actual linked heading gets lost under the navigation bar which is kept as fixed. 但是,当屏幕向下滚动到所需部分时,实际链接的标题会丢失,并保持固定不变。 It is working fine on the mobile preview. 在移动预览中工作正常。 Having issue with the desktop. 桌面出现问题。 Following is the code i am using : 以下是我正在使用的代码:

<div class="content uk-width-1-2@l uk-width-1-1@m uk-width-1-1@s">
  <span class="anchor" id="Overview"></span>
  <h2>Overview</h2>
  <p> As a very high-level summary:</p>
  <ul>
    <li>
      Companu has strong application, network and infrastructure-level
      security controls in place to ensure your data is safely stored
      and processed <br /><br />
    </li>
    <li>
      Company serves multiple tenants from the same application codebase,
      but uses effective isolation techniques to keep tenant data separate
      <br /><br />
    </li>
    <li>
      Privacy laws, which are broadly compatible with many other jurisdictions
      (for example, we support the rights of access and rectification
      for data subjects) <br/><br/>
    </li>
    <li>Comapny is hosted on AWS, in multiple regions, using VPC <br /><br /></li>
  </ul>
  <p>
    You'll find more information on each of these points
    in the detailed chapters documents below.
  </p>
</div>

.anchor {
  display: block;
  height: 63px;
  /* this is the height of your header */
  margin-top: -63px;
  /* this is again negative value of the height of your header */
  visibility: hidden;
}

Can anyone please help to sort this problem. 谁能帮忙解决这个问题。

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

#Overview::before { 
  display: block; 
  content: " "; 
  margin-top: -63px; 
  height: 63px; 
  visibility: hidden; 
}

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

Note: I would assign the Overview ID directly to the h2 element, not to an extra span 注意:我将Overview ID直接分配给h2元素,而不是额外的span

Here it is in a snippet example: 这是一个片段示例:

 html, body { margin: 0; } .header { position: fixed; width: 100%; height: 60px; top: 0; left: 0; background: #d75; } .something_in_between { height: 500px; background: #75f; padding-top: 70px; } #Overview { background: yellow; height: 500px; } #Overview::before { display: block; content: " "; margin-top: -63px; height: 63px; visibility: hidden; } 
 <div class="header">Fixed Header</div> <div class="something_in_between">Here's the <a href="#Overview">LINK</a> to the "Overview" element</div> <div id="Overview">This should be visible when clicking the link</div> 

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

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