我的项目中有一个可折叠的 div,一旦用户单击它并且它展开,我需要计算机向下滚动,以便用户知道文本在那里。 div 的扩展有效,但由于它尝试自动扩展(在 div 扩展之前),并且因为这是页面的底部(在 Div 打开之前),所以它不会滚动。所以我想做的是在我的 Javascript 中添加 3 秒的延迟,这样在 Div 打开之前它不会尝试这样做。我无法让它工作。这是我目前拥有的代码:
<script language="Javascript">
function scrollRefund() {
var divPosition = $('#refund').offset();
setTimeout(function(){
$('html, body').animate({ scrollTop: divPosition.top }, "slow");
}, 1);
}
</script>
<button type="button" class="btn-link" data-toggle="collapse" data-target="#refund" onclick="javascript: scrollRefund();">Refund Policy</button>
<div id="refund" class="collapse">
最佳答案
setTimeout 的第二个参数以毫秒为单位。尝试使用 3000 而不是 1。
<script language="Javascript">
function scrollRefund() {
var divPosition = $('#refund').offset();
setTimeout(function(){
$('html, body').animate({ scrollTop: divPosition.top }, "slow");
}, 3000);
}
</script>
<button type="button" class="btn-link" data-toggle="collapse" data-target="#refund" onclick="javascript: scrollRefund();">Refund Policy</button>
<div id="refund" class="collapse">
关于3 秒后运行 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35348244/