在下面的示例中,单击“主页”或“关于”标题将正确跳转到这些部分。但是,单击“样本标题”和“样本标题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()">☰</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>
答案 0 :(得分:3)
你的锚的position:absolute
正在弄乱它的位置;将其更改为relative
。
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
&#13;
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;
}
&#13;
<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()">☰</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>
&#13;
答案 1 :(得分:2)
至少在Chrome中,这似乎是由您应用于锚点的position:absolute
引起的。如果要在不影响流量的情况下更改元素的位置但在文档中维护元素的流程,则可以尝试使用transform:translateY(-50px)
:
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
&#13;
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;
width: 0;
height: 0;
transform: translateY(-50px);
visibility: hidden;
}
p {
height: 200px;
}
&#13;
<!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()">☰</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>
&#13;