使用说明:
在html标签中加入 toTop 属性,就可实现返回顶部的功能。该属性的值为返回顶部的时间;
功能介绍:
滚动条在顶部时,返回顶部图标隐藏,滚动条离开顶部,返回顶部图标显示。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="jquery-1.8.3.js"></script>
<style type="text/css">
*[toTop]{
width:40px;
height:40px;
position:fixed;
bottom:30px;
right:30px;
font-size:16px;
border:1px solid #000;
display:none;
cursor:pointer;
text-align:center;
padding:10px
}
</style>
<script language="javascript">
$(document).ready(function(e) {
$(window).scroll(function(){
if($(window).scrollTop() > 100) {
$("*[toTop]").fadeIn(100);
}else{
$("*[toTop]").fadeOut(100);
}
});
$("*[toTop]").click(function(){
$('body,html').animate({scrollTop:0},parseInt($("*[toTop]").attr("toTop")));
});
});
</script>
<title>返回顶部</title>
</head>
<body>
<div toTop='0'>返回顶部</div>
<div style="height:2000px; width:100px;"></div>
</body>
</html>