同时使用scrollTop()两次

时间:2014-10-29 12:56:37

标签: javascript jquery

如何使用animate ({ scrollTop() })同时滚动多个DIV?

如果我运行以下goTOP(){;函数的示例。我想滚动到页面顶部,同时滚动到#studentName TABLE的顶部。

 <table class="table table-striped table-hover" id="studentName">
  <thead>
    <tr>
      <th class="col-md-2">ID</th>
      <th class="col-md-2">Subject</th>
    </tr>
  </thead>
  <tbody></tbody>
</table>

#studentName正在滚动条 [编辑]

#studentName{
  overflow-y: scroll;
  max-height: 100px;
}

jQuery:这是我到目前为止所尝试过的。不能按预期工作。

function goTOP(){
  $('html, body').animate({ 'scrollTop' : 0 }, 700);//go top of page
  $('html, body').animate({ 'scrollTop' : $('#studentName').first().offset().top }, 100); // go to top level of this DIV
}

2 个答案:

答案 0 :(得分:2)

替换

$('html, body').animate({ 'scrollTop' : $('#studentName').first().offset().top }, 100); // go to top level of this DIV
}

修改

  $('#studentName').animate({ scrollTop: 0 }, 100); 

答案 1 :(得分:1)

function goTOP(){
  $('html, body').animate({ 'scrollTop' : 0 }, 700);//go top of page
  $('#studentName tbody').animate({ 'scrollTop': 0 }, 100); // go to top level of this DIV
}