<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>仿新浪网“返回顶部”效果</title>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
<style type="text/css">
.backToTop { display: none; width: 18px; line-height: 1.2; padding: 5px 0; background-color: #000; color: #fff; font-size: 12px; text-align: center; position: fixed; _position: absolute; right: 10px; bottom: 100px; _bottom: "auto"; cursor: pointer; opacity: .6; filter: Alpha(opacity=60); }
</style>
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
</head>
<body>
<p>
如果预览时左下角提示错误,<font color="red">请先刷新一下本页面</font>就可以看到效果了。</p>
<p>
拖动滚动条到页面底部就会显示“返回顶部”按钮。</p>
<p>
更多代码请访问:<a href="http://www.lanrentuku.com/" target="_blank">懒人图库</a></p>
<p>
</p>
<div style="height: 2000px;">
</div>
<script type="text/javascript">
(function () {
var $backToTopTxt = "返回顶部", $backToTopEle = $('<div class="backToTop"></div>').appendTo($("body"))
.text($backToTopTxt).attr("title", $backToTopTxt).click(function () {
$("html, body").animate({ scrollTop: 0 }, 120);
}), $backToTopFun = function () {
var st = $(document).scrollTop(), winh = $(window).height();
(st > 0) ? $backToTopEle.show() : $backToTopEle.hide();
//IE6下的定位
if (!window.XMLHttpRequest) {
$backToTopEle.css("top", st + winh - 166);
}
};
$(window).bind("scroll", $backToTopFun);
$(function () { $backToTopFun(); });
})();
</script>
</body>
</html>
二、
*{padding:0; margin:0}
body{height:2000px}
#totop{position:fixed;bottom:40px;right:10px;z-index:999;width:71px; cursor:pointer; display:none;}
*html #totop{position:absolute;cursor:pointer;right:10px; display:none;top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight)-112+"px")}
#totop a{display:block;width:71px;height:24px;padding-top:48px;background:url(../images/toTop.gif) no-repeat;text-align:center;color:#888}
#totop a.cur{background-position:-88px 0;text-decoration:none;color:#3a9}
02 |
$.fn.manhuatoTop = function (options) { |
07 |
var options = $.extend(defaults,options); |
08 |
$( "body" ).prepend( "<div id='totop'><a>返回</a></div>" ); |
10 |
var $top = $( "#totop" ); |
11 |
var $ta = $( "#totop a" ); |
12 |
$toTop.scroll( function (){ |
13 |
var scrolltop=$( this ).scrollTop(); |
14 |
if (scrolltop>=options.showHeight){ |
22 |
$( this ).addClass( "cur" ); |
24 |
$( this ).removeClass( "cur" ); |
26 |
$top.click( function (){ |
27 |
$( "html,body" ).animate({scrollTop: 0}, options.speed); |
30 |
});
用法如下:
<script type="text/javascript">
$(function (){
$(window).manhuatoTop({
showHeight : 100,//设置滚动高度时显示
speed : 500 //返回顶部的速度以毫秒为单位
});
});
</script>
|