先上代码:
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()是不变的。
在实际使用中是需要比较二者大小的。
本文介绍了一种使用JavaScript监听页面滚动并显示返回顶部按钮的方法。通过比较窗口高度($(window).height())与文档高度($(document).height())来判断是否显示返回顶部按钮。此外,还解释了这两种高度获取方式的区别。
761

被折叠的 条评论
为什么被折叠?



