简体   繁体   English

当我点击一个锚点时添加偏移 Y“-100px”,它会将我向下滚动到一个元素

[英]Add Offset Y "-100px" when I click on an anchor and it scrolls me down to an element

I need to add an offset of -100px when I click on a link that scrolls me down to a section.当我单击将我向下滚动到某个部分的链接时,我需要添加 -100px 的偏移量。 I have managed to make my scroll smooth, but i cannot make it to scroll with offsetY.我已经设法让我的滚动平滑,但我不能让它用 offsetY 滚动。

Here is my js code:这是我的js代码:

const links = document.querySelectorAll("a");

for (const link of links) {
  link.addEventListener("click", clickHandler);
}

function clickHandler(e) {
  e.preventDefault();
  const href = this.getAttribute("href");
  const offsetTop = document.querySelector(href).offsetTop;

  scroll({
    top: offsetTop,
    behavior: "smooth",
  });
}

... and here is my html: ...这是我的 html:

<a href ="#mysection">Go to section </a>
<p>this is some lorem</p>
<p>this is some lorem</p>
<p>this is some lorem</p>

<div id ="mysection"> The section that appears when i click on the anchor. I want an offset on this div so that when i click on anchor, it wil scroll me at the beggining of this section </div>

Maybe thats what you looking for.也许这就是你要找的。 In my case, when I clicked on menu link I was dericted to section I need, but since I have Header fixed on top, my section have been covered.就我而言,当我单击菜单链接时,我被定位到我需要的部分,但由于我将 Header 固定在顶部,因此我的部分已被覆盖。 So I added to my section one with id="about-anchor" and styled it with position relative and top -85px;所以我在第一节中添加了 id="about-anchor" 并使用 position relative 和 top -85px;

See code snippets for details.有关详细信息,请参阅代码片段。

 html { scroll-behavior: smooth; background: black; color: white; } section { border: 2px solid red; } #hero-qnchor, #about-qnchor, #contact-anchor { display: block; position: relative; top: -85px; } nav { position: fixed; width: 100%; background: white; border: 1px solid red; } nav ul { display: flex; justify-content: right; padding: 0 20px; list-style: none; } nav a { text-decoration: none; color: #333; font-size: 1.5rem; padding: 5px 10px; margin-right: 20px; transition: color 200ms ease, background-color 200ms ease; }
 <div> <nav> <ul> <a href="#hero-qnchor"> <li>Hero</li> </a> <a href="#about-qnchor"> <li>About</li> </a> <a href="#contact-anchor"> <li>Contact</li> </a> </ul> </nav> <section id="hero"> <div id="hero-qnchor"></div> <p className="about-wrapper__info-text"> Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero Hero </br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>. </p> </section> <section id="about"> <div id="about-qnchor"></div> <p className="about-wrapper__info-text"> About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About About </br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>. </p> </section> <section id="contact"> <div id="contact-anchor"></div> <p className="about-wrapper__info-text"> Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact Contact </br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>.</br>. </p> </section> </div>

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

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