基于jquery的循环左右滚动和上下滚动效果

来源:互联网 发布:mac 打开系统文件夹 编辑:程序博客网 时间:2024/05/16 01:53
[css] view plaincopy
  1. @charset "utf-8";  
  2. /* CSS Document */  
  3. * {  
  4.     margin:0;  
  5.     padding:0;  
  6. }  
  7. body {  
  8.     font-size:12px;  
  9. }  
  10. /*左右滚动*/  
  11. #scrollable {  
  12.     background-color:#efefef;  
  13.     border:1px solid #ddd;  
  14.     padding:10px 8px;  
  15.     width:523px;  
  16.     height:65px;  
  17.     margin-top:30px;  
  18. }  
  19. div.items {  
  20.     height:66px;  
  21.     margin-left:8px;  
  22.     float:left;  
  23.     width:475px !important;  
  24. }  
  25. div.items a {  
  26.     display:block;  
  27.     float:left;  
  28.     margin-right:8px;  
  29.     width:88px;  
  30.     height:66px;  
  31.     background:#BBB;  
  32.     font-size:50px;  
  33.     color:#ccc;  
  34.     line-height:66px;  
  35.     text-decoration:none;  
  36.     text-align:center;  
  37.     cursor:pointer;  
  38. }  
  39. div.items a:hover {  
  40.     color:#999;  
  41. }  
  42. div.items a.active {  
  43.     background-position:-174px 0;  
  44.     color:#555;  
  45.     cursor:default;  
  46. }  
  47. a.prev, a.next {  
  48.     background:url(left.png) no-repeat 0 0;  
  49.     display:block;  
  50.     width:18px;  
  51.     height:18px;  
  52.     float:left;  
  53.     margin:22px 0 0 0;  
  54.     cursor:pointer;  
  55. }  
  56. a.next {  
  57.     background-image:url(right.png)  
  58. }  
  59. a.prev:hover {  
  60.     background-position:0 -18px;  
  61. }  
  62. a.next:hover {  
  63.     background-position:0 -18px;  
  64. }  
  65. /*上下滚动*/  
  66. #scrollDiv {  
  67.     width300px;  
  68.     height25px;  
  69.     line-height25px;  
  70.     border#ccc 1px solid;  
  71.     overflowhidden;  
  72. }  
  73. #scrollDiv li {  
  74.     height25px;  
  75.     padding-left10px;  
  76. }  


js代码


