先上代码:
function b(){
h = $(window).height();
t = $(document).scrollTop();
if(t > h){
$('#gotop').show();
}else{
$('#gotop').hide();
}
}
$(document).ready(function(e) {
b();
$('#gotop').click(function(){
$(document).scrollTop(0);
})
});
$(window).scroll(function(e){
b();
})
注意:$(window).height()
和$(document).height()
的区别`
$(window).height()
代表了当前可见区域的大小,
$(document).height()
则代表了整个文档的高度,可视具体情况使用.
注意:当浏览器窗口大小改变时(如最大化或拉大窗口后) ,
$(window).height()
随之改变,但是$(document).height()
是不变的。
在实际使用中是需要比较二者大小的。