gpt4 book ai didi

javascript - 由于 header 导致 scrollto 偏移 javascript 的问题

转载 作者:行者123 更新时间:2023-11-29 23:15:12 26 4
gpt4 key购买 nike

我需要有关此 scrollto 代码片段的帮助。问题是我需要设置一个偏移量来说明我的菜单。如果没有偏移量,我滚动到的标题将被隐藏在菜单下方。在这里自己看看:https://julyfx.mystagingwebsite.com/stanford-mba-msx-essay-topic-analysis-examples/有人会碰巧有建议吗?谢谢你!利亚

<script type="text/javascript">
jQuery(document).ready(function($) {


$('a[href*="#"]')
// Remove links that don't actually link to anything
.not('[href="#"]')
.not('[href="#0"]')
.click(function(event) {
// On-page links
if (
location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '')
&&
location.hostname == this.hostname
) {
// Figure out element to scroll to
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
// Does a scroll target exist?
if (target.length) {
// Only prevent default if animation is actually gonna happen
event.preventDefault();
$('html, body').animate({
scrollTop: target.offset().top
}, 1000, function() {
// Callback after animation
// Must change focus!
var $target = $(target);
$target.focus();
if ($target.is(":focus")) { // Checking if the target was focused
return false;
} else {
$target.attr('tabindex','-1'); // Adding tabindex for elements not focusable
$target.focus(); // Set focus again
};
});
}
}
});
});
</script>

最佳答案

应该相对容易:如果你的标题是 85px 高,你可以将这些 85px 添加到代码中的偏移量,所以这一行

scrollTop: target.offset().top

成为

scrollTop: target.offset().top - 85

这样窗口将比计算的少滚动 85 像素,因此该部分不会隐藏在标题后面。

关于javascript - 由于 header 导致 scrollto 偏移 javascript 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53026768/

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