[javascript] view plaincopy
  1. // JavaScript Document  
  2.   
  3. // 左右滚动  
  4. (function($) {  
  5.     $.fn.extend({  
  6.         Scroll: function(opt, callback) {  
  7.             if (!opt) var opt = {};  
  8.             var _btnleft = $(opt.left);  
  9.             var _btnright = $(opt.right);  
  10.             var timerID;  
  11.             var _this = this.eq(0).find("div").eq(1);  
  12.             var lineW = _this.find("a:first").width(),  
  13.             //获取列宽   
  14.             line = opt.line ? parseInt(opt.line, 10) : parseInt(_this.width() / lineW, 10),  
  15.             //每次滚动的列数,默认为一屏,即父容器列宽   
  16.             speed = opt.speed ? parseInt(opt.speed, 10) : 500; //滚动速度,数值越大,速度越慢(毫秒)   
  17.             timer = opt.timer ? parseInt(opt.timer, 10) : 3000; //滚动的时间间隔(毫秒)   
  18.             if (line == 0) line = 1;  
  19.             var upWidth = 0 - line * lineW;  
  20.             //滚动函数   
  21.             var scrollLeft = function() {  
  22.                 if (!_this.is(":animated")) {  
  23.                     _this.animate({  
  24.                         left: upWidth  
  25.                     },  
  26.                     speed,  
  27.                     function() {  
  28.                         for (i = 1; i <= line; i++) {  
  29.                             _this.find("a:first").appendTo(_this);  
  30.                         }  
  31.                         _this.css({  
  32.                             left: 0  
  33.                         });  
  34.                     });  
  35.                 }  
  36.             }  
  37.             var scrollRight = function() {  
  38.                 if (!_this.is(":animated")) {  
  39.                     for (i = 1; i <= line; i++) {  
  40.                         _this.find("a:last").show().prependTo(_this);  
  41.                     }  
  42.                     _this.css({  
  43.                         left: upWidth  
  44.                     });  
  45.                     _this.animate({  
  46.                         left: 0  
  47.                     },  
  48.                     speed,  
  49.                     function() {});  
  50.                 }  
  51.             } //Shawphy:自动播放   
  52.             var autoPlay = function() {  
  53.                 if (timer) timerID = window.setInterval(scrollLeft, timer);  
  54.             };  
  55.             var autoStop = function() {  
  56.                 if (timer) window.clearInterval(timerID);  
  57.             };             //鼠标事件绑定   
  58.             _this.hover(autoStop, autoPlay).mouseout();  
  59.             _btnleft.css("cursor""pointer").click(scrollLeft).hover(autoStop, autoPlay);  
  60.             _btnright.css("cursor""pointer").click(scrollRight).hover(autoStop, autoPlay);  
  61.         }  
  62.     })  
  63. })(jQuery);  
  64. $(document).ready(function() {  
  65.     $("#scrollable").Scroll({  
  66.         line: 5,  
  67.         speed: 500,  
  68.         timer: 3000,  
  69.         left: ".prev",  
  70.         right: ".next"  
  71.     });  
  72. });  
  73.   
  74. // 上下滚动  
  75. function AutoScroll(obj) {  
  76.     $(obj).find("ul:first").animate({  
  77.         marginTop: "-25px"  
  78.     },  
  79.     500,  
  80.     function() {  
  81.         $(this).css({  
  82.             marginTop: "0px"  
  83.         }).find("li:first").appendTo(this);  
  84.     });  
  85. }  
  86. $(document).ready(function() {  
  87.     var myar = setInterval('AutoScroll("#scrollDiv")', 1000);  
  88.     $("#scrollDiv").hover(function() {  
  89.         clearInterval(myar);  
  90.     },  
  91.     function() {  
  92.         myar = setInterval('AutoScroll("#scrollDiv")', 1000)  
  93.     }); //当鼠标放上去的时候,滚动停止,鼠标离开的时候滚动开始  
  94. });  

html代码

[html] view plaincopy
  1. <!-- 左右滚动 -->  
  2. <div style="margin:0 auto;width:500px;">  
  3.   <div id="scrollable"> <a class="prev" href="#"></a>  
  4.     <div class="items" style="overflow: hidden; position: relative; visibility: visible; width: 478px;">  
  5.       <div style="position: absolute; width: 200000em; left: 0px;" class="scrollable_demo"> <a>1</a> <a>2</a> <a>3</a> <a>4</a> <a>5</a> <a>6</a> <a>7</a> <a>8</a> <a>9</a> <a>10</a> <a>11</a> <a>12</a> <a>13</a> <a>14</a> <a>15</a> </div>  
  6.       <br clear="all"/>  
  7.     </div>  
  8.     <a class="next" href="#"></a> </div>  
  9. </div>  
  10.   
  11. <!-- 上下滚动 -->  
  12. <div id="scrollDiv">  
  13. <ul>  
  14.   <li><a href="http://www.sina.com">欢迎浏览新浪网</a></li>  
  15.   <li><a href="http://www.163.com">欢迎浏览网易</a></li>  
  16.   <li><a href="http://www.csdn.com">欢迎进入程序员之家</a></li>  
  17.   <li><a href="http://www.51aspx.com">很好的源代码下载区</a></li>  
  18.   <li><a href="http://www.msdn.com">msdn</a></li>  
  19.   <li><a href="http://www.baidu.com">欢迎浏览百度网</a></li>  
  20. </ul>  
  21. </div  


阅读原文
0 0
原创粉丝点击