【问题标题】:Scrolling from one div to another using jQuery (should be easy)使用 jQuery 从一个 div 滚动到另一个(应该很容易)
【发布时间】: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 但 NEXT 2 会出错,因为没有可滚动的内容,只需删除第二个按钮

标签: jquery scroll jquery-animate


【解决方案1】:

单击最后一个按钮时会出现此错误,因为他没有.question,因此$(this).parents().next('.question') return undefined,您可以添加类似 sn-p 显示的条件。

//Get the next question
var next_question = $(this).parents().next('.question');

// Check if the question exist them animate
if (next_question.length) {
    $('html, body').animate({
      scrollTop: next_question.offset().top
    }, 1000);
}

$(".next_btn").click(function() {
  var next_question = $(this).parents().next('.question');

  if (next_question.length) {
    $('html, body').animate({
      scrollTop: next_question.offset().top
    }, 1000);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<br><br><br><br><br><br>
<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>

<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>

<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>
<br><br><br><br><br><br><br><br><br>

【讨论】:

  • 所以你投反对票并投票删除我的,然后复制我的答案,干得好
  • 当我试图删除我的时,我错误地投票删除,我没有投反对票。
  • 如果你不删除你的,恐怕我不得不这样做:(
  • 我现在删除它,我只是想证明它不是我的,我没有为报复投反对票,我对无用的帖子投反对票......
  • 谢谢你们的帮助
猜你喜欢
  • 2016-11-05
  • 2015-11-01
  • 2011-03-26
  • 2019-05-19
  • 1970-01-01
  • 2014-04-25
  • 1970-01-01
  • 1970-01-01
  • 2011-11-06
相关资源
最近更新 更多