點擊這裡體驗效果
這個效果模仿 QQ 空間右下角的懸浮工具列,點擊「頂部」按鈕即可平滑回到頁面頂端。以下是原始碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>qq 空間返回頂部 jqurey 效果-</title>
<style>
.fix-layout { bottom: 20px; position: fixed; right: 20px; z-index: 20; }
.gb-operation-area { width: 45px; border: 1px solid #C7E5EB; }
.gb-operation-area .gb-operation-button {
display: block; height: 45px; overflow: hidden;
position: relative; text-decoration: none; background-color: #F2FDFF; color: #7E8A8C;
}
.gb-operation-area .gb-operation-button .gb-operation-text { display: none; margin-top: 15px; text-align: center; }
.gb-operation-area .gb-operation-button:hover .gb-operation-text { display: block; }
.gb-operation-area .gb-operation-button:hover .gb-operation-icon { display: none; }
</style>
<script type="text/javascript" src="https:///keleyi/pmedia/jquery/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(function(){
$('#goto_top_btn').click(function() {
var s = $(window).scrollTop(),h = $(window).height();
if (s > h * 2) {
$('html, body').scrollTop(0);
} else {
$('html,body').animate({scrollTop: '0px'}, 300);
}
});
});
</script>
</head>
<body style="height:3000px;margin:0px;background-color:Olive">
<div style="background-color:Red; width:100%;height:150px;">歡迎</div>
<div style="background-color:Yellow; width:100%;height:150px;">hi</div>
<div class="fix-layout">
<div id="_returnTop_layout_inner" class="gb-operation-area">
<a id="goto_top_btn" class="gb-operation-button return-top" href="javascript:;">
<i class="gb-operation-icon" title="返回頂部"></i>
<span class="gb-operation-text">頂部</span>
</a>
</div>
</div>
</body>
</html>