javascript - 如何使用 jQuery 单击按钮滚动到页面底部的某个元素/div?
问题描述
我已经尝试了不同的变体,但它仍然对我不起作用。我正在尝试添加一个动画,以便当我单击按钮时,它会向下滚动到页面上的某个元素。
这是我的代码:
<script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</script>
<!-- jQuery code to show the working of this method -->
<script>
$(document).ready(function() {
$("#nav-projects").click(function() {
$("html, body").animate({
scrollTop: $(
'html, body').get(0).scrollHeight
}, 2000);
});
});
</script>
解决方案
没有 jQuery ( $('#yourelement').offset().top; ) 或 Javascript的 CSS 的另一种方法是:
html {
scroll-behavior: smooth;
}
<a href="#A">Goto A</a>
<a href="#B">Goto B</a>
<a href="#C">Goto C</a>
<hr>
<div id="A" style="height:500px;">Section A</div>
<div id="B" style="height:500px;">Section B</div>
<div id="C" style="height:500px;">Section C</div>
推荐阅读
- java - 添加绑定以进行验证
- unit-testing - 如何模拟 rxjs 6 webSocket 函数进行单元测试?
- vert.x - Vertx 3.6.3:带有 DeliveryOptions 的 eventBus.send() 失败
- animation - Xamarin.Forms ScrollView - 随着它的扩展动画它的增长
- spring-boot - 错误 - 兔子模板发布确认 - 回复代码 = 403,回复文本 = ACCESS_REFUSED - 无法发布到内部交换
- google-chrome-extension - 内容脚本大量计算导致性能不佳(chrome扩展)
- java - 如何在 Switch case 中分配价格值?
- google-cloud-composer - 拒绝访问 Airflow 网络服务器
- fortran - 现代 Fortran:从后代调用祖先过程
- javascript - 为什么使用匿名函数作为全局对象?