2 * BxSlider v4.1.1 - Fully loaded, responsive content slider
5 * Copyright 2013, Steven Wanderski - http://stevenwanderski.com - http://bxcreative.com
6 * Written while drinking Belgian ales and listening to jazz
8 * Released under the MIT license - http://opensource.org/licenses/MIT
21 hideControlOnEnd: false,
30 adaptiveHeight: false,
31 adaptiveHeightSpeed: 500,
34 preloadImages: 'visible',
41 preventDefaultSwipeX: true,
42 preventDefaultSwipeY: false,
47 pagerShortSeparator: ' / ',
61 autoControlsCombine: false,
62 autoControlsSelector: null,
68 autoDirection: 'next',
79 onSliderLoad: function() {},
80 onSlideBefore: function() {},
81 onSlideAfter: function() {},
82 onSlideNext: function() {},
83 onSlidePrev: function() {}
86 $.fn.bxSlider = function(options){
88 if(this.length == 0) return this;
90 // support mutltiple elements
92 this.each(function(){$(this).bxSlider(options)});
96 // create a namespace to be used throughout the plugin
98 // set a reference to our slider element
103 * Makes slideshow responsive
105 // first get the original window dimens (thanks alot IE)
106 var windowWidth = $(window).width();
107 var windowHeight = $(window).height();
112 * ===================================================================================
113 * = PRIVATE FUNCTIONS
114 * ===================================================================================
118 * Initializes namespace settings to be used throughout plugin
120 var init = function(){
121 // merge user-supplied options with the defaults
122 slider.settings = $.extend({}, defaults, options);
123 // parse slideWidth setting
124 slider.settings.slideWidth = parseInt(slider.settings.slideWidth);
125 // store the original children
126 slider.children = el.children(slider.settings.slideSelector);
127 // check if actual number of slides is less than minSlides / maxSlides
128 if(slider.children.length < slider.settings.minSlides) slider.settings.minSlides = slider.children.length;
129 if(slider.children.length < slider.settings.maxSlides) slider.settings.maxSlides = slider.children.length;
130 // if random start, set the startSlide setting to random number
131 if(slider.settings.randomStart) slider.settings.startSlide = Math.floor(Math.random() * slider.children.length);
132 // store active slide information
133 slider.active = { index: slider.settings.startSlide }
134 // store if the slider is in carousel mode (displaying / moving multiple slides)
135 slider.carousel = slider.settings.minSlides > 1 || slider.settings.maxSlides > 1;
136 // if carousel, force preloadImages = 'all'
137 if(slider.carousel) slider.settings.preloadImages = 'all';
138 // calculate the min / max width thresholds based on min / max number of slides
139 // used to setup and update carousel slides dimensions
140 slider.minThreshold = (slider.settings.minSlides * slider.settings.slideWidth) + ((slider.settings.minSlides - 1) * slider.settings.slideMargin);
141 slider.maxThreshold = (slider.settings.maxSlides * slider.settings.slideWidth) + ((slider.settings.maxSlides - 1) * slider.settings.slideMargin);
142 // store the current state of the slider (if currently animating, working is true)
143 slider.working = false;
144 // initialize the controls object
145 slider.controls = {};
146 // initialize an auto interval
147 slider.interval = null;
148 // determine which property to use for transitions
149 slider.animProp = slider.settings.mode == 'vertical' ? 'top' : 'left';
150 // determine if hardware acceleration can be used
151 slider.usingCSS = slider.settings.useCSS && slider.settings.mode != 'fade' && (function(){
152 // create our test div element
153 var div = document.createElement('div');
154 // css transition properties
155 var props = ['WebkitPerspective', 'MozPerspective', 'OPerspective', 'msPerspective'];
156 // test for each property
158 if(div.style[props[i]] !== undefined){
159 slider.cssPrefix = props[i].replace('Perspective', '').toLowerCase();
160 slider.animProp = '-' + slider.cssPrefix + '-transform';
166 // if vertical mode always make maxSlides and minSlides equal
167 if(slider.settings.mode == 'vertical') slider.settings.maxSlides = slider.settings.minSlides;
168 // save original style data
169 el.data("origStyle", el.attr("style"));
170 el.children(slider.settings.slideSelector).each(function() {
171 $(this).data("origStyle", $(this).attr("style"));
173 // perform all DOM / CSS modifications
178 * Performs all DOM and CSS modifications
180 var setup = function(){
181 // wrap el in a wrapper
182 el.wrap('<div class="bx-wrapper"><div class="bx-viewport"></div></div>');
183 // store a namspace reference to .bx-viewport
184 slider.viewport = el.parent();
185 // add a loading div to display while images are loading
186 slider.loader = $('<div class="bx-loading" />');
187 slider.viewport.prepend(slider.loader);
188 // set el to a massive width, to hold any needed slides
189 // also strip any margin and padding from el
191 width: slider.settings.mode == 'horizontal' ? (slider.children.length * 100 + 215) + '%' : 'auto',
194 // if using CSS, add the easing property
195 if(slider.usingCSS && slider.settings.easing){
196 el.css('-' + slider.cssPrefix + '-transition-timing-function', slider.settings.easing);
197 // if not using CSS and no easing value was supplied, use the default JS animation easing (swing)
198 }else if(!slider.settings.easing){
199 slider.settings.easing = 'swing';
201 var slidesShowing = getNumberSlidesShowing();
202 // make modifications to the viewport (.bx-viewport)
203 slider.viewport.css({
208 slider.viewport.parent().css({
209 maxWidth: getViewportMaxWidth()
211 // make modification to the wrapper (.bx-wrapper)
212 if(!slider.settings.pager) {
213 slider.viewport.parent().css({
217 // apply css to all slider children
218 slider.children.css({
219 'float': slider.settings.mode == 'horizontal' ? 'left' : 'none',
223 // apply the calculated width after the float is applied to prevent scrollbar interference
224 slider.children.css('width', getSlideWidth());
225 // if slideMargin is supplied, add the css
226 if(slider.settings.mode == 'horizontal' && slider.settings.slideMargin > 0) slider.children.css('marginRight', slider.settings.slideMargin);
227 if(slider.settings.mode == 'vertical' && slider.settings.slideMargin > 0) slider.children.css('marginBottom', slider.settings.slideMargin);
228 // if "fade" mode, add positioning and z-index CSS
229 if(slider.settings.mode == 'fade'){
230 slider.children.css({
231 position: 'absolute',
235 // prepare the z-index on the showing element
236 slider.children.eq(slider.settings.startSlide).css({zIndex: 50, display: 'block'});
238 // create an element to contain all slider controls (pager, start / stop, etc)
239 slider.controls.el = $('<div class="bx-controls" />');
240 // if captions are requested, add them
241 if(slider.settings.captions) appendCaptions();
242 // check if startSlide is last slide
243 slider.active.last = slider.settings.startSlide == getPagerQty() - 1;
244 // if video is true, set up the fitVids plugin
245 if(slider.settings.video) el.fitVids();
246 // set the default preload selector (visible)
247 var preloadSelector = slider.children.eq(slider.settings.startSlide);
248 if (slider.settings.preloadImages == "all") preloadSelector = slider.children;
249 // only check for control addition if not in "ticker" mode
250 if(!slider.settings.ticker){
251 // if pager is requested, add it
252 if(slider.settings.pager) appendPager();
253 // if controls are requested, add them
254 if(slider.settings.controls) appendControls();
255 // if auto is true, and auto controls are requested, add them
256 if(slider.settings.auto && slider.settings.autoControls) appendControlsAuto();
257 // if any control option is requested, add the controls wrapper
258 if(slider.settings.controls || slider.settings.autoControls || slider.settings.pager) slider.viewport.after(slider.controls.el);
259 // if ticker mode, do not allow a pager
261 slider.settings.pager = false;
263 // preload all images, then perform final DOM / CSS modifications that depend on images being loaded
264 loadElements(preloadSelector, start);
267 var loadElements = function(selector, callback){
268 var total = selector.find('img, iframe').length;
274 selector.find('img, iframe').each(function(){
275 $(this).one('load', function() {
276 if(++count == total) callback();
278 if(this.complete) $(this).load();
286 var start = function(){
287 // if infinite loop, prepare additional slides
288 if(slider.settings.infiniteLoop && slider.settings.mode != 'fade' && !slider.settings.ticker){
289 var slice = slider.settings.mode == 'vertical' ? slider.settings.minSlides : slider.settings.maxSlides;
290 var sliceAppend = slider.children.slice(0, slice).clone().addClass('bx-clone');
291 var slicePrepend = slider.children.slice(-slice).clone().addClass('bx-clone');
292 el.append(sliceAppend).prepend(slicePrepend);
294 // remove the loading DOM element
295 slider.loader.remove();
296 // set the left / top position of "el"
298 // if "vertical" mode, always use adaptiveHeight to prevent odd behavior
299 if (slider.settings.mode == 'vertical') slider.settings.adaptiveHeight = true;
300 // set the viewport height
301 slider.viewport.height(getViewportHeight());
302 // make sure everything is positioned just right (same as a window resize)
304 // onSliderLoad callback
305 slider.settings.onSliderLoad(slider.active.index);
306 // slider has been fully initialized
307 slider.initialized = true;
308 // bind the resize call to the window
309 if (slider.settings.responsive) $(window).bind('resize', resizeWindow);
310 // if auto is true, start the show
311 if (slider.settings.auto && slider.settings.autoStart) initAuto();
312 // if ticker is true, start the ticker
313 if (slider.settings.ticker) initTicker();
314 // if pager is requested, make the appropriate pager link active
315 if (slider.settings.pager) updatePagerActive(slider.settings.startSlide);
316 // check for any updates to the controls (like hideControlOnEnd updates)
317 if (slider.settings.controls) updateDirectionControls();
318 // if touchEnabled is true, setup the touch events
319 if (slider.settings.touchEnabled && !slider.settings.ticker) initTouch();
323 * Returns the calculated height of the viewport, used to determine either adaptiveHeight or the maxHeight value
325 var getViewportHeight = function(){
327 // first determine which children (slides) should be used in our height calculation
329 // if mode is not "vertical" and adaptiveHeight is false, include all children
330 if(slider.settings.mode != 'vertical' && !slider.settings.adaptiveHeight){
331 children = slider.children;
333 // if not carousel, return the single active child
334 if(!slider.carousel){
335 children = slider.children.eq(slider.active.index);
336 // if carousel, return a slice of children
338 // get the individual slide index
339 var currentIndex = slider.settings.moveSlides == 1 ? slider.active.index : slider.active.index * getMoveBy();
340 // add the current slide to the children
341 children = slider.children.eq(currentIndex);
342 // cycle through the remaining "showing" slides
343 for (i = 1; i <= slider.settings.maxSlides - 1; i++){
344 // if looped back to the start
345 if(currentIndex + i >= slider.children.length){
346 children = children.add(slider.children.eq(i - 1));
348 children = children.add(slider.children.eq(currentIndex + i));
353 // if "vertical" mode, calculate the sum of the heights of the children
354 if(slider.settings.mode == 'vertical'){
355 children.each(function(index) {
356 height += $(this).outerHeight();
358 // add user-supplied margins
359 if(slider.settings.slideMargin > 0){
360 height += slider.settings.slideMargin * (slider.settings.minSlides - 1);
362 // if not "vertical" mode, calculate the max height of the children
364 height = Math.max.apply(Math, children.map(function(){
365 return $(this).outerHeight(false);
372 * Returns the calculated width to be used for the outer wrapper / viewport
374 var getViewportMaxWidth = function(){
376 if(slider.settings.slideWidth > 0){
377 if(slider.settings.mode == 'horizontal'){
378 width = (slider.settings.maxSlides * slider.settings.slideWidth) + ((slider.settings.maxSlides - 1) * slider.settings.slideMargin);
380 width = slider.settings.slideWidth;
387 * Returns the calculated width to be applied to each slide
389 var getSlideWidth = function(){
390 // start with any user-supplied slide width
391 var newElWidth = slider.settings.slideWidth;
392 // get the current viewport width
393 var wrapWidth = slider.viewport.width();
394 // if slide width was not supplied, or is larger than the viewport use the viewport width
395 if(slider.settings.slideWidth == 0 ||
396 (slider.settings.slideWidth > wrapWidth && !slider.carousel) ||
397 slider.settings.mode == 'vertical'){
398 newElWidth = wrapWidth;
399 // if carousel, use the thresholds to determine the width
400 }else if(slider.settings.maxSlides > 1 && slider.settings.mode == 'horizontal'){
401 if(wrapWidth > slider.maxThreshold){
402 // newElWidth = (wrapWidth - (slider.settings.slideMargin * (slider.settings.maxSlides - 1))) / slider.settings.maxSlides;
403 }else if(wrapWidth < slider.minThreshold){
404 newElWidth = (wrapWidth - (slider.settings.slideMargin * (slider.settings.minSlides - 1))) / slider.settings.minSlides;
411 * Returns the number of slides currently visible in the viewport (includes partially visible slides)
413 var getNumberSlidesShowing = function(){
414 var slidesShowing = 1;
415 if(slider.settings.mode == 'horizontal' && slider.settings.slideWidth > 0){
416 // if viewport is smaller than minThreshold, return minSlides
417 if(slider.viewport.width() < slider.minThreshold){
418 slidesShowing = slider.settings.minSlides;
419 // if viewport is larger than minThreshold, return maxSlides
420 }else if(slider.viewport.width() > slider.maxThreshold){
421 slidesShowing = slider.settings.maxSlides;
422 // if viewport is between min / max thresholds, divide viewport width by first child width
424 var childWidth = slider.children.first().width();
425 slidesShowing = Math.floor(slider.viewport.width() / childWidth);
427 // if "vertical" mode, slides showing will always be minSlides
428 }else if(slider.settings.mode == 'vertical'){
429 slidesShowing = slider.settings.minSlides;
431 return slidesShowing;
435 * Returns the number of pages (one full viewport of slides is one "page")
437 var getPagerQty = function(){
439 // if moveSlides is specified by the user
440 if(slider.settings.moveSlides > 0){
441 if(slider.settings.infiniteLoop){
442 pagerQty = slider.children.length / getMoveBy();
444 // use a while loop to determine pages
447 // when breakpoint goes above children length, counter is the number of pages
448 while (breakPoint < slider.children.length){
450 breakPoint = counter + getNumberSlidesShowing();
451 counter += slider.settings.moveSlides <= getNumberSlidesShowing() ? slider.settings.moveSlides : getNumberSlidesShowing();
454 // if moveSlides is 0 (auto) divide children length by sides showing, then round up
456 pagerQty = Math.ceil(slider.children.length / getNumberSlidesShowing());
462 * Returns the number of indivual slides by which to shift the slider
464 var getMoveBy = function(){
465 // if moveSlides was set by the user and moveSlides is less than number of slides showing
466 if(slider.settings.moveSlides > 0 && slider.settings.moveSlides <= getNumberSlidesShowing()){
467 return slider.settings.moveSlides;
469 // if moveSlides is 0 (auto)
470 return getNumberSlidesShowing();
474 * Sets the slider's (el) left or top position
476 var setSlidePosition = function(){
477 // if last slide, not infinite loop, and number of children is larger than specified maxSlides
478 if(slider.children.length > slider.settings.maxSlides && slider.active.last && !slider.settings.infiniteLoop){
479 if (slider.settings.mode == 'horizontal'){
480 // get the last child's position
481 var lastChild = slider.children.last();
482 var position = lastChild.position();
483 // set the left position
484 setPositionProperty(-(position.left - (slider.viewport.width() - lastChild.width())), 'reset', 0);
485 }else if(slider.settings.mode == 'vertical'){
486 // get the last showing index's position
487 var lastShowingIndex = slider.children.length - slider.settings.minSlides;
488 var position = slider.children.eq(lastShowingIndex).position();
489 // set the top position
490 setPositionProperty(-position.top, 'reset', 0);
494 // get the position of the first showing slide
495 var position = slider.children.eq(slider.active.index * getMoveBy()).position();
496 // check for last slide
497 if (slider.active.index == getPagerQty() - 1) slider.active.last = true;
498 // set the repective position
499 if (position != undefined){
500 if (slider.settings.mode == 'horizontal') setPositionProperty(-position.left, 'reset', 0);
501 else if (slider.settings.mode == 'vertical') setPositionProperty(-position.top, 'reset', 0);
507 * Sets the el's animating property position (which in turn will sometimes animate el).
508 * If using CSS, sets the transform property. If not using CSS, sets the top / left property.
511 * - the animating property's value
513 * @param type (string) 'slider', 'reset', 'ticker'
514 * - the type of instance for which the function is being
516 * @param duration (int)
517 * - the amount of time (in ms) the transition should occupy
519 * @param params (array) optional
520 * - an optional parameter containing any variables that need to be passed in
522 var setPositionProperty = function(value, type, duration, params){
525 // determine the translate3d value
526 var propValue = slider.settings.mode == 'vertical' ? 'translate3d(0, ' + value + 'px, 0)' : 'translate3d(' + value + 'px, 0, 0)';
527 // add the CSS transition-duration
528 el.css('-' + slider.cssPrefix + '-transition-duration', duration / 1000 + 's');
530 // set the property value
531 el.css(slider.animProp, propValue);
532 // bind a callback method - executes when CSS transition completes
533 el.bind('transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd', function(){
534 // unbind the callback
535 el.unbind('transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd');
536 updateAfterSlideTransition();
538 }else if(type == 'reset'){
539 el.css(slider.animProp, propValue);
540 }else if(type == 'ticker'){
541 // make the transition use 'linear'
542 el.css('-' + slider.cssPrefix + '-transition-timing-function', 'linear');
543 el.css(slider.animProp, propValue);
544 // bind a callback method - executes when CSS transition completes
545 el.bind('transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd', function(){
546 // unbind the callback
547 el.unbind('transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd');
548 // reset the position
549 setPositionProperty(params['resetValue'], 'reset', 0);
550 // start the loop again
557 animateObj[slider.animProp] = value;
559 el.animate(animateObj, duration, slider.settings.easing, function(){
560 updateAfterSlideTransition();
562 }else if(type == 'reset'){
563 el.css(slider.animProp, value)
564 }else if(type == 'ticker'){
565 el.animate(animateObj, speed, 'linear', function(){
566 setPositionProperty(params['resetValue'], 'reset', 0);
567 // run the recursive loop after animation
575 * Populates the pager with proper amount of pages
577 var populatePager = function(){
579 var pagerQty = getPagerQty();
580 // loop through each pager item
581 for(var i=0; i < pagerQty; i++){
582 var linkContent = '';
583 // if a buildPager function is supplied, use it to get pager link value, else use index + 1
584 if(slider.settings.buildPager && $.isFunction(slider.settings.buildPager)){
585 linkContent = slider.settings.buildPager(i);
586 slider.pagerEl.addClass('bx-custom-pager');
589 slider.pagerEl.addClass('bx-default-pager');
591 // var linkContent = slider.settings.buildPager && $.isFunction(slider.settings.buildPager) ? slider.settings.buildPager(i) : i + 1;
592 // add the markup to the string
593 pagerHtml += '<div class="bx-pager-item"><a href="" data-slide-index="' + i + '" class="bx-pager-link">' + linkContent + '</a></div>';
595 // populate the pager element with pager links
596 slider.pagerEl.html(pagerHtml);
600 * Appends the pager to the controls element
602 var appendPager = function(){
603 if(!slider.settings.pagerCustom){
604 // create the pager DOM element
605 slider.pagerEl = $('<div class="bx-pager" />');
606 // if a pager selector was supplied, populate it with the pager
607 if(slider.settings.pagerSelector){
608 $(slider.settings.pagerSelector).html(slider.pagerEl);
609 // if no pager selector was supplied, add it after the wrapper
611 slider.controls.el.addClass('bx-has-pager').append(slider.pagerEl);
613 // populate the pager
616 slider.pagerEl = $(slider.settings.pagerCustom);
618 // assign the pager click binding
619 slider.pagerEl.delegate('a', 'click', clickPagerBind);
623 * Appends prev / next controls to the controls element
625 var appendControls = function(){
626 slider.controls.next = $('<a class="bx-next" href="">' + slider.settings.nextText + '</a>');
627 slider.controls.prev = $('<a class="bx-prev" href="">' + slider.settings.prevText + '</a>');
628 // bind click actions to the controls
629 slider.controls.next.bind('click', clickNextBind);
630 slider.controls.prev.bind('click', clickPrevBind);
631 // if nextSlector was supplied, populate it
632 if(slider.settings.nextSelector){
633 $(slider.settings.nextSelector).append(slider.controls.next);
635 // if prevSlector was supplied, populate it
636 if(slider.settings.prevSelector){
637 $(slider.settings.prevSelector).append(slider.controls.prev);
639 // if no custom selectors were supplied
640 if(!slider.settings.nextSelector && !slider.settings.prevSelector){
641 // add the controls to the DOM
642 slider.controls.directionEl = $('<div class="bx-controls-direction" />');
643 // add the control elements to the directionEl
644 slider.controls.directionEl.append(slider.controls.prev).append(slider.controls.next);
645 // slider.viewport.append(slider.controls.directionEl);
646 slider.controls.el.addClass('bx-has-controls-direction').append(slider.controls.directionEl);
651 * Appends start / stop auto controls to the controls element
653 var appendControlsAuto = function(){
654 slider.controls.start = $('<div class="bx-controls-auto-item"><a class="bx-start" href="">' + slider.settings.startText + '</a></div>');
655 slider.controls.stop = $('<div class="bx-controls-auto-item"><a class="bx-stop" href="">' + slider.settings.stopText + '</a></div>');
656 // add the controls to the DOM
657 slider.controls.autoEl = $('<div class="bx-controls-auto" />');
658 // bind click actions to the controls
659 slider.controls.autoEl.delegate('.bx-start', 'click', clickStartBind);
660 slider.controls.autoEl.delegate('.bx-stop', 'click', clickStopBind);
661 // if autoControlsCombine, insert only the "start" control
662 if(slider.settings.autoControlsCombine){
663 slider.controls.autoEl.append(slider.controls.start);
664 // if autoControlsCombine is false, insert both controls
666 slider.controls.autoEl.append(slider.controls.start).append(slider.controls.stop);
668 // if auto controls selector was supplied, populate it with the controls
669 if(slider.settings.autoControlsSelector){
670 $(slider.settings.autoControlsSelector).html(slider.controls.autoEl);
671 // if auto controls selector was not supplied, add it after the wrapper
673 slider.controls.el.addClass('bx-has-controls-auto').append(slider.controls.autoEl);
675 // update the auto controls
676 updateAutoControls(slider.settings.autoStart ? 'stop' : 'start');
680 * Appends image captions to the DOM
682 var appendCaptions = function(){
683 // cycle through each child
684 slider.children.each(function(index){
685 // get the image title attribute
686 var title = $(this).find('img:first').attr('title');
687 // append the caption
688 if (title != undefined && ('' + title).length) {
689 $(this).append('<div class="bx-caption"><span>' + title + '</span></div>');
700 var clickNextBind = function(e){
701 // if auto show is running, stop it
702 if (slider.settings.auto) el.stopAuto();
713 var clickPrevBind = function(e){
714 // if auto show is running, stop it
715 if (slider.settings.auto) el.stopAuto();
721 * Click start binding
726 var clickStartBind = function(e){
737 var clickStopBind = function(e){
743 * Click pager binding
748 var clickPagerBind = function(e){
749 // if auto show is running, stop it
750 if (slider.settings.auto) el.stopAuto();
751 var pagerLink = $(e.currentTarget);
752 var pagerIndex = parseInt(pagerLink.attr('data-slide-index'));
753 // if clicked pager link is not active, continue with the goToSlide call
754 if(pagerIndex != slider.active.index) el.goToSlide(pagerIndex);
759 * Updates the pager links with an active class
761 * @param slideIndex (int)
762 * - index of slide to make active
764 var updatePagerActive = function(slideIndex){
765 // if "short" pager type
766 var len = slider.children.length; // nb of children
767 if(slider.settings.pagerType == 'short'){
768 if(slider.settings.maxSlides > 1) {
769 len = Math.ceil(slider.children.length/slider.settings.maxSlides);
771 slider.pagerEl.html( (slideIndex + 1) + slider.settings.pagerShortSeparator + len);
774 // remove all pager active classes
775 slider.pagerEl.find('a').removeClass('active');
776 // apply the active class for all pagers
777 slider.pagerEl.each(function(i, el) { $(el).find('a').eq(slideIndex).addClass('active'); });
781 * Performs needed actions after a slide transition
783 var updateAfterSlideTransition = function(){
784 // if infinte loop is true
785 if(slider.settings.infiniteLoop){
788 if(slider.active.index == 0){
789 // set the new position
790 position = slider.children.eq(0).position();
791 // carousel, last slide
792 }else if(slider.active.index == getPagerQty() - 1 && slider.carousel){
793 position = slider.children.eq((getPagerQty() - 1) * getMoveBy()).position();
795 }else if(slider.active.index == slider.children.length - 1){
796 position = slider.children.eq(slider.children.length - 1).position();
798 if (slider.settings.mode == 'horizontal') { setPositionProperty(-position.left, 'reset', 0);; }
799 else if (slider.settings.mode == 'vertical') { setPositionProperty(-position.top, 'reset', 0);; }
801 // declare that the transition is complete
802 slider.working = false;
803 // onSlideAfter callback
804 slider.settings.onSlideAfter(slider.children.eq(slider.active.index), slider.oldIndex, slider.active.index);
808 * Updates the auto controls state (either active, or combined switch)
810 * @param state (string) "start", "stop"
811 * - the new state of the auto show
813 var updateAutoControls = function(state){
814 // if autoControlsCombine is true, replace the current control with the new state
815 if(slider.settings.autoControlsCombine){
816 slider.controls.autoEl.html(slider.controls[state]);
817 // if autoControlsCombine is false, apply the "active" class to the appropriate control
819 slider.controls.autoEl.find('a').removeClass('active');
820 slider.controls.autoEl.find('a:not(.bx-' + state + ')').addClass('active');
825 * Updates the direction controls (checks if either should be hidden)
827 var updateDirectionControls = function(){
828 if(getPagerQty() == 1){
829 slider.controls.prev.addClass('disabled');
830 slider.controls.next.addClass('disabled');
831 }else if(!slider.settings.infiniteLoop && slider.settings.hideControlOnEnd){
833 if (slider.active.index == 0){
834 slider.controls.prev.addClass('disabled');
835 slider.controls.next.removeClass('disabled');
837 }else if(slider.active.index == getPagerQty() - 1){
838 slider.controls.next.addClass('disabled');
839 slider.controls.prev.removeClass('disabled');
840 // if any slide in the middle
842 slider.controls.prev.removeClass('disabled');
843 slider.controls.next.removeClass('disabled');
849 * Initialzes the auto process
851 var initAuto = function(){
852 // if autoDelay was supplied, launch the auto show using a setTimeout() call
853 if(slider.settings.autoDelay > 0){
854 var timeout = setTimeout(el.startAuto, slider.settings.autoDelay);
855 // if autoDelay was not supplied, start the auto show normally
859 // if autoHover is requested
860 if(slider.settings.autoHover){
863 // if the auto show is currently playing (has an active interval)
865 // stop the auto show and pass true agument which will prevent control update
867 // create a new autoPaused value which will be used by the relative "mouseout" event
868 slider.autoPaused = true;
871 // if the autoPaused value was created be the prior "mouseover" event
872 if(slider.autoPaused){
873 // start the auto show and pass true agument which will prevent control update
875 // reset the autoPaused value
876 slider.autoPaused = null;
883 * Initialzes the ticker process
885 var initTicker = function(){
886 var startPosition = 0;
887 // if autoDirection is "next", append a clone of the entire slider
888 if(slider.settings.autoDirection == 'next'){
889 el.append(slider.children.clone().addClass('bx-clone'));
890 // if autoDirection is "prev", prepend a clone of the entire slider, and set the left position
892 el.prepend(slider.children.clone().addClass('bx-clone'));
893 var position = slider.children.first().position();
894 startPosition = slider.settings.mode == 'horizontal' ? -position.left : -position.top;
896 setPositionProperty(startPosition, 'reset', 0);
897 // do not allow controls in ticker mode
898 slider.settings.pager = false;
899 slider.settings.controls = false;
900 slider.settings.autoControls = false;
901 // if autoHover is requested
902 if(slider.settings.tickerHover && !slider.usingCSS){
904 slider.viewport.hover(function(){
907 // calculate the total width of children (used to calculate the speed ratio)
909 slider.children.each(function(index){
910 totalDimens += slider.settings.mode == 'horizontal' ? $(this).outerWidth(true) : $(this).outerHeight(true);
912 // calculate the speed ratio (used to determine the new speed to finish the paused animation)
913 var ratio = slider.settings.speed / totalDimens;
914 // determine which property to use
915 var property = slider.settings.mode == 'horizontal' ? 'left' : 'top';
916 // calculate the new speed
917 var newSpeed = ratio * (totalDimens - (Math.abs(parseInt(el.css(property)))));
918 tickerLoop(newSpeed);
921 // start the ticker loop
926 * Runs a continuous loop, news ticker-style
928 var tickerLoop = function(resumeSpeed){
929 speed = resumeSpeed ? resumeSpeed : slider.settings.speed;
930 var position = {left: 0, top: 0};
931 var reset = {left: 0, top: 0};
932 // if "next" animate left position to last child, then reset left to 0
933 if(slider.settings.autoDirection == 'next'){
934 position = el.find('.bx-clone').first().position();
935 // if "prev" animate left position to 0, then reset left to first non-clone child
937 reset = slider.children.first().position();
939 var animateProperty = slider.settings.mode == 'horizontal' ? -position.left : -position.top;
940 var resetValue = slider.settings.mode == 'horizontal' ? -reset.left : -reset.top;
941 var params = {resetValue: resetValue};
942 setPositionProperty(animateProperty, 'ticker', speed, params);
946 * Initializes touch events
948 var initTouch = function(){
949 // initialize object to contain all touch values
954 slider.viewport.bind('touchstart', onTouchStart);
958 * Event handler for "touchstart"
963 var onTouchStart = function(e){
967 // record the original position when touch starts
968 slider.touch.originalPos = el.position();
969 var orig = e.originalEvent;
970 // record the starting touch x, y coordinates
971 slider.touch.start.x = orig.changedTouches[0].pageX;
972 slider.touch.start.y = orig.changedTouches[0].pageY;
973 // bind a "touchmove" event to the viewport
974 slider.viewport.bind('touchmove', onTouchMove);
975 // bind a "touchend" event to the viewport
976 slider.viewport.bind('touchend', onTouchEnd);
981 * Event handler for "touchmove"
986 var onTouchMove = function(e){
987 var orig = e.originalEvent;
988 // if scrolling on y axis, do not prevent default
989 var xMovement = Math.abs(orig.changedTouches[0].pageX - slider.touch.start.x);
990 var yMovement = Math.abs(orig.changedTouches[0].pageY - slider.touch.start.y);
992 if((xMovement * 3) > yMovement && slider.settings.preventDefaultSwipeX){
995 }else if((yMovement * 3) > xMovement && slider.settings.preventDefaultSwipeY){
998 if(slider.settings.mode != 'fade' && slider.settings.oneToOneTouch){
1000 // if horizontal, drag along x axis
1001 if(slider.settings.mode == 'horizontal'){
1002 var change = orig.changedTouches[0].pageX - slider.touch.start.x;
1003 value = slider.touch.originalPos.left + change;
1004 // if vertical, drag along y axis
1006 var change = orig.changedTouches[0].pageY - slider.touch.start.y;
1007 value = slider.touch.originalPos.top + change;
1009 setPositionProperty(value, 'reset', 0);
1014 * Event handler for "touchend"
1017 * - DOM event object
1019 var onTouchEnd = function(e){
1020 slider.viewport.unbind('touchmove', onTouchMove);
1021 var orig = e.originalEvent;
1023 // record end x, y positions
1024 slider.touch.end.x = orig.changedTouches[0].pageX;
1025 slider.touch.end.y = orig.changedTouches[0].pageY;
1026 // if fade mode, check if absolute x distance clears the threshold
1027 if(slider.settings.mode == 'fade'){
1028 var distance = Math.abs(slider.touch.start.x - slider.touch.end.x);
1029 if(distance >= slider.settings.swipeThreshold){
1030 slider.touch.start.x > slider.touch.end.x ? el.goToNextSlide() : el.goToPrevSlide();
1036 // calculate distance and el's animate property
1037 if(slider.settings.mode == 'horizontal'){
1038 distance = slider.touch.end.x - slider.touch.start.x;
1039 value = slider.touch.originalPos.left;
1041 distance = slider.touch.end.y - slider.touch.start.y;
1042 value = slider.touch.originalPos.top;
1044 // if not infinite loop and first / last slide, do not attempt a slide transition
1045 if(!slider.settings.infiniteLoop && ((slider.active.index == 0 && distance > 0) || (slider.active.last && distance < 0))){
1046 setPositionProperty(value, 'reset', 200);
1048 // check if distance clears threshold
1049 if(Math.abs(distance) >= slider.settings.swipeThreshold){
1050 distance < 0 ? el.goToNextSlide() : el.goToPrevSlide();
1053 // el.animate(property, 200);
1054 setPositionProperty(value, 'reset', 200);
1058 slider.viewport.unbind('touchend', onTouchEnd);
1062 * Window resize event callback
1064 var resizeWindow = function(e){
1065 // get the new window dimens (again, thank you IE)
1066 var windowWidthNew = $(window).width();
1067 var windowHeightNew = $(window).height();
1068 // make sure that it is a true window resize
1069 // *we must check this because our dinosaur friend IE fires a window resize event when certain DOM elements
1070 // are resized. Can you just die already?*
1071 if(windowWidth != windowWidthNew || windowHeight != windowHeightNew){
1072 // set the new window dimens
1073 windowWidth = windowWidthNew;
1074 windowHeight = windowHeightNew;
1075 // update all dynamic elements
1081 * ===================================================================================
1082 * = PUBLIC FUNCTIONS
1083 * ===================================================================================
1087 * Performs slide transition to the specified slide
1089 * @param slideIndex (int)
1090 * - the destination slide's index (zero-based)
1092 * @param direction (string)
1093 * - INTERNAL USE ONLY - the direction of travel ("prev" / "next")
1095 el.goToSlide = function(slideIndex, direction){
1096 // if plugin is currently in motion, ignore request
1097 if(slider.working || slider.active.index == slideIndex) return;
1098 // declare that plugin is in motion
1099 slider.working = true;
1100 // store the old index
1101 slider.oldIndex = slider.active.index;
1102 // if slideIndex is less than zero, set active index to last child (this happens during infinite loop)
1104 slider.active.index = getPagerQty() - 1;
1105 // if slideIndex is greater than children length, set active index to 0 (this happens during infinite loop)
1106 }else if(slideIndex >= getPagerQty()){
1107 slider.active.index = 0;
1108 // set active index to requested slide
1110 slider.active.index = slideIndex;
1112 // onSlideBefore, onSlideNext, onSlidePrev callbacks
1113 slider.settings.onSlideBefore(slider.children.eq(slider.active.index), slider.oldIndex, slider.active.index);
1114 if(direction == 'next'){
1115 slider.settings.onSlideNext(slider.children.eq(slider.active.index), slider.oldIndex, slider.active.index);
1116 }else if(direction == 'prev'){
1117 slider.settings.onSlidePrev(slider.children.eq(slider.active.index), slider.oldIndex, slider.active.index);
1119 // check if last slide
1120 slider.active.last = slider.active.index >= getPagerQty() - 1;
1121 // update the pager with active class
1122 if(slider.settings.pager) updatePagerActive(slider.active.index);
1123 // // check for direction control update
1124 if(slider.settings.controls) updateDirectionControls();
1125 // if slider is set to mode: "fade"
1126 if(slider.settings.mode == 'fade'){
1127 // if adaptiveHeight is true and next height is different from current height, animate to the new height
1128 if(slider.settings.adaptiveHeight && slider.viewport.height() != getViewportHeight()){
1129 slider.viewport.animate({height: getViewportHeight()}, slider.settings.adaptiveHeightSpeed);
1131 // fade out the visible child and reset its z-index value
1132 slider.children.filter(':visible').fadeOut(slider.settings.speed).css({zIndex: 0});
1133 // fade in the newly requested slide
1134 slider.children.eq(slider.active.index).css('zIndex', 51).fadeIn(slider.settings.speed, function(){
1135 $(this).css('zIndex', 50);
1136 updateAfterSlideTransition();
1138 // slider mode is not "fade"
1140 // if adaptiveHeight is true and next height is different from current height, animate to the new height
1141 if(slider.settings.adaptiveHeight && slider.viewport.height() != getViewportHeight()){
1142 slider.viewport.animate({height: getViewportHeight()}, slider.settings.adaptiveHeightSpeed);
1145 var position = {left: 0, top: 0};
1146 // if carousel and not infinite loop
1147 if(!slider.settings.infiniteLoop && slider.carousel && slider.active.last){
1148 if(slider.settings.mode == 'horizontal'){
1149 // get the last child position
1150 var lastChild = slider.children.eq(slider.children.length - 1);
1151 position = lastChild.position();
1152 // calculate the position of the last slide
1153 moveBy = slider.viewport.width() - lastChild.outerWidth();
1155 // get last showing index position
1156 var lastShowingIndex = slider.children.length - slider.settings.minSlides;
1157 position = slider.children.eq(lastShowingIndex).position();
1159 // horizontal carousel, going previous while on first slide (infiniteLoop mode)
1160 }else if(slider.carousel && slider.active.last && direction == 'prev'){
1161 // get the last child position
1162 var eq = slider.settings.moveSlides == 1 ? slider.settings.maxSlides - getMoveBy() : ((getPagerQty() - 1) * getMoveBy()) - (slider.children.length - slider.settings.maxSlides);
1163 var lastChild = el.children('.bx-clone').eq(eq);
1164 position = lastChild.position();
1165 // if infinite loop and "Next" is clicked on the last slide
1166 }else if(direction == 'next' && slider.active.index == 0){
1167 // get the last clone position
1168 position = el.find('> .bx-clone').eq(slider.settings.maxSlides).position();
1169 slider.active.last = false;
1170 // normal non-zero requests
1171 }else if(slideIndex >= 0){
1172 var requestEl = slideIndex * getMoveBy();
1173 position = slider.children.eq(requestEl).position();
1176 /* If the position doesn't exist
1177 * (e.g. if you destroy the slider on a next click),
1178 * it doesn't throw an error.
1180 if ("undefined" !== typeof(position)) {
1181 var value = slider.settings.mode == 'horizontal' ? -(position.left - moveBy) : -position.top;
1182 // plugin values to be animated
1183 setPositionProperty(value, 'slide', slider.settings.speed);
1189 * Transitions to the next slide in the show
1191 el.goToNextSlide = function(){
1192 // if infiniteLoop is false and last page is showing, disregard call
1193 if (!slider.settings.infiniteLoop && slider.active.last) return;
1194 var pagerIndex = parseInt(slider.active.index) + 1;
1195 el.goToSlide(pagerIndex, 'next');
1199 * Transitions to the prev slide in the show
1201 el.goToPrevSlide = function(){
1202 // if infiniteLoop is false and last page is showing, disregard call
1203 if (!slider.settings.infiniteLoop && slider.active.index == 0) return;
1204 var pagerIndex = parseInt(slider.active.index) - 1;
1205 el.goToSlide(pagerIndex, 'prev');
1209 * Starts the auto show
1211 * @param preventControlUpdate (boolean)
1212 * - if true, auto controls state will not be updated
1214 el.startAuto = function(preventControlUpdate){
1215 // if an interval already exists, disregard call
1216 if(slider.interval) return;
1217 // create an interval
1218 slider.interval = setInterval(function(){
1219 slider.settings.autoDirection == 'next' ? el.goToNextSlide() : el.goToPrevSlide();
1220 }, slider.settings.pause);
1221 // if auto controls are displayed and preventControlUpdate is not true
1222 if (slider.settings.autoControls && preventControlUpdate != true) updateAutoControls('stop');
1226 * Stops the auto show
1228 * @param preventControlUpdate (boolean)
1229 * - if true, auto controls state will not be updated
1231 el.stopAuto = function(preventControlUpdate){
1232 // if no interval exists, disregard call
1233 if(!slider.interval) return;
1234 // clear the interval
1235 clearInterval(slider.interval);
1236 slider.interval = null;
1237 // if auto controls are displayed and preventControlUpdate is not true
1238 if (slider.settings.autoControls && preventControlUpdate != true) updateAutoControls('start');
1242 * Returns current slide index (zero-based)
1244 el.getCurrentSlide = function(){
1245 return slider.active.index;
1249 * Returns number of slides in show
1251 el.getSlideCount = function(){
1252 return slider.children.length;
1256 * Update all dynamic slider elements
1258 el.redrawSlider = function(){
1259 // resize all children in ratio to new screen size
1260 slider.children.add(el.find('.bx-clone')).outerWidth(getSlideWidth());
1261 // adjust the height
1262 slider.viewport.css('height', getViewportHeight());
1263 // update the slide position
1264 if(!slider.settings.ticker) setSlidePosition();
1265 // if active.last was true before the screen resize, we want
1266 // to keep it last no matter what screen size we end on
1267 if (slider.active.last) slider.active.index = getPagerQty() - 1;
1268 // if the active index (page) no longer exists due to the resize, simply set the index as last
1269 if (slider.active.index >= getPagerQty()) slider.active.last = true;
1270 // if a pager is being displayed and a custom pager is not being used, update it
1271 if(slider.settings.pager && !slider.settings.pagerCustom){
1273 updatePagerActive(slider.active.index);
1278 * Destroy the current instance of the slider (revert everything back to original state)
1280 el.destroySlider = function(){
1281 // don't do anything if slider has already been destroyed
1282 if(!slider.initialized) return;
1283 slider.initialized = false;
1284 $('.bx-clone', this).remove();
1285 slider.children.each(function() {
1286 $(this).data("origStyle") != undefined ? $(this).attr("style", $(this).data("origStyle")) : $(this).removeAttr('style');
1288 $(this).data("origStyle") != undefined ? this.attr("style", $(this).data("origStyle")) : $(this).removeAttr('style');
1289 $(this).unwrap().unwrap();
1290 if(slider.controls.el) slider.controls.el.remove();
1291 if(slider.controls.next) slider.controls.next.remove();
1292 if(slider.controls.prev) slider.controls.prev.remove();
1293 if(slider.pagerEl) slider.pagerEl.remove();
1294 $('.bx-caption', this).remove();
1295 if(slider.controls.autoEl) slider.controls.autoEl.remove();
1296 clearInterval(slider.interval);
1297 if(slider.settings.responsive) $(window).unbind('resize', resizeWindow);
1301 * Reload the slider (revert all DOM changes, and re-initialize)
1303 el.reloadSlider = function(settings){
1304 if (settings != undefined) options = settings;
1311 // returns the current jQuery object