javascript - 如何使用 javascript 制作从链接到 anchor 的滚动动画?

标签 javascript jquery html scroll

http://www.littlestonegroup.org

如果您单击“学习”,我希望它向下滚动到下一个链接。我可以让它捕捉到使用 anchor 链接,但我想要有滚动效果。我看过很多教程,但我不确定如何安装 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

您可以通过将上述函数包装在以下内容中将其添加到结束正文标记之前:

<script type="text/javascript">

    function here

</script>

关于javascript - 如何使用 javascript 制作从链接到 anchor 的滚动动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25028221/

相关文章:

javascript - 使用 .each() 方法验证 jquery 中的表单字段

javascript - 仅在向上滚动时固定导航栏

javascript - 汇总多个选择框的值

html - 将剪辑路径添加到具有额外垂直空间的 svg 图像元素

html - 如何在 Azure Linux Web 应用程序上托管静态 HTML 页面?

javascript - 无法使用 localStorage 手动生成 id

javascript - ajax post 调用不工作

javascript - 要求用户共享的JS对话框, "message"参数什么也没做?

javascript - API 应为 %100 同步或 %100 异步示例

html - 将 Bootstrap 侧边栏扩展到网页末尾