繁体   English   中英

如何使用JavaScript为从链接到锚点的滚动添加动画效果?

[英]How do I use javascript to animate a scroll from a link to an anchor?

http://www.littlestonegroup.org

如果您单击“学习”,我希望它向下滚动到下一个链接。 我可以使用锚点链接,但是我想拥有滚动效果。 我看过许多教程,但不确定如何安装JavaScript或如何实现教程建议的脚本。

预先感谢,非常感谢您的帮助!

我在CSS-Tricks上发现了此平滑滚动功能:

$(function() {
  $('a[href*=#]:not([href=#])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        $('html,body').animate({
          scrollTop: target.offset().top
        }, 1000);
        return false;
      }
    }
  });
});

这是作用中的功能JSFiddle

您可以通过将以上函数包装在以下代码中,将其添加到结束body标签之前:

<script type="text/javascript">

    function here

</script>

暂无
暂无

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

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