话不多说,直接上代码
<!DOCTYPE html>
<html lang="en"><head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{
margin:0;
padding: 0;
}
#header {
height: 45px;
background-color: #d0cccc;
}
#top{
position: fixed;
bottom:0;
right:0;
width:45px;
height:45px;
background:#d0cccc;
border-radius:50%;
}
</style>
</head>
<body style="height:900px;">
<div id="header"></div>
<div id="top"></div>
</body>
<script src="jquery-1.11.1.js"></script>
<script>
$(function() {
jQuery(document).ready(function($) {
$body = (window.opera) ? (document.compatMode == "CSS1Compat" ? $('html') : $('body')) : $('html,body'); // 这行是 Opera 的补丁, 少了它 Opera 是直接用跳的而且画面闪烁 by willin
$('#top').click(function() {
$body.animate({
scrollTop: $('#header').offset().top
}, 1000);
return false; // 返回false可以避免在原链接后加上#
});
});
});
$(function(){
$("#top").hide();
$(window).scroll(function(){
var clientH = $(window).height();
var scrollTop = $(window).scrollTop();
if(scrollTop > clientH/4){
$("#top").show();
}else{
$("#top").hide();
}
});
});
</script>
</html>