jquery封装的时间轴

来源:互联网 发布:2015数据精灵下载 编辑:程序博客网 时间:2024/05/31 19:35


效果


代码

样式文件style.css

[css] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. .timeline{  
  2.     positionabsolute;  
  3.     z-index5000;  
  4.     font-size12px;  
  5.     border1px solid #ccc;  
  6.     background: whitesmoke;  
  7.     background: -webkit-linear-gradient(top, whitesmoke, #ddd);  
  8.     background: -ms-linear-gradient(top, whitesmoke, #ddd);  
  9.     background: -moz-linear-gradient(top, whitesmoke, #ddd);  
  10.     border-radius: 4px 0 4px 0;  
  11.     box-shadow: 0px 0px 10px rgba(150,150,150,0.5);  
  12. }  
  13. .timeline ul.ulvec{  
  14.     margin-left10px;  
  15.     list-stylenone;  
  16.     backgroundurl("dot.gif"0px 0px repeat-y;  
  17.     padding-right10px;  
  18. }  
  19. .timeline ul li.livec{  
  20.     margin-left-43px;  
  21.     padding0px 0px 0px 12px;  
  22.     backgroundurl("biggerdot.png"0px 50% no-repeat;  
  23.     cursorpointer;  
  24. }  
  25. .timeline ul.ulhor{  
  26.     margin0px;  
  27.     padding5px 10px;  
  28.     list-stylenone;  
  29.     backgroundurl("dot.gif"0px 5px repeat-x;  
  30. }  
  31. .timeline ul li.lihor{  
  32.     display: inline-block;  
  33.     margin0px;  
  34.     padding10px 0px 0px 0px;  
  35.     margin-top-3px;  
  36.     backgroundurl("biggerdot.png"50% 0px no-repeat;  
  37.     cursorpointer;  
  38. }  
  39. .timeline ul li span{  
  40.     displayblock;  
  41.     padding4px 15px;  
  42.     border1px solid transparent;  
  43. }  
  44. .timeline ul li.active span{  
  45.     color#f2f2f2;  
  46.     box-shadow: inset 0px 0px 30px #333333;  
  47.     border-radius: 4px;  
  48.     border1px solid #ffffff;  
  49.     background#666;  
  50. }  


控件代码 jquery.custom.timeline.js

[javascript] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. (function($){  
  2.     $.fn.TimeLine = function(options){  
  3.         var defaults = {  
  4.             data:null,  
  5.             vertical:false  
  6.         };  
  7.   
  8.         var options = $.extend(defaults,options);  
  9.         var _data = options.data;  
  10.         var _vertical = options.vertical;  
  11.         var _showDiv = $(this).addClass("timeline");  
  12.         var _ul = $("<ul />").appendTo(_showDiv);  
  13.         if(_vertical){  
  14.             _ul.addClass("ulvec");  
  15.         }  
  16.         else{  
  17.             _ul.addClass("ulhor");  
  18.         }  
  19.         for(var i= 0,dl=_data.length;i<dl;i++){  
  20.             var _li = $("<li />").appendTo(_ul);  
  21.             if(_vertical){  
  22.                 _li.addClass("livec");  
  23.             }  
  24.             else{  
  25.                 _li.addClass("lihor");  
  26.             }  
  27.             var _span = $("<span />").attr("value",_data[i].value).html(_data[i].label).appendTo(_li);  
  28.             _span.on("click",function(){  
  29.                 var _value = this.getAttribute("value");  
  30.                 active(_value);  
  31.             });  
  32.         }  
  33.         function active(value){  
  34.             $("li").removeClass("active");  
  35.             var _spans = $("ul").find("span");  
  36.             for(var i= 0,dl=_spans.length;i<dl;i++){  
  37.                 var _span = _spans[i];  
  38.                 if(_span.getAttribute("value")===value){  
  39.                     var _li = $(_span.parentNode);  
  40.                     _li.addClass("active");  
  41.                 }  
  42.             }  
  43.         }  
  44.         this.active = active;  
  45.         return this;  
  46.     }  
  47. })(jQuery);  

调用实现

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head lang="en">  
  4.     <meta charset="UTF-8">  
  5.     <title></title>  
  6.     <link rel="stylesheet" href="style.css" type="text/css">  
  7.     <script src="http://localhost/jquery/jquery-1.8.3.js"></script>  
  8.     <script src="jquery.custom.timeline.js"></script>  
  9.     <script>  
  10.         var data = [{"label":"2011年","value":"2011"},  
  11.             {"label":"2012年","value":"2012"},  
  12.             {"label":"2013年","value":"2013"}  
  13.         ];  
  14.         $(function(){  
  15.             var timelinehor = $("#timelinehor").TimeLine({  
  16.                 data:data,  
  17.                 vertical:false  
  18.             });  
  19.             timelinehor.active(data[0].value);  
  20.             var timelinevec = $("#timelinevec").TimeLine({  
  21.                 data:data,  
  22.                 vertical:true  
  23.             });  
  24.             timelinevec.active(data[0].value);  
  25.         });  
  26.     </script>  
  27. </head>  
  28. <body>  
  29. <div id="timelinehor"></div><br><br><br>  
  30. <div id="timelinevec"></div>  
  31. </body>  
  32. </html>  

效果


代码

样式文件style.css

[css] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. .timeline{  
  2.     positionabsolute;  
  3.     z-index5000;  
  4.     font-size12px;  
  5.     border1px solid #ccc;  
  6.     background: whitesmoke;  
  7.     background: -webkit-linear-gradient(top, whitesmoke, #ddd);  
  8.     background: -ms-linear-gradient(top, whitesmoke, #ddd);  
  9.     background: -moz-linear-gradient(top, whitesmoke, #ddd);  
  10.     border-radius: 4px 0 4px 0;  
  11.     box-shadow: 0px 0px 10px rgba(150,150,150,0.5);  
  12. }  
  13. .timeline ul.ulvec{  
  14.     margin-left10px;  
  15.     list-stylenone;  
  16.     backgroundurl("dot.gif"0px 0px repeat-y;  
  17.     padding-right10px;  
  18. }  
  19. .timeline ul li.livec{  
  20.     margin-left-43px;  
  21.     padding0px 0px 0px 12px;  
  22.     backgroundurl("biggerdot.png"0px 50% no-repeat;  
  23.     cursorpointer;  
  24. }  
  25. .timeline ul.ulhor{  
  26.     margin0px;  
  27.     padding5px 10px;  
  28.     list-stylenone;  
  29.     backgroundurl("dot.gif"0px 5px repeat-x;  
  30. }  
  31. .timeline ul li.lihor{  
  32.     display: inline-block;  
  33.     margin0px;  
  34.     padding10px 0px 0px 0px;  
  35.     margin-top-3px;  
  36.     backgroundurl("biggerdot.png"50% 0px no-repeat;  
  37.     cursorpointer;  
  38. }  
  39. .timeline ul li span{  
  40.     displayblock;  
  41.     padding4px 15px;  
  42.     border1px solid transparent;  
  43. }  
  44. .timeline ul li.active span{  
  45.     color#f2f2f2;  
  46.     box-shadow: inset 0px 0px 30px #333333;  
  47.     border-radius: 4px;  
  48.     border1px solid #ffffff;  
  49.     background#666;  
  50. }  


控件代码 jquery.custom.timeline.js

[javascript] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. (function($){  
  2.     $.fn.TimeLine = function(options){  
  3.         var defaults = {  
  4.             data:null,  
  5.             vertical:false  
  6.         };  
  7.   
  8.         var options = $.extend(defaults,options);  
  9.         var _data = options.data;  
  10.         var _vertical = options.vertical;  
  11.         var _showDiv = $(this).addClass("timeline");  
  12.         var _ul = $("<ul />").appendTo(_showDiv);  
  13.         if(_vertical){  
  14.             _ul.addClass("ulvec");  
  15.         }  
  16.         else{  
  17.             _ul.addClass("ulhor");  
  18.         }  
  19.         for(var i= 0,dl=_data.length;i<dl;i++){  
  20.             var _li = $("<li />").appendTo(_ul);  
  21.             if(_vertical){  
  22.                 _li.addClass("livec");  
  23.             }  
  24.             else{  
  25.                 _li.addClass("lihor");  
  26.             }  
  27.             var _span = $("<span />").attr("value",_data[i].value).html(_data[i].label).appendTo(_li);  
  28.             _span.on("click",function(){  
  29.                 var _value = this.getAttribute("value");  
  30.                 active(_value);  
  31.             });  
  32.         }  
  33.         function active(value){  
  34.             $("li").removeClass("active");  
  35.             var _spans = $("ul").find("span");  
  36.             for(var i= 0,dl=_spans.length;i<dl;i++){  
  37.                 var _span = _spans[i];  
  38.                 if(_span.getAttribute("value")===value){  
  39.                     var _li = $(_span.parentNode);  
  40.                     _li.addClass("active");  
  41.                 }  
  42.             }  
  43.         }  
  44.         this.active = active;  
  45.         return this;  
  46.     }  
  47. })(jQuery);  

调用实现

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head lang="en">  
  4.     <meta charset="UTF-8">  
  5.     <title></title>  
  6.     <link rel="stylesheet" href="style.css" type="text/css">  
  7.     <script src="http://localhost/jquery/jquery-1.8.3.js"></script>  
  8.     <script src="jquery.custom.timeline.js"></script>  
  9.     <script>  
  10.         var data = [{"label":"2011年","value":"2011"},  
  11.             {"label":"2012年","value":"2012"},  
  12.             {"label":"2013年","value":"2013"}  
  13.         ];  
  14.         $(function(){  
  15.             var timelinehor = $("#timelinehor").TimeLine({  
  16.                 data:data,  
  17.                 vertical:false  
  18.             });  
  19.             timelinehor.active(data[0].value);  
  20.             var timelinevec = $("#timelinevec").TimeLine({  
  21.                 data:data,  
  22.                 vertical:true  
  23.             });  
  24.             timelinevec.active(data[0].value);  
  25.         });  
  26.     </script>  
  27. </head>  
  28. <body>  
  29. <div id="timelinehor"></div><br><br><br>  
  30. <div id="timelinevec"></div>  
  31. </body>  
  32. </html>  

0 0
原创粉丝点击