javascript - 不同标题类型之间的 anchor 标记行为不一致

标签 javascript html css anchor href

在下面的示例中,单击“主页”或“关于”标题将正确跳转到这些部分。但是,单击“示例标题”和“示例标题2”将没有任何效果。我怎样才能做到单击示例标题将允许用户正确跳转到这些部分。

非常感谢您的帮助,谢谢。

function myFunction() {
  var x = document.getElementById("myTopnav");
  if (x.className === "topnav") {
    x.className += " responsive";
  } else {
    x.className = "topnav";
  }
}
body {
  margin: 0;
}

.topnav {
  overflow: hidden;
  background-color: #000;
  position: fixed;
  top: 0;
  width: 100%;
  text-align: center;
  z-index: 10;
}

.topnav a {
  display: inline-block;
  color: #fff;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav .icon {
  display: none;
}

@media screen and (max-width: 600px) {
  .topnav a {
    display: none;
  }
  .topnav a.icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .topnav.responsive {
    position: fixed;
  }
  .topnav.responsive .icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
}

.section {
  position: relative;
}

.anchor {
  display: block;
  position: absolute;
  width: 0;
  height: 0;
  z-index: -1;
  top: -50px;
  left: 0;
  visibility: hidden;
}

p{
height: 200px;
}
<!DOCTYPE html>
<html lang="en">

<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8" />
<title>title</title>

<body>

  <div class="topnav" id="myTopnav">
    <a href="#home" onclick="myFunction()" class="active">Home</a>
    <a href="#about" onclick="myFunction()">About</a>
    <a href="#contact" onclick="myFunction()">Contact</a>
    <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">&#9776;</a>
  </div>

  <div class="w3-row-padding" style="max-width: 50em;margin: auto; margin-top: 50px;">
    <hr style="margin-left: 10px; margin-right:10px">

    <div class="w3-full section" style="margin-left: 10px; margin-right:10px">
      <span id="home" class="anchor"></span>
      <h1 style="margin-bottom:0px;"><a href="#home">Home</a></h1>
      <p>This is a paragraph with large height.</p>
    </div>

    <div class="w3-full section" style="margin-left: 10px; margin-right:10px">
      <span id="about" class="anchor"></span>
      <h1><a href="#about">About</a></h1>
      <span id="1" class="anchor"></span>
      <h3 style="margin-bottom:0px;"><a href="#1">Sample Title</a></h3>

      <p>This is a paragraph with large height.</p>

      <span id="2" class="anchor"></span>
      <h3 style="margin-bottom:0px;"><a href="#2">Sample Title 2</a></h3>

      <p>This is a paragraph with large height.</p>

    </div>

    <div class="w3-full section" style="margin-left: 10px; margin-right:10px">
      <span id="contact" class="anchor"></span>
      <h1><a href="#contact">Contact</a></h1>

      <p>This is a paragraph with large height.</p>

    </div>
  </div>
</body>

</html>

最佳答案

anchor 的 position:absolute 扰乱了它的位置;将其更改为相对

function myFunction() {
  var x = document.getElementById("myTopnav");
  if (x.className === "topnav") {
    x.className += " responsive";
  } else {
    x.className = "topnav";
  }
}
span.anchor {
  position: relative;
}

body {
  margin: 0;
}

.topnav {
  overflow: hidden;
  background-color: #000;
  position: fixed;
  top: 0;
  width: 100%;
  text-align: center;
  z-index: 10;
}

.topnav a {
  display: inline-block;
  color: #fff;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav .icon {
  display: none;
}

@media screen and (max-width: 600px) {
  .topnav a {
    display: none;
  }
  .topnav a.icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .topnav.responsive {
    position: fixed;
  }
  .topnav.responsive .icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
}

.section {
  position: relative;
}

.anchor {
  display: block;
  position: absolute;
  width: 0;
  height: 0;
  z-index: -1;
  top: -50px;
  left: 0;
  visibility: hidden;
}
<div class="topnav" id="myTopnav">
  <a href="#home" onclick="myFunction()" class="active">Home</a>
  <a href="#about" onclick="myFunction()">About</a>
  <a href="#contact" onclick="myFunction()">Contact</a>
  <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">&#9776;</a>
</div>

<div class="w3-row-padding" style="max-width: 50em;margin: auto; margin-top: 50px;">
  <hr style="margin-left: 10px; margin-right:10px">

  <div class="w3-full section" style="margin-left: 10px; margin-right:10px">
    <span id="home" class="anchor"></span>
    <h1 style="margin-bottom:0px;"><a href="#home">Home</a></h1>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
  </div>

  <div class="w3-full section" style="margin-left: 10px; margin-right:10px">
    <span id="about" class="anchor"></span>
    <h1><a href="#about">About</a></h1>
    <span id="1" class="anchor"></span>
    <h3 style="margin-bottom:0px;"><a href="#1">Sample Title</a></h3>

    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>

    <span id="2" class="anchor"></span>
    <h3 style="margin-bottom:0px;"><a href="#2">Sample Title 2</a></h3>

    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>

  </div>

  <div class="w3-full section" style="margin-left: 10px; margin-right:10px">
    <span id="contact" class="anchor"></span>
    <h1><a href="#contact">Contact</a></h1>

    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>

    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>

  </div>


</div>

关于javascript - 不同标题类型之间的 anchor 标记行为不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49803890/

相关文章:

javascript - 使用 jquery 在视频上循环淡入淡出

javascript - 如何使溢出 y 滚动响应的 div(高度明智)?

javascript - 在 Canvas 中创建流畅的动画

css - 覆盖继承的 CSS 高度

html - 使英雄形象响应

php - 铜版哥特式灯在 Firefox 21.0 中不起作用

javascript - JSHint 错误 : Functions declared within loops referencing an outer scoped variable may lead to confusing semantics

javascript - jquery 移动更改为下一个和上一个 data-role=page

html - 图形 : When to use footer, 中的 block 引用何时使用 figcaption?

java - 用户进入特定网页后如何禁用刷新按钮?