/source/gl/js/easySlider1.5.js

http://prosporous.googlecode.com/ · JavaScript · 175 lines · 126 code · 20 blank · 29 comment · 19 complexity · cd068fe214b81f9982cbfe32b20dc557 MD5 · raw file

  1. /*
  2. * Easy Slider 1.5 - jQuery plugin
  3. * written by Alen Grakalic
  4. * http://cssglobe.com/post/4004/easy-slider-15-the-easiest-jquery-plugin-for-sliding
  5. *
  6. * Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
  7. * Dual licensed under the MIT (MIT-LICENSE.txt)
  8. * and GPL (GPL-LICENSE.txt) licenses.
  9. *
  10. * Built for jQuery library
  11. * http://jquery.com
  12. *
  13. */
  14. /*
  15. * markup example for $("#slider").easySlider();
  16. *
  17. * <div id="slider">
  18. * <ul>
  19. * <li><img src="images/01.jpg" alt="" /></li>
  20. * <li><img src="images/02.jpg" alt="" /></li>
  21. * <li><img src="images/03.jpg" alt="" /></li>
  22. * <li><img src="images/04.jpg" alt="" /></li>
  23. * <li><img src="images/05.jpg" alt="" /></li>
  24. * </ul>
  25. * </div>
  26. *
  27. */
  28. (function($) {
  29. $.fn.easySlider = function(options){
  30. // default configuration properties
  31. var defaults = {
  32. prevId: 'prevBtn',
  33. prevText: 'Previous',
  34. nextId: 'nextBtn',
  35. nextText: 'Next',
  36. controlsShow: true,
  37. controlsBefore: '',
  38. controlsAfter: '',
  39. controlsFade: true,
  40. firstId: 'firstBtn',
  41. firstText: 'First',
  42. firstShow: false,
  43. lastId: 'lastBtn',
  44. lastText: 'Last',
  45. lastShow: false,
  46. vertical: false,
  47. speed: 800,
  48. auto: false,
  49. pause: 2000,
  50. continuous: false
  51. };
  52. var options = $.extend(defaults, options);
  53. this.each(function() {
  54. var obj = $(this);
  55. var s = $("li", obj).length;
  56. var w = $("li", obj).width();
  57. var h = $("li", obj).height();
  58. obj.width(w);
  59. obj.height(h);
  60. obj.css("overflow","hidden");
  61. var ts = s-1;
  62. var t = 0;
  63. $("ul", obj).css('width',s*w);
  64. if(!options.vertical) $("li", obj).css('float','left');
  65. if(options.controlsShow){
  66. var html = options.controlsBefore;
  67. if(options.firstShow) html += '<span id="'+ options.firstId +'"><a href=\"javascript:void(0);\">'+ options.firstText +'</a></span>';
  68. html += ' <span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span>';
  69. html += ' <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>';
  70. if(options.lastShow) html += ' <span id="'+ options.lastId +'"><a href=\"javascript:void(0);\">'+ options.lastText +'</a></span>';
  71. html += options.controlsAfter;
  72. $(obj).after(html);
  73. };
  74. $("a","#"+options.nextId).click(function(){
  75. animate("next",true);
  76. });
  77. $("a","#"+options.prevId).click(function(){
  78. animate("prev",true);
  79. });
  80. $("a","#"+options.firstId).click(function(){
  81. animate("first",true);
  82. });
  83. $("a","#"+options.lastId).click(function(){
  84. animate("last",true);
  85. });
  86. function animate(dir,clicked){
  87. var ot = t;
  88. switch(dir){
  89. case "next":
  90. t = (ot>=ts) ? (options.continuous ? 0 : ts) : t+1;
  91. break;
  92. case "prev":
  93. t = (t<=0) ? (options.continuous ? ts : 0) : t-1;
  94. break;
  95. case "first":
  96. t = 0;
  97. break;
  98. case "last":
  99. t = ts;
  100. break;
  101. default:
  102. break;
  103. };
  104. var diff = Math.abs(ot-t);
  105. var speed = diff*options.speed;
  106. if(!options.vertical) {
  107. p = (t*w*-1);
  108. $("ul",obj).animate(
  109. { marginLeft: p },
  110. speed
  111. );
  112. } else {
  113. p = (t*h*-1);
  114. $("ul",obj).animate(
  115. { marginTop: p },
  116. speed
  117. );
  118. };
  119. if(!options.continuous && options.controlsFade){
  120. if(t==ts){
  121. $("a","#"+options.nextId).hide();
  122. $("a","#"+options.lastId).hide();
  123. } else {
  124. $("a","#"+options.nextId).show();
  125. $("a","#"+options.lastId).show();
  126. };
  127. if(t==0){
  128. $("a","#"+options.prevId).hide();
  129. $("a","#"+options.firstId).hide();
  130. } else {
  131. $("a","#"+options.prevId).show();
  132. $("a","#"+options.firstId).show();
  133. };
  134. };
  135. if(clicked) clearTimeout(timeout);
  136. if(options.auto && dir=="next" && !clicked){;
  137. timeout = setTimeout(function(){
  138. animate("next",false);
  139. },diff*options.speed+options.pause);
  140. };
  141. };
  142. // init
  143. var timeout;
  144. if(options.auto){;
  145. timeout = setTimeout(function(){
  146. animate("next",false);
  147. },options.pause);
  148. };
  149. if(!options.continuous && options.controlsFade){
  150. $("a","#"+options.prevId).hide();
  151. $("a","#"+options.firstId).hide();
  152. };
  153. });
  154. };
  155. })(jQuery);