首页 > 语言 > JavaScript > 正文

jQuery插件boxScroll实现图片轮播特效

2024-05-06 16:23:05
字体:大 中 小
来源:转载
供稿:网友

本文给大家分享的是使用jQuery插件Boxscroll来实现简单的图片轮播特效的代码,非常简单实用,有需要的小伙伴可以参考下。

BoxScroll

常见图片轮播效果的简单实现。可以数字列表控制或者左右按键控制。逻辑很简单,到了尽头得往回跑,看看注释就知道了。

代码如下:

HTML

 

 
  1. <!doctype html> 
  2. <html> 
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  4. <meta content="" name="keywords" /> 
  5. <meta content="" name="description" /> 
  6. <meta name="author" content="codetker" /> 
  7. <head> 
  8. <title>简易图片轮播插件</title> 
  9. <link href="style/reset.css" rel="stylesheet" type="text/css"> 
  10. <link href="style/style.css" rel="stylesheet" type="text/css"> 
  11. <script type="text/javascript" src="js/jquery-1.9.1.min.js"></script> 
  12. <script type="text/javascript" src="js/jquery.codetker.boxScroll.js"></script> 
  13. </head> 
  14.  
  15. <body> 
  16. <div class="wrap"> 
  17. <div class="scrollBox"> 
  18. <div class="picOuterBox boxStyle"> 
  19. <div class="arrow arrowLeft">ToLeft</div> 
  20. <div class="arrow arrowRight">ToRight</div> 
  21. <ul class="picInnerBox boxStyle"> 
  22. <li> 
  23. <a href="" title=""> 
  24. <img src="images/test.jpg" alt=""> 
  25. </a> 
  26. </li> 
  27. <li> 
  28. <a href="" title=""> 
  29. <img src="images/test.jpg" alt=""> 
  30. </a> 
  31. </li> 
  32. <li> 
  33. <a href="" title=""> 
  34. <img src="images/test.jpg" alt=""> 
  35. </a> 
  36. </li> 
  37. <li> 
  38. <a href="" title=""> 
  39. <img src="images/test.jpg" alt=""> 
  40. </a> 
  41. </li> 
  42. <li> 
  43. <a href="" title=""> 
  44. <img src="images/test.jpg" alt=""> 
  45. </a> 
  46. </li> 
  47. </ul> 
  48. </div> 
  49. <div class="picControl"> 
  50. <ul> 
  51. <li class="liSelected">1</li> 
  52. <li>2</li> 
  53. <li>3</li> 
  54. <li>4</li> 
  55. <li>5</li> 
  56. </ul> 
  57. </div> 
  58. </div> 
  59. </div> 
  60. <script type="text/javascript"> 
  61. $(document).ready(function(){ 
  62. $(".scrollBox").boxScroll(); 
  63. }); 
  64. </script> 
  65. </body> 
  66. </html> 

CSS

 

 
  1. @charset "utf-8"; 
  2. /* CSS Document */ 
  3. body{ 
  4. margin:0 0; 
  5. padding:0 0; 
  6. height:100%; 
  7. width:100%; 
  8. } 
  9. .wrap{ 
  10. font-family:"微软雅黑","宋体", Times, "Times New Roman", serif; 
  11. font-size:14px; 
  12. margin:0 0; 
  13. padding:0 0; 
  14. height:100%; 
  15. width:100%; 
  16. overflow:hidden; 
  17. } 
  18. .boxStyle{/*照片大小*/ 
  19. width: 500px; 
  20. height: 256px; 
  21. } 
  22. .scrollBox{ 
  23. position: relative; 
  24. width: 500px; 
  25. margin: 0 auto; 
  26. } 
  27. .picInnerBox{ 
  28. width: 10000px;/*足够大能放下即可,如果需要上下滚动,改height*/ 
  29. overflow: hidden; 
  30. } 
  31. .picInnerBox li{ 
  32. cursor: pointer; 
  33. float: left; 
  34. } 
  35. .picOuterBox{ 
  36. overflow: hidden; 
  37.  
  38. } 
  39. .arrow{ 
  40. position: absolute; 
  41. top: 45%; 
  42. height: 40px; 
  43. cursor: pointer; 
  44. z-index: 99;  
  45. } 
  46. .arrow:hover{ 
  47. color: #fff; 
  48. } 
  49. .arrowLeft{ 
  50. float: left; 
  51. left: 5%; 
  52. } 
  53. .arrowRight{ 
  54. float: right; 
  55. right: 5%; 
  56. } 
  57. .picControl{ 
  58. overflow: auto; 
  59. width: 100px; 
  60. margin: 0 auto; 
  61. } 
  62. .picControl ul li{ 
  63. cursor: pointer; 
  64. float: left; 
  65. width: 20px; 
  66. height: 20px; 
  67. text-align: center; 
  68. } 
  69. .picControl ul li:hover{ 
  70. color:red; 
  71. } 
  72. .liSelected{ 
  73. color: red; 
  74. } 

