gpt4 book ai didi

javascript - 如何使这个滚动条高于内容 120px?

转载 作者:行者123 更新时间:2023-11-30 20:04:14 24 4
gpt4 key购买 nike

当我应用此脚本时,它会转到链接,但我希望它比所链接的内容高出 120 像素。

代码如下:

<script>
$(document).ready(function(){
// 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 !== 0) {
// 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
});
});
</script>

编辑#02

这是我的 HTML、CSS 和 Java,供那些想知道的人使用:

https://codepen.io/crosso_7/pen/WYegpY

最佳答案

只需从您的计算中减去像素数量即可。

  $(document).ready(function(){
// 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 !== 0) {
// 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 -120
}, 800, function(){

// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
} // End if
});
});

关于javascript - 如何使这个滚动条高于内容 120px?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53084493/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com