jquery点击返回网页顶部代码

快乐打工仔 分类:实例代码

本章节分享一段简单的代码实例,它实现了点击平滑返回网页顶部的功能。

代码实例如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.pipipi.net" />
<title>前端教程网</title>
<style>
.scrollTop{
  width:50px;
  height:1500px;
  margin:0px auto;
  background-color:green;
}
</style>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script type="text/javascript">
$(function(){
  $("input").click(function(){
   $('html, body').animate({scrollTop:0}, 'slow');//回到顶部
  })
})
</script>
</head>
<body>
<div class="scrollTop"></div>
<input type="button" value="回到顶部"/>
</body>
</html>

上面的代码实现了我们的要求,代码非常的简单,这里就不多介绍了,更多内容可以参阅相关阅读。

相关阅读:

(1).animate()方法可以参阅jQuery animate()一章节。

(2).scrollTop属性可以参阅js scrollTop一章节。

回复

我来回复
  • 暂无回复内容