读书人

用旋钮控制图片左右滚动

发布时间: 2013-10-23 11:39:13 作者: rapoo

用按钮控制图片左右滚动

用按钮控制图片左右滚动

用按钮控制图片左右滚动,默认不会滚动的

用旋钮控制图片左右滚动XML/HTML Code
  1. <div class="layout">
  2. <div class="hotPic">
  3. <div class="JQ-slide">
  4. <div class="JQ-slide-nav"><a class="prev" href="javascript:void(0);">?</a><a class="next" href="javascript:void(0);">?</a></div>
  5. <div class="wrap">
  6. <ul class="JQ-slide-content imgList">
  7. <li>
  8. <a href="#" class="img"><img src="../erlianhaote.png" width="140" height="100" /></a>
  9. <a href="#" class="txt">二连浩特</a>
  10. </li>
  11. <li>
  12. <a href="#" class="img"><img src="../dalian.jpg" width="140" height="100" /></a>
  13. <a href="#" class="txt">大连</a>
  14. </li>
  15. <li>
  16. <a href="#" class="img"><img src="../sanya.jpg" width="140" height="100" /></a>
  17. <a href="#" class="txt">三亚</a>
  18. </li>
  19. <li>
  20. <a href="#" class="img"><img src="../dandong.png" width="140" height="100" /></a>
  21. <a href="#" class="txt">丹东</a>
  22. </li>
  23. <li>
  24. <a href="#" class="img"><img src="../zhoushan.jpg" width="140" height="100" /></a>
  25. <a href="#" class="txt">中山</a>
  26. </li>
  27. <li>
  28. <a href="http://www.freejs.net" class="img"><img src="../freejs.jpg" width="140" height="100" /></a>
  29. <a href="#" class="txt">freejs</a>
  30. </li>
  31. <li>
  32. <a href="#" class="img"><img src="../mohe.png" width="140" height="100" /></a>
  33. <a href="#" class="txt">漠河</a>
  34. </li>
  35. </ul>
  36. </div>
  37. </div>
  38. </div>
  39. </div>

JavaScript Code
  1. <script type="text/javascript">
  2. $(function (){
  3. /* 用按钮控制图片左右滚动 */
  4. $(".hotPic .JQ-slide").Slide({
  5. effect:"scroolLoop",
  6. autoPlay:false,
  7. speed:"normal",
  8. timer:3000,
  9. steps:1
  10. });
  11. });
  12. </script>

除了jq库以外,还用了slide.js

