Monday, October 22, 2012

基于jQuery的图片展示(横向滚动,可左右控制)

http://blog.csdn.net/fengxuezhiye/article/details/7336678

基于jQuery的图片展示(横向滚动,可左右控制)

分类: WEB前端1807人阅读评论(0)收藏举报
今天在网上看了一个图片展示的实例,就整理一下贴了出来,为的是以后使用方便,也可以和大家一起分享。
下面是整个页面,如果要使用,只需把整个页面拷贝过去,然后修改图片路径,把jQuery的包也要放进去。
展示效果:

页面代码:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  5. <title>jQuery 横向图片滚动,带左右控制</title>
  6. <style type="text/css">
  7. <!--
  8. *{margin:0; padding:0}
  9. li{list-style:none}
  10. img{border:0}
  11. body{background-color:#fff; font-size:12px; color:#2c2c2c; font-family:Arial, Helvetica, sans-serif}
  12. a{color:#444; text-decoration:none}
  13. a:hover{color:#900; text-decoration:underline}
  14. .con{overflow:hidden; margin:30px auto; width:442px; height:100%; padding:10px 15px; border:solid 1px #FCBA79; background-color:#FFFBEC;}
  15. #carousel_container{position:relative; height:100px; overflow:hidden;}
  16. #carousel_inner{width:391px; height:95px; overflow: hidden; position:absolute;left:25px; top:5px;}
  17. #left_scroll{position: absolute;left:0;top:43px;width:9px;height:9px;cursor: pointer;cursor: hand; background:url(images/left.gif) no-repeat;}
  18. #right_scroll{position: absolute;top:43px;right:0; width: 9px;height: 9px;cursor: pointer;cursor: hand; background: url(images/right.gif) no-repeat;}
  19. #carousel_ul{width:9999px; height:95px; position:relative;}
  20. #carousel_ul li{float: left;width:84px; height:86px; border:solid 1px #FEDDAB; margin-right:15px; display:inline;}
  21. -->
  22. </style>
  23. <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
  24. <script type="text/javascript">
  25. <!--
  26. var SellerScroll = function(options){
  27. this.SetOptions(options);
  28. this.lButton = this.options.lButton;
  29. this.rButton = this.options.rButton;
  30. this.oList = this.options.oList;
  31. this.showSum = this.options.showSum;
  32. this.iList = $("#" + this.options.oList + " > li");
  33. this.iListSum = this.iList.length;
  34. this.iListWidth = this.iList.outerWidth(true);
  35. this.moveWidth = this.iListWidth * this.showSum;
  36. this.dividers = Math.ceil(this.iListSum / this.showSum); //共分为多少块
  37. this.moveMaxOffset = (this.dividers - 1) * this.moveWidth;
  38. this.LeftScroll();
  39. this.RightScroll();
  40. };
  41. SellerScroll.prototype = {
  42. SetOptions: function(options){
  43. this.options = {
  44. lButton: "left_scroll",
  45. rButton: "right_scroll",
  46. oList: "carousel_ul",
  47. showSum: 4 //一次滚动多少个items
  48. };
  49. $.extend(this.options, options || {});
  50. },
  51. ReturnLeft: function(){
  52. return isNaN(parseInt($("#" + this.oList).css("left"))) ? 0 : parseInt($("#" + this.oList).css("left"));
  53. },
  54. LeftScroll: function(){
  55. if(this.dividers == 1) return;
  56. var _this = this, currentOffset;
  57. $("#" + this.lButton).click(function(){
  58. currentOffset = _this.ReturnLeft();
  59. if(currentOffset == 0){
  60. for(var i = 1; i <= _this.showSum; i++){
  61. $(_this.iList[_this.iListSum - i]).prependTo($("#" + _this.oList));
  62. }
  63. $("#" + _this.oList).css({ left: -_this.moveWidth });
  64. $("#" + _this.oList + ":not(:animated)").animate( { left: "+=" + _this.moveWidth }, { duration: "slow", complete: function(){
  65. for(var j = _this.showSum + 1; j <= _this.iListSum; j++){
  66. $(_this.iList[_this.iListSum - j]).prependTo($("#" + _this.oList));
  67. }
  68. $("#" + _this.oList).css({ left: -_this.moveWidth * (_this.dividers - 1) });
  69. } } );
  70. }else{
  71. $("#" + _this.oList + ":not(:animated)").animate( { left: "+=" + _this.moveWidth }, "slow" );
  72. }
  73. });
  74. },
  75. RightScroll: function(){
  76. if(this.dividers == 1) return;
  77. var _this = this, currentOffset;
  78. $("#" + this.rButton).click(function(){
  79. currentOffset = _this.ReturnLeft();
  80. if(Math.abs(currentOffset) >= _this.moveMaxOffset){
  81. for(var i = 0; i < _this.showSum; i++){
  82. $(_this.iList[i]).appendTo($("#" + _this.oList));
  83. }
  84. $("#" + _this.oList).css({ left: -_this.moveWidth * (_this.dividers - 2) });
  85. $("#" + _this.oList + ":not(:animated)").animate( { left: "-=" + _this.moveWidth }, { duration: "slow", complete: function(){
  86. for(var j = _this.showSum; j < _this.iListSum; j++){
  87. $(_this.iList[j]).appendTo($("#" + _this.oList));
  88. }
  89. $("#" + _this.oList).css({ left: 0 });
  90. } } );
  91. }else{
  92. $("#" + _this.oList + ":not(:animated)").animate( { left: "-=" + _this.moveWidth }, "slow" );
  93. }
  94. });
  95. }
  96. };
  97. $(document).ready(function(){
  98. var ff = new SellerScroll();
  99. });
  100. //-->
  101. </script>
  102. </head>
  103. <body>
  104. <div class="con">
  105. <div id="carousel_container">
  106. <div id="left_scroll"></div>
  107. <div id="carousel_inner">
  108. <ul id="carousel_ul">
  109. <li><img src="images/wall_s1.jpg" border="0"></li>
  110. <li><img src="images/wall_s2.jpg" border="0"></li>
  111. <li><img src="images/wall_s3.jpg" border="0"></li>
  112. <li><img src="images/wall_s4.jpg" border="0"></li>
  113. <li><img src="images/wall_s5.jpg" border="0"></li>
  114. <li><img src="images/wall_s6.jpg" border="0"></li>
  115. <li><img src="images/wall_s7.jpg" border="0"></li>
  116. <li><img src="images/wall_s1.jpg" border="0"></li>
  117. <li><img src="images/wall_s2.jpg" border="0"></li>
  118. <li><img src="images/wall_s3.jpg" border="0"></li>
  119. <li><img src="images/wall_s4.jpg" border="0"></li>
  120. <li><img src="images/wall_s5.jpg" border="0"></li>
  121. <li><img src="images/wall_s1.jpg" border="0"></li>
  122. <li><img src="images/wall_s2.jpg" border="0"></li>
  123. <li><img src="images/wall_s3.jpg" border="0"></li>
  124. </ul>
  125. </div>
  126. <div id="right_scroll"></div>
  127. </div>
  128. </div>
  129. </body>
  130. </html>

No comments:

Post a Comment