box-slider.jquery.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. ; (function (w, $, undefined) {
  2. w.jqBoxSlider = w.jqBoxSlider || {};
  3. var methods = {} // external method api
  4. , supports3D = true // set during vendorPrefix determination
  5. , slideAnimators = {} // map of animation effect objects
  6. , defaults = { // default required settings
  7. speed: 800
  8. , responsive: true
  9. , timeout: 5000
  10. , autoScroll: false
  11. , pauseOnHover: false
  12. , effect: 'scrollVert3d'
  13. , perspective: 1000
  14. };
  15. // API methods ---------------------------------------------------------------
  16. // sets up all selected boxes with applied options
  17. methods.init = function (opts) {
  18. var defaultSettings = $.extend({}, defaults, opts)
  19. , animator = methods.slideAnimator(defaultSettings.effect);
  20. return this.each(function () {
  21. var $this = $(this)
  22. , settings = $.extend({}, defaultSettings)
  23. , $slides = $this.children();
  24. $this.data('bssettings', settings);
  25. settings.slideAnimator = animator;
  26. settings.slideAnimator.initialize($this, $slides, settings);
  27. $slides.eq(settings.bsfaceindex || 0).addClass('jbs-current');
  28. setupControls($this, settings);
  29. if (settings.autoScroll) {
  30. settings.autointv = setInterval(function () {
  31. showNextSlide($this);
  32. }, settings.timeout + settings.speed);
  33. if (settings.pauseOnHover) {
  34. $this.on('hover', togglePlayPause);
  35. }
  36. }
  37. if (settings.responsive && jqBoxSlider.responder) {
  38. jqBoxSlider.responder.watch($this);
  39. }
  40. });
  41. };
  42. // toggles the autoplay state for each slider
  43. methods.playPause = function () {
  44. return this.each(function (i, el) {
  45. togglePlayPause.call($(this));
  46. });
  47. };
  48. // show the slide at the given index
  49. methods.showSlide = function (index) {
  50. index = parseInt(index, 10);
  51. return this.each(function () {
  52. var $box = $(this);
  53. var settings = $box.data('bssettings');
  54. resetAutoScroll($box);
  55. showNextSlide($box, index, index < settings.bsfaceindex);
  56. });
  57. };
  58. // moves the slider to the next slide
  59. methods.next = function () {
  60. return this.each(function () {
  61. var $box = $(this);
  62. showNextSlide($box);
  63. });
  64. };
  65. // moves the slider to the previous slide
  66. methods.prev = function () {
  67. return this.each(function () {
  68. var $box = $(this);
  69. showNextSlide($box, null, true);
  70. });
  71. };
  72. // registers and configures a slide animator
  73. methods.registerAnimator = function (names, animator) {
  74. $.each(names.split(','), function (i, name) {
  75. slideAnimators[name] = animator;
  76. });
  77. animator._cacheOriginalCSS = cacheCSS;
  78. if (typeof animator.configure === 'function') {
  79. animator.configure(supports3D, vendorPrefix);
  80. }
  81. };
  82. // returns a slide animation adaptor
  83. methods.slideAnimator = function (effect) {
  84. if (typeof slideAnimators[effect] === 'object') {
  85. return slideAnimators[effect];
  86. }
  87. throw new Error(
  88. 'The slide animator for the ' + effect +
  89. ' effect has not been registered'
  90. );
  91. };
  92. // sets or gets an option for the set of matched sliders
  93. methods.option = function (setting, newValue) {
  94. if (typeof newValue === 'undefined') {
  95. return (this.data('bssettings') || {})[setting];
  96. }
  97. if (setting === 'effect') {
  98. throw new Error('Effect can only be set during plugin initialisation');
  99. }
  100. return this.each(function (i, el) {
  101. var $box = $(this)
  102. , settings = $box.data('bssettings') || {}
  103. , $slides = filterSlides($box.children(), settings);
  104. settings[setting] = newValue;
  105. resetAutoScroll($box, settings);
  106. //if (typeof settings.slideAnimator.reset === 'function') {
  107. // settings.slideAnimator.reset($box, $slides, settings);
  108. //}
  109. });
  110. };
  111. // destroy the plugin for the selected sliders
  112. methods.destroy = function () {
  113. return this.each(function () {
  114. var $box = $(this)
  115. , data = $box.data() || {}
  116. , settings = data.bssettings;
  117. if (settings && typeof settings.slideAnimator === 'object') {
  118. clearInterval(settings.autointv);
  119. settings.slideAnimator.destroy($box, settings);
  120. tearDownControls(settings);
  121. $box
  122. .removeData('bssettings')
  123. .removeClass('jbs-in-motion')
  124. .find('.jbs-current')
  125. .removeClass('jbs-current');
  126. }
  127. });
  128. };
  129. // Called on window resize if the responder is being used
  130. methods.resize = function ($sliders) {
  131. return $.each($sliders || this, function (i, slider) {
  132. var $slider = $(slider)
  133. , settings = $slider.data('bssettings');
  134. if ( // Call resize method on animator if it exists
  135. settings &&
  136. settings.slideAnimator &&
  137. $.isFunction(settings.slideAnimator.resize)
  138. ) {
  139. settings.slideAnimator.resize(
  140. $slider
  141. , filterSlides($slider, settings)
  142. , settings
  143. );
  144. }
  145. });
  146. };
  147. $.extend(jqBoxSlider, methods);
  148. // Event listeners and controls ----------------------------------------------
  149. // initialise controls for $box
  150. var setupControls = function ($box, settings) {
  151. var $controls = $();
  152. if (settings.next != null) {
  153. $controls = $controls.add($(settings.next).on(
  154. 'click', { reverse: false }, nextSlideListener
  155. ));
  156. }
  157. if (settings.prev != null) {
  158. $controls = $controls.add($(settings.prev).on(
  159. 'click', { reverse: true }, nextSlideListener
  160. ));
  161. }
  162. if (settings.pause != null) {
  163. $controls = $controls.add($(settings.pause).on(
  164. 'click', playPauseListener
  165. ));
  166. }
  167. $controls.data('bsbox', $box);
  168. };
  169. var tearDownControls = function (settings) {
  170. $(
  171. settings.next,
  172. settings.prev,
  173. settings.pause
  174. )
  175. .off('click', nextSlideListener)
  176. .off('click', playPauseListener)
  177. .removeData('bsbox');
  178. };
  179. // event listener for a next button
  180. var nextSlideListener = function (ev) {
  181. var $box = $(this).data('bsbox');
  182. resetAutoScroll($box);
  183. showNextSlide($box, undefined, ev.data.reverse);
  184. ev.preventDefault();
  185. };
  186. // event listener for play pause button
  187. var playPauseListener = function (ev) {
  188. var $this = $(this)
  189. , $box = $this.data('bsbox');
  190. togglePlayPause.call($box);
  191. $this.toggleClass('paused');
  192. ev.preventDefault();
  193. };
  194. // event listener for pause on hover
  195. var togglePlayPause = function (ev, reset, settings) {
  196. var $box = $(this)
  197. , playing;
  198. settings = settings || $box.data('bssettings');
  199. playing = settings.autointv != null;
  200. if (typeof settings.onplaypause === 'function') {
  201. settings.onplaypause.call($box, playing ? 'pause' : 'play');
  202. }
  203. if (playing || reset) {
  204. settings.autointv = clearInterval(settings.autointv);
  205. if (!reset) return;
  206. }
  207. settings.autointv = setInterval(function () {
  208. showNextSlide($box);
  209. }, settings.timeout + settings.speed);
  210. };
  211. // moves the slider to the next or previous slide
  212. var showNextSlide = function ($box, index, reverse) {
  213. var settings = $box.data('bssettings')
  214. , $slides = filterSlides($box, settings)
  215. , currIndex
  216. , nextIndex
  217. , $currSlide
  218. , $nextSlide;
  219. currIndex = settings.bsfaceindex || 0;
  220. nextIndex = calculateIndex(currIndex, $slides.length, reverse, index);
  221. // only go forward if not already in motion
  222. // and user defined index is not out of bounds
  223. if ($box.hasClass('jbs-in-motion') || nextIndex === -1) return;
  224. $currSlide = $slides.eq(currIndex);
  225. $nextSlide = $slides.eq(nextIndex);
  226. $box.addClass('jbs-in-motion'); // stops user clunking through faces ----- FIXME: queue user clicks and keep rotating the box
  227. if (typeof settings.onbefore === 'function') {
  228. settings.onbefore.call($box, $currSlide, $nextSlide, currIndex, nextIndex);
  229. }
  230. // add additional settings for the transition and
  231. // call the slide animation
  232. $.extend(settings, settings.slideAnimator.transition($.extend({
  233. $box: $box
  234. , $slides: $slides
  235. , $currSlide: $currSlide
  236. , $nextSlide: $nextSlide
  237. , reverse: reverse
  238. , currIndex: currIndex
  239. , nextIndex: nextIndex
  240. }, settings)));
  241. setTimeout( // remove the active flag class once transition is complete
  242. function () {
  243. $box.removeClass('jbs-in-motion');
  244. $currSlide.removeClass('jbs-current');
  245. $nextSlide.addClass('jbs-current');
  246. if (typeof settings.onafter === 'function') {
  247. settings.onafter.call($box, $currSlide, $nextSlide, currIndex, nextIndex);
  248. }
  249. }
  250. , settings.speed
  251. );
  252. // cache settings for next transition
  253. settings.bsfaceindex = nextIndex;
  254. };
  255. var filterSlides = function ($box, settings) {
  256. var $slides = $box.children();
  257. // User defined slide filter
  258. if (typeof settings.slideFilter === 'function') {
  259. $slides = $slides.filter(settings.slideFilter);
  260. }
  261. // Effect defined filter
  262. if (typeof settings._slideFilter === 'function') {
  263. $slides = $slides.filter(function (index) {
  264. return settings._slideFilter.call($slides, index, settings);
  265. });
  266. }
  267. return $slides;
  268. };
  269. // if the box is autoscrolling it is reset
  270. var resetAutoScroll = function ($box, settings) {
  271. settings || (settings = $box.data('bssettings') || {});
  272. if (settings.autoScroll) {
  273. togglePlayPause.call($box, undefined, true, settings);
  274. }
  275. };
  276. // get the next slides index
  277. var calculateIndex = function (currIndex, slidesLen, reverse, index) {
  278. var nextIndex = index;
  279. if (nextIndex == null) { // came from next button click
  280. if (reverse) {
  281. nextIndex = currIndex - 1 < 0 ? slidesLen - 1 : currIndex - 1;
  282. }
  283. else {
  284. nextIndex = currIndex + 1 < slidesLen ? currIndex + 1 : 0;
  285. }
  286. }
  287. if ( // already on selected slide or incorrect index
  288. nextIndex === currIndex ||
  289. nextIndex >= slidesLen ||
  290. nextIndex < 0
  291. ) { return -1; }
  292. return nextIndex;
  293. };
  294. // caches the desired css for reapplying the original styling when
  295. // the plugin is destroyed or reset
  296. var cacheCSS = function ($el, name, settings, extraAtts) {
  297. var el = $el.get(0)
  298. , attributes = [
  299. 'position', 'top', 'left', 'display', 'overflow',
  300. 'width', 'height', 'zIndex'
  301. ].concat(extraAtts || []);
  302. settings.origCSS || (settings.origCSS = {});
  303. settings.origCSS[name] || (settings.origCSS[name] = {});
  304. $.each(attributes, function (i, att) {
  305. settings.origCSS[name][att] = el.style[att];
  306. });
  307. };
  308. // set the correct vendor prefix for the css properties
  309. var vendorPrefix = (function () {
  310. var bs = document.body.style
  311. , prefix = '';
  312. if ('webkitTransition' in bs) {
  313. prefix = '-webkit-';
  314. }
  315. if ('MozTransition' in bs) {
  316. prefix = '-moz-';
  317. }
  318. supports3D = (
  319. 'webkitPerspective' in bs ||
  320. 'MozPerspective' in bs ||
  321. 'perspective' in bs
  322. );
  323. return prefix;
  324. }());
  325. $.fn.boxSlider = function (m) {
  326. if (typeof m === 'string' && typeof methods[m] === 'function') {
  327. return methods[m].apply(this, Array.prototype.slice.call(arguments, 1));
  328. }
  329. return methods.init.apply(this, arguments);
  330. };
  331. }(window, jQuery || Zepto));