gpt4 book ai didi

javascript - 平滑滚动到特定 Div

转载 作者:行者123 更新时间:2023-11-28 14:59:00 24 4
gpt4 key购买 nike

我有一个链接并试图使其平滑滚动,它可以工作,但它使所有链接都处于事件状态,我只想将其用于此特定链接,而不是其他链接

<a href="#shelf"></a>

<div id="shelf">content</div>


$(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 !== "") {
// 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
});
});

最佳答案

然后在 jQuery 选择器中更加具体变化:

<a href="#shelf"></a>

还有

 $("a").on('click', function (event) {

对于:

<a href="#shelf" id="myspecificlink"></a>

还有

 $("a#myspecificlink").on('click', function (event) {

关于javascript - 平滑滚动到特定 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41756586/

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