我有一个链接,尝试使其平滑滚动,它可以工作,但是它使所有链接都处于活动状态,我只希望该特定链接具有其他功能
<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/