jquery.wmuslider.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*!
  2. * jQuery wmuSlider v2.1
  3. *
  4. * Copyright (c) 2011 Brice Lechatellier
  5. * http://brice.lechatellier.com/
  6. *
  7. * Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
  8. */
  9. ;(function($) {
  10. $.fn.wmuSlider = function(options) {
  11. /* Default Options
  12. ================================================== */
  13. var defaults = {
  14. animation: 'fade',
  15. animationDuration: 600,
  16. slideshow: true,
  17. slideshowSpeed: 7000,
  18. slideToStart: 0,
  19. navigationControl: true,
  20. paginationControl: true,
  21. previousText: 'Previous',
  22. nextText: 'Next',
  23. touch: false,
  24. slide: 'article',
  25. items: 1
  26. };
  27. var options = $.extend(defaults, options);
  28. return this.each(function() {
  29. /* Variables
  30. ================================================== */
  31. var $this = $(this);
  32. var currentIndex = options.slideToStart;
  33. var wrapper = $this.find('.wmuSliderWrapper');
  34. var slides = $this.find(options.slide);
  35. var slidesCount = slides.length;
  36. var slideshowTimeout;
  37. var paginationControl;
  38. var isAnimating;
  39. /* Load Slide
  40. ================================================== */
  41. var loadSlide = function(index, infinite, touch) {
  42. if (isAnimating) {
  43. return false;
  44. }
  45. isAnimating = true;
  46. currentIndex = index;
  47. var slide = $(slides[index]);
  48. $this.animate({ height: slide.innerHeight() });
  49. if (options.animation == 'fade') {
  50. slides.css({
  51. position: 'absolute',
  52. opacity: 0
  53. });
  54. slide.css({'position' : 'relative', 'z-index' : '2'});
  55. slide.siblings(options.slide).css('z-index' , '1');
  56. slide.animate({ opacity:1 }, options.animationDuration, function() {
  57. isAnimating = false;
  58. });
  59. } else if (options.animation == 'slide') {
  60. if (!infinite) {
  61. wrapper.animate({ marginLeft: -$this.width() / options.items * index }, options.animationDuration, function() {
  62. isAnimating = false;
  63. });
  64. } else {
  65. if (index == 0) {
  66. wrapper.animate({ marginLeft: -$this.width() / options.items * slidesCount }, options.animationDuration, function() {
  67. wrapper.css('marginLeft', 0);
  68. isAnimating = false;
  69. });
  70. } else {
  71. if (!touch) {
  72. wrapper.css('marginLeft', -$this.width() / options.items * slidesCount);
  73. }
  74. wrapper.animate({ marginLeft: -$this.width() / options.items * index }, options.animationDuration, function() {
  75. isAnimating = false;
  76. });
  77. }
  78. }
  79. }
  80. if (paginationControl) {
  81. paginationControl.find('a').each(function(i) {
  82. if(i == index) {
  83. $(this).addClass('wmuActive');
  84. } else {
  85. $(this).removeClass('wmuActive');
  86. }
  87. });
  88. }
  89. // Trigger Event
  90. $this.trigger('slideLoaded', index);
  91. };
  92. /* Navigation Control
  93. ================================================== */
  94. if (options.navigationControl) {
  95. var prev = $('<a class="wmuSliderPrev">' + options.previousText + '</a>');
  96. prev.click(function(e) {
  97. e.preventDefault();
  98. clearTimeout(slideshowTimeout);
  99. if (currentIndex == 0) {
  100. loadSlide(slidesCount - 1, true);
  101. } else {
  102. loadSlide(currentIndex - 1);
  103. }
  104. });
  105. $this.append(prev);
  106. var next = $('<a class="wmuSliderNext">' + options.nextText + '</a>');
  107. next.click(function(e) {
  108. e.preventDefault();
  109. clearTimeout(slideshowTimeout);
  110. if (currentIndex + 1 == slidesCount) {
  111. loadSlide(0, true);
  112. } else {
  113. loadSlide(currentIndex + 1);
  114. }
  115. });
  116. $this.append(next);
  117. }
  118. /* Pagination Control
  119. ================================================== */
  120. if (options.paginationControl) {
  121. paginationControl = $('<ul class="wmuSliderPagination"></ul>');
  122. $.each(slides, function(i) {
  123. paginationControl.append('<li><a href="#">' + i + '</a></li>');
  124. paginationControl.find('a:eq(' + i + ')').click(function(e) {
  125. e.preventDefault();
  126. clearTimeout(slideshowTimeout);
  127. loadSlide(i);
  128. });
  129. });
  130. $this.append(paginationControl);
  131. }
  132. /* Slideshow
  133. ================================================== */
  134. if (options.slideshow) {
  135. var slideshow = function() {
  136. if (currentIndex + 1 < slidesCount) {
  137. loadSlide(currentIndex + 1);
  138. } else {
  139. loadSlide(0, true);
  140. }
  141. slideshowTimeout = setTimeout(slideshow, options.slideshowSpeed);
  142. }
  143. slideshowTimeout = setTimeout(slideshow, options.slideshowSpeed);
  144. }
  145. /* Resize Slider
  146. ================================================== */
  147. var resize = function() {
  148. var slide = $(slides[currentIndex]);
  149. $this.animate({ height: slide.innerHeight() });
  150. if (options.animation == 'slide') {
  151. slides.css({
  152. width: $this.width() / options.items
  153. });
  154. wrapper.css({
  155. marginLeft: -$this.width() / options.items * currentIndex,
  156. width: $this.width() * slides.length
  157. });
  158. }
  159. };
  160. /* Touch
  161. ================================================== */
  162. var touchSwipe = function(event, phase, direction, distance) {
  163. clearTimeout(slideshowTimeout);
  164. if(phase == 'move' && (direction == 'left' || direction == 'right')) {
  165. if (direction == 'right') {
  166. if (currentIndex == 0) {
  167. wrapper.css('marginLeft', (-slidesCount * $this.width() / options.items) + distance);
  168. } else {
  169. wrapper.css('marginLeft', (-currentIndex * $this.width() / options.items) + distance);
  170. }
  171. } else if (direction == 'left') {
  172. wrapper.css('marginLeft', (-currentIndex * $this.width() / options.items) - distance);
  173. }
  174. } else if (phase == 'cancel' ) {
  175. if (direction == 'right' && currentIndex == 0) {
  176. wrapper.animate({ marginLeft: -slidesCount * $this.width() / options.items }, options.animationDuration);
  177. } else {
  178. wrapper.animate({ marginLeft: -currentIndex * $this.width() / options.items }, options.animationDuration);
  179. }
  180. } else if (phase == 'end' ) {
  181. if (direction == 'right') {
  182. if (currentIndex == 0) {
  183. loadSlide(slidesCount - 1, true, true);
  184. } else {
  185. loadSlide(currentIndex - 1);
  186. }
  187. } else if (direction == 'left') {
  188. if (currentIndex + 1 == slidesCount) {
  189. loadSlide(0, true);
  190. } else {
  191. loadSlide(currentIndex + 1);
  192. }
  193. } else {
  194. wrapper.animate({ marginLeft: -currentIndex * $this.width() / options.items }, options.animationDuration);
  195. }
  196. }
  197. };
  198. if (options.touch && options.animation == 'slide') {
  199. if (!$.isFunction($.fn.swipe)) {
  200. $.ajax({
  201. url: 'jquery.touchSwipe.min.js',
  202. async: false
  203. });
  204. }
  205. if ($.isFunction($.fn.swipe)) {
  206. $this.swipe({ triggerOnTouchEnd:false, swipeStatus:touchSwipe, allowPageScroll:'vertical' });
  207. }
  208. }
  209. /* Init Slider
  210. ================================================== */
  211. var init = function() {
  212. var slide = $(slides[currentIndex]);
  213. var img = slide.find('img');
  214. img.load(function() {
  215. wrapper.show();
  216. $this.animate({ height: slide.innerHeight() });
  217. });
  218. if (options.animation == 'fade') {
  219. slides.css({
  220. position: 'absolute',
  221. width: '100%',
  222. opacity: 0
  223. });
  224. $(slides[currentIndex]).css('position', 'relative');
  225. } else if (options.animation == 'slide') {
  226. if (options.items > slidesCount) {
  227. options.items = slidesCount;
  228. }
  229. slides.css('float', 'left');
  230. slides.each(function(i){
  231. var slide = $(this);
  232. slide.attr('data-index', i);
  233. });
  234. for(var i = 0; i < options.items; i++) {
  235. wrapper.append($(slides[i]).clone());
  236. }
  237. slides = $this.find(options.slide);
  238. }
  239. resize();
  240. $this.trigger('hasLoaded');
  241. loadSlide(currentIndex);
  242. }
  243. init();
  244. /* Bind Events
  245. ================================================== */
  246. // Resize
  247. $(window).resize(resize);
  248. // Load Slide
  249. $this.bind('loadSlide', function(e, i) {
  250. clearTimeout(slideshowTimeout);
  251. loadSlide(i);
  252. });
  253. });
  254. }
  255. })(jQuery);