JavaScript Code
  1. /**
  2. * @author ?
  3. */
  4. (function($){
  5. $.fn.Slide=function(options){
  6. var opts = $.extend({},$.fn.Slide.deflunt,options);
  7. var index=1;
  8. var targetLi = $("." + opts.claNav + " li", $(this));//?
  9. var clickNext = $("." + opts.claNav + " .next", $(this));//??
  10. var clickPrev = $("." + opts.claNav + " .prev", $(this));//??
  11. var ContentBox = $("." + opts.claCon , $(this));//?
  12. var ContentBoxNum=ContentBox.children().size();//??
  13. var slideH=ContentBox.children().first().height();//????????
  14. var slideW=ContentBox.children().first().width();//??????
  15. var autoPlay;
  16. var slideWH;
  17. if(opts.effect=="scroolY"||opts.effect=="scroolTxt"){
  18. slideWH=slideH;
  19. }else if(opts.effect=="scroolX"||opts.effect=="scroolLoop"){
  20. ContentBox.css("width",ContentBoxNum*slideW);
  21. slideWH=slideW;
  22. }else if(opts.effect=="fade"){
  23. ContentBox.children().first().css("z-index","1");
  24. }
  25. return this.each(function() {
  26. var $this=$(this);
  27. //
  28. var doPlay=function(){
  29. $.fn.Slide.effect[opts.effect](ContentBox, targetLi, index, slideWH, opts);
  30. index++;
  31. if (index*opts.steps >= ContentBoxNum) {
  32. index = 0;
  33. }
  34. };
  35. clickNext.click(function(event){
  36. $.fn.Slide.effectLoop.scroolLeft(ContentBox, targetLi, index, slideWH, opts,function(){
  37. for(var i=0;i<opts.steps;i++){
  38. ContentBox.find("li:first",$this).appendTo(ContentBox);
  39. }
  40. ContentBox.css({"left":"0"});
  41. });
  42. event.preventDefault();
  43. });
  44. clickPrev.click(function(event){
  45. for(var i=0;i<opts.steps;i++){
  46. ContentBox.find("li:last").prependTo(ContentBox);
  47. }
  48. ContentBox.css({"left":-index*opts.steps*slideW});
  49. $.fn.Slide.effectLoop.scroolRight(ContentBox, targetLi, index, slideWH, opts);
  50. event.preventDefault();
  51. });
  52. //?
  53. if (opts.autoPlay) {
  54. autoPlay = setInterval(doPlay, opts.timer);
  55. ContentBox.hover(function(){
  56. if(autoPlay){
  57. clearInterval(autoPlay);
  58. }
  59. },function(){
  60. if(autoPlay){
  61. clearInterval(autoPlay);
  62. }
  63. autoPlay=setInterval(doPlay, opts.timer);
  64. });
  65. }
  66. //??
  67. targetLi.hover(function(){
  68. if(autoPlay){
  69. clearInterval(autoPlay);
  70. }
  71. index=targetLi.index(this);
  72. window.setTimeout(function(){$.fn.Slide.effect[opts.effect](ContentBox, targetLi, index, slideWH, opts);},200);
  73. },function(){
  74. if(autoPlay){
  75. clearInterval(autoPlay);
  76. }
  77. autoPlay = setInterval(doPlay, opts.timer);
  78. });
  79. });
  80. };
  81. $.fn.Slide.deflunt={
  82. effect : "scroolY",
  83. autoPlay:true,
  84. speed : "normal",
  85. timer : 1000,
  86. defIndex : 0,
  87. claNav:"JQ-slide-nav",
  88. claCon:"JQ-slide-content",
  89. steps:1
  90. };
  91. $.fn.Slide.effectLoop={
  92. scroolLeft:function(contentObj,navObj,i,slideW,opts,callback){
  93. contentObj.animate({"left":-i*opts.steps*slideW},opts.speed,callback);
  94. if (navObj) {
  95. navObj.eq(i).addClass("on").siblings().removeClass("on");
  96. }
  97. },
  98. scroolRight:function(contentObj,navObj,i,slideW,opts,callback){
  99. contentObj.stop().animate({"left":0},opts.speed,callback);
  100. }
  101. }
  102. $.fn.Slide.effect={
  103. fade:function(contentObj,navObj,i,slideW,opts){
  104. contentObj.children().eq(i).stop().animate({opacity:1},opts.speed).css({"z-index": "1"}).siblings().animate({opacity: 0},opts.speed).css({"z-index":"0"});
  105. navObj.eq(i).addClass("on").siblings().removeClass("on");
  106. },
  107. scroolTxt:function(contentObj,undefined,i,slideH,opts){
  108. //alert(i*opts.steps*slideH);
  109. contentObj.animate({"margin-top":-opts.steps*slideH},opts.speed,function(){
  110. for( var j=0;j<opts.steps;j++){
  111. contentObj.find("li:first").appendTo(contentObj);
  112. }
  113. contentObj.css({"margin-top":"0"});
  114. });
  115. },
  116. scroolX:function(contentObj,navObj,i,slideW,opts,callback){
  117. contentObj.stop().animate({"left":-i*opts.steps*slideW},opts.speed,callback);
  118. if (navObj) {
  119. navObj.eq(i).addClass("on").siblings().removeClass("on");
  120. }
  121. },
  122. scroolY:function(contentObj,navObj,i,slideH,opts){
  123. contentObj.stop().animate({"top":-i*opts.steps*slideH},opts.speed);
  124. if (navObj) {
  125. navObj.eq(i).addClass("on").siblings().removeClass("on");
  126. }
  127. }
  128. };
  129. })(jQuery);


原文地址:http://www.freejs.net/article_jquerytupiantexiao_62.html

读书人网 >Web前端

热点推荐