gpt4 book ai didi

javascript - 我希望我的 anchor 链接在 div 之前停止

转载 作者:太空宇宙 更新时间:2023-11-03 18:29:30 25 4
gpt4 key购买 nike

我有一个 jquery 脚本,它为我的 anchor 链接添加了一个“缓动”效果,唯一的问题是现在我有一个粘性导航,所以当导航滑动到 DIV 时它隐藏了它的一部分。

有没有办法将导航滑动到 div 开始之前?

anchor 链接缓动效果:

$(function () {
"use strict";
var $root = $('html, body');
$('a').click(function () {
var href = $.attr(this, 'href');
$root.animate({
scrollTop: $(href).offset().top
}, 500, function () {
window.location.hash = href;
});
return false;
});
});

HTML

        <nav>
<ul class="navigation">
<li><a href="#donate">How to donate</a></li>
<li><a href="#locations">Drop-off locations</a></li>
<li><a href="#party">Party details</a></li>
</ul>
</nav>

谢谢。

最佳答案

我将使用此代码进行从顶部到所需部分的单个号召性用语(纯 JavaScript)

const scrollerBtn = document.getElementById('btn');
const sectionView= document.getElementById('section-view');
var headerHeight = document.getElementById('main-header').offsetHeight;

scrollerBtn.addEventListener('click', () => {

// scroll to your element
sectionView.scrollIntoView(true);

// now calculate fixed header
var scrolledY = window.scrollY;

if (scrolledY) {
window.scroll(0, (scrolledY - headerHeight));
}
})

关于javascript - 我希望我的 anchor 链接在 div 之前停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20152479/

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