jquery點選返回網頁頂部程式碼

antzone發表於2017-04-05

本章節分享一段簡單的程式碼例項,它實現了點選平滑返回網頁頂部的功能。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com" />
<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一章節。

相關文章