单击按钮后jQuery平滑滚动锚定

时间:2013-06-11 15:34:30

标签: javascript jquery html css

由于某种原因,我无法顺利向下滚动到锚点。谁能明白为什么?

这是html:

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

这是javascript:

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

1 个答案:

答案 0 :(得分:4)

试试这个: - 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>

还要确保jQuery.fx.off设置为false。