jquery回到页面顶端

html代码:

<h1 id="anchor">页面标题</h1>
<div id="container">页面内容</div>
<p><a href="#anchor" class="topLink">回到顶端</a></p>

css代码:

#container{
   height: 1000px;
   line-height: 1000px;
   text-align: center;
   font-family: Arial;
   border: 1px solid #EEEEEE;
}

.topLink{
  float:right;
  padding:10px;
  color: #CCC;
}

js代码:

$(document).ready(function() {

	$("a.topLink").click(function() {
		$("html, body").animate({
			scrollTop: $($(this).attr("href")).offset().top + "px"
		}, {
			duration: 500,
			easing: "swing"
		});
		return false;
	});

});

相关推荐