0
当我在page上点击锚(即南澳大利亚州,SA餐饮指南,SA风格,SA住宿指南,背包客短途休息澳大利亚)时,由于标头很粘,部分会稍微向上移动。如何用固定导航栏控制锚点的位置?
当我在page上点击锚(即南澳大利亚州,SA餐饮指南,SA风格,SA住宿指南,背包客短途休息澳大利亚)时,由于标头很粘,部分会稍微向上移动。如何用固定导航栏控制锚点的位置?
您应该调整此值scrollTop: $(hash).offset().top
。如custom.js中的scrollTop: $(hash).offset().top+50;
或scrollTop: $(hash).offset().top-50;
。
$(document).ready(function(){
// Add smooth scrolling to all links
$(".hashtag 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-100
// scrollTop: $(hash).offset().top+50;
}, 1000, function(){
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
} // End if
});
});
在页脚中添加此脚本以添加顶部偏移量。
<script type="text/javascript">
$(document).ready(function(){
// Add smooth scrolling to all links
$(".hashtag 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
var target_offset = $(hash).offset() ? $(hash).offset().top : 0;
//change this number to create the additional off set
var customoffset = 40
$('html, body').animate({scrollTop:target_offset - customoffset}, 1000, function(){
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
} // End if
});
});
</script>
如果我将这个“scrollTop:$(hash).offset()。top + 50;”添加到我的代码中,那么它不会像以前一样顺利滚动到该位置。 – Priya
在这里除去';'分号'scrollTop:$(hash).offset()。top + 50;'。它应该是'scrollTop:$(hash).offset()。top + 50' – vel
set'scrollTop:$(hash).offset()。top-100;' – vel