JavaScript

 

 
  1. /* 
  2. * boxScroll 0.1 
  3. * 兼容等常见浏览器 
  4. */ 
  5. ;(function($,window,document,undefined){ 
  6. //定义构造函数 
  7. var BoxObj=function(ele,opt){ 
  8. this.$element=ele; //最外层对象 
  9. this.defaults={ 
  10. 'style': 0 ,//滚动样式选择,默认为普通效果 
  11. 'speed': 1 ,//默认为1s 
  12. 'direction': 'left',//默认为向左边滚动 
  13. 'toLeft':$(ele).children('.picOuterBox').children('.arrowLeft'),//默认格式下重要位置 
  14. 'toRight':$(ele).children('.picOuterBox').children('.arrowRight'), 
  15. 'ControlUl':$(ele).children('.picControl').children('ul') 
  16. }, 
  17.  
  18. this.options=$.extend({},this.defaults,opt ); 
  19. //这里可以添加一些通用方法  
  20. } 
  21.  
  22. //给构造函数添加方法 
  23. BoxObj.prototype={ 
  24.  
  25. commonScroll:function(){ 
  26. //接收对象属性 
  27. var boxWindow=$(this.$element).children('.picOuterBox').children('.picInnerBox'); 
  28. var speed=this.defaults.speed; 
  29. var style=this.defaults.style; 
  30. var direction=(this.defaults.direction=='left')? 1 : -1; 
  31. var toLeft=this.defaults.toLeft; 
  32. var toRight=this.defaults.toRight; 
  33. var Control=this.defaults.ControlUl; 
  34.  
  35. var boxWidth=$(boxWindow).children('li').width(); 
  36. var imgIndexMax=$(boxWindow).children('li').length; 
  37. var imgIndex; 
  38. function getImgIndex(){//判断当前图片的位置 
  39. imgIndex=Math.round(parseInt($(boxWindow).css("margin-left"))*(-1)/boxWidth); 
  40. } 
  41.  
  42. var timer;//必须在外面定义保证全局针对这一功能只有这一个计时器 
  43. timer=setInterval(function(){ 
  44. boxScroll(imgIndex,direction); 
  45. },5000); 
  46.  
  47. function rest(){ 
  48. clearInterval(timer); 
  49. timer=setInterval(function(){ 
  50. boxScroll(imgIndex,direction); 
  51. },5000); 
  52. } 
  53.  
  54. //绑定点击按钮 
  55. $(Control).delegate('li', 'click', function() { 
  56. boxScroll($(this).index(),0); 
  57. rest(); 
  58. }); 
  59.  
  60. //绑定左右按钮 
  61. $(toLeft).click(function() { 
  62. boxScroll(0,-1); 
  63. rest(); 
  64. }); 
  65. $(toRight).click(function() { 
  66. boxScroll(0,1); 
  67. rest(); 
  68. }); 
  69.  
  70. function boxScroll(index,dir){ 
  71. if (!$(boxWindow).is(':animated')) {//当ul窗口没有在动时 
  72. if(!dir){//响应ul li control操作 
  73. //此时dir=0,则依靠传入的imgIndex 
  74. imgIndex=index; 
  75. //其它时候dir!=0,则依靠dir 
  76. }else{//响应toLeft和toRight 
  77. if(dir==1){//向右动 
  78. getImgIndex(); 
  79. if (imgIndex==(imgIndexMax-1)) { 
  80. imgIndex=0; 
  81.  
  82. }else{ 
  83. imgIndex+=1; 
  84. } 
  85. }else{//向左动 
  86. getImgIndex(); 
  87. if (imgIndex==0) { 
  88. imgIndex=(imgIndexMax-1); 
  89. }else{ 
  90. imgIndex-=1; 
  91. } 
  92. } 
  93. } 
  94. $(Control).children('li').eq(imgIndex).addClass('liSelected'); 
  95. $(Control).children('li').eq(imgIndex).siblings().removeClass('liSelected'); 
  96. $(boxWindow).animate({ 
  97. "margin-left":imgIndex*boxWidth*(-1)+'px' 
  98. }, 1000*speed); 
  99. } 
  100. } 
  101. } 
  102. } 
  103.  
  104. //在插件中使用windowObj对象的方法,0为vertical,1为horizontal 
  105. $.fn.boxScroll=function(options){ 
  106. //创建实体 
  107. var boxObj=new BoxObj(this,options); 
  108. //用尾调的形式调用对象方法 
  109. return boxObj.commonScroll(); 
  110. } 
  111. })(jQuery,window,document); 

详细下载参见https://github.com/codetker/myBoxScroll。

以上所述就是本文的全部内容了,希望大家能够喜欢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选