jQuery回到顶部

在这里插入代码片<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>回到顶部</title>
		<style>
			*{margin: 0;
			padding: 0;
		}
		.container{
			width: 1200px;
			height: 100%;
			margin:0 auto;
		}
		.header{
			height: 200px;
		}
		.nav{
			height: 80px;
		}
		.content{
			height: 900px;
		}
		.footer{
			height: 240px;
		}
		.header .container{
			background-color: orange;
		}
		.nav .container{
			background-color: gray;
		}
		.content .container{
			background-color: yellow;
		}
		.footer .container{
			background:#333; color:#fff;
		}
		.t{
			line-height: 80px;
			width: 80px;
			border:1px solid #f0f0f0;
			position: fixed;
			right: 50px;
			bottom:50px;
			text-align: center;
			background:#f30;
		}
		</style>
</head>
<body>
	<div class="header"><div class="container">头部</div></div>
	<div class="nav"><div class="container">导航</div></div>
	<div class="content"><div class="container">内容</div></div>	
	<div class="footer"><div class="container">页脚</div></div>
	<div class="t">TOP</div>
	<script src="http://libs.baidu.com/jquery/2.0.0/jquery.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
		$(function(){
			$(".t").on("click",function(){
				$("html").animate({"scrollTop":0},1000);
			})
			$(window).scroll(function(){
				var str=$(window).scrollTop();
				if(str>300){
					$(".t").show();
				}else{
					$(".t").hide();
				}
			})
		})
	</script>
</body>
</html>``
发布了35 篇原创文章 · 获赞 7 · 访问量 786

猜你喜欢

转载自blog.csdn.net/skf063316/article/details/103910005