本文介绍如何使用jQuery监听页面滚动事件,当文档滚动高度超过200像素时显示返回顶部按钮,否则隐藏。点击按钮后,页面平滑滚动至顶部,耗时1000毫秒。
jQuery返回顶部按钮实现代码
具体实现代码如下:
<script>
$(function(){
$(window).bind('scroll',function(){
if ($(document).scrollTop() >200) {
".asd-return").show(1);
}else{
".asd-return").hide(1);
}
})
$(window).bind('scroll',function(){
$('body,html').animate({scrollTop:0},1000);
})
})
</script>