$(function() {
//先将#back-top隐藏
$('#back-top').hide();
//当滚动条的垂直位置距顶部100像素一下时,跳转链接出现,否则消失
$(window).scroll(function() {
if ($(window).scrollTop() > 100) {
$('#back-top').fadeIn(1000);
} else {
$("#back-top").fadeOut(1000);
}
});
//点击跳转链接,滚动条跳到0的位置,页面移动速度是1000
$("#back-top").click(function() {
$('body').animate({
scrollTop: '0'
}, 1000);
return false; //防止默认事件行为
})
})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>返回顶部</title>
<script src="scripts/jquery-1.7.1.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(function(){
var toppos = 0;
$(".back_top_index").hide();
$(document).scroll(
function(){
toppos=$(document).scrollTop();
var oClient =$(window).height();//当前窗口可视区域高度
if(toppos>oClient){
$(".back_top_index").fadeIn();
}else if(toppos==0){
$(".back_top_index").fadeOut();
}
}
)
$(".back_top_index").click(function(){
$("html,body").animate({scrollTop:0})
})
})
</script>
<div style="height:2000px;">3423424</div>
<div class="back_top_index">1</div>
<style>
.back_top_index{width: 50px;height: 50px;position: fixed;bottom: 10px;right: 10px;background-color: red;}
</style>
</body>
</html>