经常要用到返回顶部/底部的功能, 可惜网上找的都不尽人意, 复制过来大部分都用不了, 一怒之下自己做了个。
文件组织:
0. scrolltop.png (背景透明)
1. Style.css
- .scrolltools {
- position: fixed;
- top:100px;
- right:10px;
- display: block;
- margin: -30px 0 0 2px;
- width: 40px;
- background: #f4f4f4;
- border: 1px #cdcdcd solid;
- border-radius: 3px;
- border-top: 0;
- cursor: pointer;
- }
- .scrolltools a {
- background: url("images/scrolltop.png");
- border-top: 1px solid #CDCDCD;
- color: #787878;
- display: block;
- height: 24px;
- line-height: 12px;
- padding: 3px 5px;
- text-align: center;
- text-decoration: none;
- width: 30px;
- }
- .toTop { background-position: 0 0 !important; }
- .toReply { background-position: 0 -30px !important; }
- .toList { background-position: 0 -60px !important; }
- .toBottom { background-position: 0 -90px !important; }
- .toTop:hover { background-position: -40px 0 !important; }
- .toReply:hover { background-position: -40px -30px !important; }
- .toList:hover { background-position: -40px -60px !important; }
- .toBottom:hover { background-position: -40px -90px !important; }
2. scroll_tools.js
- /*
- Author : yenange
- Description : "返回顶部/快速回复/返回列表/返回底部" 边栏功能实现
- Date : 2013-11-06
- */
- (function ($) {
- $.fn.scrolltools = function ( op ) {
- var defaultOp = {
- showToTop : true,
- showToReply : false,
- showToList : false,
- showToBottom : true,
- alwaysShowToTop : true,
- alwaysShowToBottom : true,
- hiddenTopIconHeight : 100,
- hiddenBottomIconHeight : 100,
- replayFn : function(){ alert('进入回复'); },
- listFn : function(){ alert('进入列表'); }
- };
- op = $.extend(defaultOp, op);
- var $outDiv = $(this); //外围div
- var $toTop = $outDiv.find(".toTop"); //返回顶部
- var $toReply = $outDiv.find(".toReply"); //快速回复
- var $toList = $outDiv.find(".toList"); //返回列表
- var $toBottom = $outDiv.find(".toBottom"); //返回底部
- //将设置为不显示的元素给隐藏
- $toTop.toggle(op.showToTop);
- $toReply.toggle(op.showToReply);
- $toList.toggle(op.showToList);
- $toBottom.toggle(op.showToBottom);
- //将外层位置设置在页面中部
- $outDiv.css( {"top" : ($(window).height())/2 });
- $(window).resize(function(){
- $outDiv.css( {"top" : ($(window).height())/2 });
- });
- //如果 [向上图标] 需要显示,但不需要一直显示 ( 离顶部距离很小时消失 )
- if(op.showToTop===true && op.alwaysShowToTop===false){
- $(window).scroll(function () {
- $toTop.toggle( $(window).scrollTop() > op.hiddenTopIconHeight );
- });
- }
- //如果 [向下图标] 需要显示,但不需要一直显示 ( 离底部距离很小时消失 )
- if(op.showToBottom===true && op.alwaysShowToBottom===false){
- $(window).scroll(function () {
- $toBottom.toggle( op.hiddenBottomIconHeight + $(window).scrollTop() < $(document).height() - $(window).height() );
- });
- }
- // [向上图标] 需要显示, 为其定义事件
- if(op.showToTop===true){
- $toTop.click(function(){
- window.scroll(0,0);
- });
- }
- // [向下图标] 需要显示, 为其定义事件
- if(op.showToBottom===true){
- $toBottom.click(function(){
- window.scroll(0,$(document).height() - $(window).height());
- });
- }
- // [快速回复] 需要显示, 为其定义事件
- if(op.showToReply===true && typeof(op.replayFn)!="undefined"){
- $toReply.click(function(){
- op.replayFn();
- });
- }
- // [返回列表] 需要显示, 为其定义事件
- if(op.showToList===true && typeof(op.listFn)!="undefined"){
- $toList.click(function(){
- op.listFn();
- });
- }
- }
- })(jQuery);
3. 测试页面: 1.htm
- <!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>
- <link href="scroll_tools/css/Style.css?1" rel="stylesheet" type="text/css" />
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
- <script src="scroll_tools/js/scroll_tools.js" type="text/javascript"></script>
- <script type="text/javascript">
- //插件调用
- $(function(){
- $(".scrolltools").scrolltools({ alwaysShowToTop:true, alwaysShowToBottom:true });
- });
- </script>
- </head>
- <body>
- <div style="height:1500px;background-color:white;" >
- <div class="scrolltools">
- <span>
- <a title="返回顶部" class="toTop"></a>
- </span>
- <span>
- <a title="快速回复" class="toReply"></a>
- </span>
- <span>
- <a title="返回列表" class="toList"></a>
- </span>
- <span>
- <a title="返回底部" class="toBottom"></a>
- </span>
- </div>
- </div>
- </body>
- </html>