【发布时间】:2018-07-12 15:16:17
【问题描述】:
当我按下带有next_btn 类的按钮时,我想要做的就是从第一个 div 滚动到第二个 div(两者都有“问题”类)。我觉得我的 jQuery 是正确的,但是,当我单击时没有任何反应。
注意:之后我会添加更多问题,因此我不想为每个问题和每个按钮都使用一个 ID。
控制台错误:
未捕获的类型错误:无法读取未定义的属性“顶部”
$(".next_btn").click(function() {
$('html, body').animate({
scrollTop: $(this).parents().next('.question').offset().top
}, 1000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="question">
<div class="row header">
<div class="col-sm-12 logo">
<img src="q.png" height="350px;">
</div>
</div>
<div class="col-sm-12 buttons" style="left:0px; text-align:center;">
<button class="btn next_btn">NEXT</button><br>
</div>
</div>
<div class="question">
<div class="row header">
<div class="col-sm-12 logo">
<img src="q.png" height="150px;">
</div>
</div>
<div class="row content">
<div class="col-sm-12 buttons" style="left:0px; text-align:center;">
<button class="btn next_btn">NEXT</button><br>
</div>
</div>
</div>
【问题讨论】:
-
您粘贴的代码工作正常,您的代码没有问题,除非您单击第二个/最后一个按钮
-
第一个按钮也给我一个错误。
-
NEXT按钮 1 将页面滚动到图像 2 但NEXT2 会出错,因为没有可滚动的内容,只需删除第二个按钮
标签: jquery scroll jquery-animate