我有以下结构化代码。我有粘性的标题,以及我的页面中的几个部分。我这里很少有页面超链接。
正如您在片段中看到的,在第1节到第2节中有一个链接文本。
我从w3school中添加了一些jquery来平滑滚动。
问题
当单击超链接时,它滚动到第2节,并将第2节的起始点移到主体的顶部。因为我有一个粘性的标题,所以它隐藏了第2节的一些内容。
现在我想要的是:当滚动到第2节时,我希望这个部分在粘稠的标题之后开始,而不是从主体的顶部开始。
// Add smooth scrolling to all links
$("a").on('click', function(event) {
// Make sure this.hash has a value before overriding default behavior
if (this.hash !== "") {
// Prevent default anchor click behavior
event.preventDefault();
// Store hash
var hash = this.hash;
// Using jQuery's animate() method to add smooth page scroll
// The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 800, function() {
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
} // End if
});header {
background: red;
width: 100%;
height: 50px;
position: fixed;
top: 0;
left: 0;
right: 0;
}
#sec-1,
#sec-2 {
width: 100%;
height: 100vh;
}
#sec-1 {
margin-top: 50px;
background: green;
}
#sec-2 {
background: blue;
}
#sec-2>p {
background: #fff;
margin: auto;
width: 60%;
height: 100px;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header></header>
<section id="sec-1">
<a href="#sec-2">Scroll to section 2</a>
</section>
<section id="sec-2">
<p>This is section 2</p>
</section>
发布于 2018-09-21 11:58:11
您的问题是javascript中的最后一行。
/ Add hash (#) to URL when done scrolling (default click behavior) window.location.hash = hash;
这实际上迫使您的URL跳转到原来的散列位置。您实际上并没有添加一个#,但是您强制窗口跳转到javascript变量hash (在本例中是section-2 )中定义的原始窗口。
// Add smooth scrolling to all links
$("a").on('click', function(event) {
// Make sure this.hash has a value before overriding default behavior
if (this.hash !== "") {
// Prevent default anchor click behavior
event.preventDefault();
// Store hash
var hash = this.hash;
// Using jQuery's animate() method to add smooth page scroll
// The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top -70
}, 800, function() {
});
} // End if
});卷轴现在按预期工作。
发布于 2018-09-21 11:57:56
你需要从卷轴中移除标题的高度;
$('html, body').animate({
scrollTop: $(hash).offset().top - $('header').height()
}, 800, function() {
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});发布于 2018-09-21 12:03:18
首先,您需要从动画块内的scrollTop偏移量中减去标头的高度。
其次,当您使用window.location.hash时,它会引起实际的麻烦,当window.location.hash被触发时,页面再次滚动以寻找超链接(就像传统的超链接行为一样)。贝娄代码正在正常工作,希望它能解决你的问题。
// Add smooth scrolling to all links
$("a").on('click', function(event) {
// Make sure this.hash has a value before overriding default behavior
if (this.hash !== "") {
// Prevent default anchor click behavior
event.preventDefault();
// Store hash
var hash = this.hash;
// Using jQuery's animate() method to add smooth page scroll
// The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: parseInt($(hash).offset().top - parseInt($('header').height()))
}, 800, function() {
// Add hash (#) to URL when done scrolling (default click behavior)
// window.location.hash = hash;
});
} // End if
});header {
background: red;
width: 100%;
height: 50px;
position: fixed;
top: 0;
left: 0;
right: 0;
}
#sec-1,
#sec-2 {
width: 100%;
height: 100vh;
}
#sec-1 {
margin-top: 50px;
background: green;
}
#sec-2 {
background: blue;
}
#sec-2>p {
background: #fff;
margin: auto;
width: 60%;
height: 100px;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header></header>
<section id="sec-1">
<a href="#sec-2">Scroll to section 2</a>
</section>
<section id="sec-2">
<p>This is section 2</p>
</section>
https://stackoverflow.com/questions/52442661
复制相似问题