jq简单实现点击回到顶部功能

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="js/jquery-2.1.0.js" ></script>
<title></title>
<style>
.gao{
width: 100%;
height: 1100px;
}
.top{
width: 80px;
height: 60px;
line-height: 60px;
position:fixed;
right: 10px;
bottom: 30px;
background: green;
color: white;
text-align: center;
display: none;
cursor: pointer;
}
</style>
</head>
<body>
<div class="gao"></div>
<div class="top">回到顶部</div>
</body>
<script>
$(document).scroll(function(){
var scrtops = $(document).scrollTop();
if(scrtops>110){
$('.top').fadeIn();
}else{
$('.top').fadeOut();
}
console.log(scrtops)
})
$('.top').click(function(){
var s =$('body').scrollTop();
$('body').animate({scrollTop:0},100)
})
</script>
</html>

posted @ 2017-03-12 18:20  松前月下  阅读(1361)  评论(0编辑  收藏  举报