简体   繁体   中英

jQuery smooth scrolling to anchor after button clicked

I can't get this to smoothly scroll down to the anchor for some reason. Can anybody see why?

This is the html:

<div id="onward">
    <a href="#xdroneslogan" class="btn">
        <i class="icon-chevron-down icon-white"></i> Onward
    </a>
</div>

This is the javascript:

<script>
    $("#onward a").click(function(){
      var onwardId = $(this).attr("href");
      $("html, body").animate({scrollTop: $(onwardId).offset().top}, "slow");
      return false;
    });
</script>

Try this out:- http://jsfiddle.net/adiioo7/bguAG/1/

JS:-

$("#onward a").click(function () {
    var onwardId = $(this).attr("href");
    $("html, body").animate({
        scrollTop: $(onwardId).offset().top
    }, "slow");
    return false;
});

HTML:-

<div id="xdroneslogan" style="height:1000px;background:red;"></div>
<div id="onward">
<a href="#xdroneslogan" class="btn"><i class="icon-chevron-down icon-white"></i> Onward</a>

</div>

Also make sure the jQuery.fx.off is set to false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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