页面评论功能,当评论较多时,有时须要滚动到评论头部。
能够使用scrollTop方法,加上一点延时动画(animate),可訪问在线演示,代码大体例如以下:
<html>
<script src="//wow.techbrood.com/libs/jquery/jquery.min.js"></script>
<script>
$(document).ready(function (){
$("#scroll").click(function (){
$('html, body').animate({
scrollTop: $("#div1").offset().top
}, 2000);
});
});
</script>
<ul id="commentlist">
<li style="text-align: left">
<img src="" alt="">
<p class="name">iefreer</p>
<span class="time">2015-07-17 16:38:45</span>
<p class="comment_content">comment1</p>
</li>
<li style="text-align: left">
<img src="" alt="">
<p class="name">iefreer</p>
<span class="time">2015-07-17 16:38:25</span>
<p class="comment_content">comment2</p>
</li>
<li style="text-align: left">
<img src="" alt="">
<p class="name">iefreer</p>
<span class="time">2015-07-17 16:38:02</span>
<p class="comment_content">comment3</p>
</li>
</ul>
<button id="scroll">Scroll me</button>
</html>