繁体   English   中英

平滑滚动到特定的Div

[英]Smooth scroll to specific Div

我有一个链接,尝试使其平滑滚动,它可以工作,但它使所有链接都处于活动状态,我只希望该特定链接具有其他功能

<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) {

更改

$("a").on("click", function(){ ... });

$("a#toshelf").on("click", function(){ ... }); 

并将其作为您的HTML

<a href="#shelf" id="toshelf">Click me!</a>

代替使用标签选择器,请使用id选择器,如下所示:

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

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


$(document).ready(function ()
{      
   $("#a1").on('click', function (event)
    {
      //Your code here        
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM