javascript - Jquery延迟滚动到#ID
问题描述
我正在尝试在一段时间后将 Scroll 应用于 div。此代码运行良好,因为它会延迟滚动,但我不知道如何将其应用于特定的 DIV ID。
$(document).ready(function(){
setTimeout(function (){var scroll= $(window).scrollTop();
scroll= scroll+ 800;
$('html, body').animate({scrollTop: scroll}, 5000);}, 5000);
});
解决方案
您可以使用该offset()
方法获取文档中元素的位置top
和left
位置。
$(document).ready(function() {
setTimeout(function() {
//get the offset of the target in the page
var scroll = $('#target').offset().top;
$('html, body').animate({
scrollTop: scroll
}, 2000);
}, 2000);
});
#target {
background-color: red;
width: 400px;
min-height: 1400px;
margin-top: 1000px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="target"></div>
推荐阅读
- cadence-workflow - 使用 Cadence 匹配服务的 UpdateTaskList 操作失败
- python - 删除不以子字符串结尾的行
- javascript - 记录到控制台的 Javascript 对象中显示的意外行为
- azure-ad-b2c - Azure B2C 自定义 UI 空 div API
- java - 为什么我的矩形形状在 Java 图形 2d 中如此之小
- angular - 如何删除图片中的第二个标题?
- javascript - D3.js - scale.clamp() 打破缩放
- java - 重构 if-else 块以使用 Map.computeIfAbsent Map.computeIfPresent 错误结果
- fastapi - FASTAPI - Pydantic:TypeError:必须是实数,而不是 NoneType
- android - 为什么 TabBar View 隐藏在 Tab Bar 下?