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