gpt4 book ai didi

javascript - 如何向div添加平滑滚动

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

我知道已经有对此的答案,但我进行了搜索,但没有一个有效,我不知道为什么。

所以我想要的是通过标签平滑滚动到 div

我想要的是this

所以我拿了 js 代码,但在我身上根本不起作用,滚动完全失去了作用。

这是我的代码:wHTML

<div class="col-lg-3 col-md-3 col-sm-3" id="some">
<a href="#info" id="normal">
<img src="imgs/product/normal.png" class="img-responsive">
</a>
</div>

<!-- OTHER CONTENT -->

<div id="info">
Go here
</div>

还有 JS:

$("#some a[href^='#']").on('click', function(e) {
// prevent default anchor click behavior
e.preventDefault();
// animate
$('html, body').animate({
scrollTop: $(this.hash).offset().top
}, 300, function() {
// when done, add hash to url
// (default click behaviour)
window.location.hash = this.hash;
});
});

最佳答案

我不知道您是否复制 > 粘贴了您的代码,但您在 id 前缺少引号:

<div class="col-lg-3 col-md-3 col-sm-3 id="some">

这可能是为什么它不起作用的解决方案,因为 $('#some a')那时不存在。

关于javascript - 如何向div添加平滑滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24610878/

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