jquery.bxslider.js 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  1. /**
  2. * BxSlider v4.1.1 - Fully loaded, responsive content slider
  3. * http://bxslider.com
  4. *
  5. * Copyright 2013, Steven Wanderski - http://stevenwanderski.com - http://bxcreative.com
  6. * Written while drinking Belgian ales and listening to jazz
  7. *
  8. * Released under the MIT license - http://opensource.org/licenses/MIT
  9. */
  10. ;(function($){
  11. var plugin = {};
  12. var defaults = {
  13. // GENERAL
  14. mode: 'horizontal',
  15. slideSelector: '',
  16. infiniteLoop: true,
  17. hideControlOnEnd: false,
  18. speed: 500,
  19. easing: null,
  20. slideMargin: 0,
  21. startSlide: 0,
  22. randomStart: false,
  23. captions: false,
  24. ticker: false,
  25. tickerHover: false,
  26. adaptiveHeight: false,
  27. adaptiveHeightSpeed: 500,
  28. video: false,
  29. useCSS: true,
  30. preloadImages: 'visible',
  31. responsive: true,
  32. // TOUCH
  33. touchEnabled: true,
  34. swipeThreshold: 50,
  35. oneToOneTouch: true,
  36. preventDefaultSwipeX: true,
  37. preventDefaultSwipeY: false,
  38. // PAGER
  39. pager: true,
  40. pagerType: 'full',
  41. pagerShortSeparator: ' / ',
  42. pagerSelector: null,
  43. buildPager: null,
  44. pagerCustom: null,
  45. // CONTROLS
  46. controls: true,
  47. nextText: 'Next',
  48. prevText: 'Prev',
  49. nextSelector: null,
  50. prevSelector: null,
  51. autoControls: false,
  52. startText: 'Start',
  53. stopText: 'Stop',
  54. autoControlsCombine: false,
  55. autoControlsSelector: null,
  56. // AUTO
  57. auto: false,
  58. pause: 4000,
  59. autoStart: true,
  60. autoDirection: 'next',
  61. autoHover: false,
  62. autoDelay: 0,
  63. // CAROUSEL
  64. minSlides: 1,
  65. maxSlides: 1,
  66. moveSlides: 0,
  67. slideWidth: 0,
  68. // CALLBACKS
  69. onSliderLoad: function() {},
  70. onSlideBefore: function() {},
  71. onSlideAfter: function() {},
  72. onSlideNext: function() {},
  73. onSlidePrev: function() {}
  74. }
  75. $.fn.bxSlider = function(options){
  76. if(this.length == 0) return this;
  77. // support mutltiple elements
  78. if(this.length > 1){
  79. this.each(function(){$(this).bxSlider(options)});
  80. return this;
  81. }
  82. // create a namespace to be used throughout the plugin
  83. var slider = {};
  84. // set a reference to our slider element
  85. var el = this;
  86. plugin.el = this;
  87. /**
  88. * Makes slideshow responsive
  89. */
  90. // first get the original window dimens (thanks alot IE)
  91. var windowWidth = $(window).width();
  92. var windowHeight = $(window).height();
  93. /**
  94. * ===================================================================================
  95. * = PRIVATE FUNCTIONS
  96. * ===================================================================================
  97. */
  98. /**
  99. * Initializes namespace settings to be used throughout plugin
  100. */
  101. var init = function(){
  102. // merge user-supplied options with the defaults
  103. slider.settings = $.extend({}, defaults, options);
  104. // parse slideWidth setting
  105. slider.settings.slideWidth = parseInt(slider.settings.slideWidth);
  106. // store the original children
  107. slider.children = el.children(slider.settings.slideSelector);
  108. // check if actual number of slides is less than minSlides / maxSlides
  109. if(slider.children.length < slider.settings.minSlides) slider.settings.minSlides = slider.children.length;
  110. if(slider.children.length < slider.settings.maxSlides) slider.settings.maxSlides = slider.children.length;
  111. // if random start, set the startSlide setting to random number
  112. if(slider.settings.randomStart) slider.settings.startSlide = Math.floor(Math.random() * slider.children.length);
  113. // store active slide information
  114. slider.active = { index: slider.settings.startSlide }
  115. // store if the slider is in carousel mode (displaying / moving multiple slides)
  116. slider.carousel = slider.settings.minSlides > 1 || slider.settings.maxSlides > 1;
  117. // if carousel, force preloadImages = 'all'
  118. if(slider.carousel) slider.settings.preloadImages = 'all';
  119. // calculate the min / max width thresholds based on min / max number of slides
  120. // used to setup and update carousel slides dimensions
  121. slider.minThreshold = (slider.settings.minSlides * slider.settings.slideWidth) + ((slider.settings.minSlides - 1) * slider.settings.slideMargin);
  122. slider.maxThreshold = (slider.settings.maxSlides * slider.settings.slideWidth) + ((slider.settings.maxSlides - 1) * slider.settings.slideMargin);
  123. // store the current state of the slider (if currently animating, working is true)
  124. slider.working = false;
  125. // initialize the controls object
  126. slider.controls = {};
  127. // initialize an auto interval
  128. slider.interval = null;
  129. // determine which property to use for transitions
  130. slider.animProp = slider.settings.mode == 'vertical' ? 'top' : 'left';
  131. // determine if hardware acceleration can be used
  132. slider.usingCSS = slider.settings.useCSS && slider.settings.mode != 'fade' && (function(){
  133. // create our test div element
  134. var div = document.createElement('div');
  135. // css transition properties
  136. var props = ['WebkitPerspective', 'MozPerspective', 'OPerspective', 'msPerspective'];
  137. // test for each property
  138. for(var i in props){
  139. if(div.style[props[i]] !== undefined){
  140. slider.cssPrefix = props[i].replace('Perspective', '').toLowerCase();
  141. slider.animProp = '-' + slider.cssPrefix + '-transform';
  142. return true;
  143. }
  144. }
  145. return false;
  146. }());
  147. // if vertical mode always make maxSlides and minSlides equal
  148. if(slider.settings.mode == 'vertical') slider.settings.maxSlides = slider.settings.minSlides;
  149. // save original style data
  150. el.data("origStyle", el.attr("style"));
  151. el.children(slider.settings.slideSelector).each(function() {
  152. $(this).data("origStyle", $(this).attr("style"));
  153. });
  154. // perform all DOM / CSS modifications
  155. setup();
  156. }
  157. /**
  158. * Performs all DOM and CSS modifications
  159. */
  160. var setup = function(){
  161. // wrap el in a wrapper
  162. el.wrap('<div class="bx-wrapper"><div class="bx-viewport"></div></div>');
  163. // store a namspace reference to .bx-viewport
  164. slider.viewport = el.parent();
  165. // add a loading div to display while images are loading
  166. slider.loader = $('<div class="bx-loading" />');
  167. slider.viewport.prepend(slider.loader);
  168. // set el to a massive width, to hold any needed slides
  169. // also strip any margin and padding from el
  170. el.css({
  171. width: slider.settings.mode == 'horizontal' ? (slider.children.length * 100 + 215) + '%' : 'auto',
  172. position: 'relative'
  173. });
  174. // if using CSS, add the easing property
  175. if(slider.usingCSS && slider.settings.easing){
  176. el.css('-' + slider.cssPrefix + '-transition-timing-function', slider.settings.easing);
  177. // if not using CSS and no easing value was supplied, use the default JS animation easing (swing)
  178. }else if(!slider.settings.easing){
  179. slider.settings.easing = 'swing';
  180. }
  181. var slidesShowing = getNumberSlidesShowing();
  182. // make modifications to the viewport (.bx-viewport)
  183. slider.viewport.css({
  184. width: '100%',
  185. overflow: 'hidden',
  186. position: 'relative'
  187. });
  188. slider.viewport.parent().css({
  189. maxWidth: getViewportMaxWidth()
  190. });
  191. // make modification to the wrapper (.bx-wrapper)
  192. if(!slider.settings.pager) {
  193. slider.viewport.parent().css({
  194. margin: '0 auto 0px'
  195. });
  196. }
  197. // apply css to all slider children
  198. slider.children.css({
  199. 'float': slider.settings.mode == 'horizontal' ? 'left' : 'none',
  200. listStyle: 'none',
  201. position: 'relative'
  202. });
  203. // apply the calculated width after the float is applied to prevent scrollbar interference
  204. slider.children.css('width', getSlideWidth());
  205. // if slideMargin is supplied, add the css
  206. if(slider.settings.mode == 'horizontal' && slider.settings.slideMargin > 0) slider.children.css('marginRight', slider.settings.slideMargin);
  207. if(slider.settings.mode == 'vertical' && slider.settings.slideMargin > 0) slider.children.css('marginBottom', slider.settings.slideMargin);
  208. // if "fade" mode, add positioning and z-index CSS
  209. if(slider.settings.mode == 'fade'){
  210. slider.children.css({
  211. position: 'absolute',
  212. zIndex: 0,
  213. display: 'none'
  214. });
  215. // prepare the z-index on the showing element
  216. slider.children.eq(slider.settings.startSlide).css({zIndex: 50, display: 'block'});
  217. }
  218. // create an element to contain all slider controls (pager, start / stop, etc)
  219. slider.controls.el = $('<div class="bx-controls" />');
  220. // if captions are requested, add them
  221. if(slider.settings.captions) appendCaptions();
  222. // check if startSlide is last slide
  223. slider.active.last = slider.settings.startSlide == getPagerQty() - 1;
  224. // if video is true, set up the fitVids plugin
  225. if(slider.settings.video) el.fitVids();
  226. // set the default preload selector (visible)
  227. var preloadSelector = slider.children.eq(slider.settings.startSlide);
  228. if (slider.settings.preloadImages == "all") preloadSelector = slider.children;
  229. // only check for control addition if not in "ticker" mode
  230. if(!slider.settings.ticker){
  231. // if pager is requested, add it
  232. if(slider.settings.pager) appendPager();
  233. // if controls are requested, add them
  234. if(slider.settings.controls) appendControls();
  235. // if auto is true, and auto controls are requested, add them
  236. if(slider.settings.auto && slider.settings.autoControls) appendControlsAuto();
  237. // if any control option is requested, add the controls wrapper
  238. if(slider.settings.controls || slider.settings.autoControls || slider.settings.pager) slider.viewport.after(slider.controls.el);
  239. // if ticker mode, do not allow a pager
  240. }else{
  241. slider.settings.pager = false;
  242. }
  243. // preload all images, then perform final DOM / CSS modifications that depend on images being loaded
  244. loadElements(preloadSelector, start);
  245. }
  246. var loadElements = function(selector, callback){
  247. var total = selector.find('img, iframe').length;
  248. if (total == 0){
  249. callback();
  250. return;
  251. }
  252. var count = 0;
  253. selector.find('img, iframe').each(function(){
  254. $(this).one('load', function() {
  255. if(++count == total) callback();
  256. }).each(function() {
  257. if(this.complete) $(this).load();
  258. });
  259. });
  260. }
  261. /**
  262. * Start the slider
  263. */
  264. var start = function(){
  265. // if infinite loop, prepare additional slides
  266. if(slider.settings.infiniteLoop && slider.settings.mode != 'fade' && !slider.settings.ticker){
  267. var slice = slider.settings.mode == 'vertical' ? slider.settings.minSlides : slider.settings.maxSlides;
  268. var sliceAppend = slider.children.slice(0, slice).clone().addClass('bx-clone');
  269. var slicePrepend = slider.children.slice(-slice).clone().addClass('bx-clone');
  270. el.append(sliceAppend).prepend(slicePrepend);
  271. }
  272. // remove the loading DOM element
  273. slider.loader.remove();
  274. // set the left / top position of "el"
  275. setSlidePosition();
  276. // if "vertical" mode, always use adaptiveHeight to prevent odd behavior
  277. if (slider.settings.mode == 'vertical') slider.settings.adaptiveHeight = true;
  278. // set the viewport height
  279. slider.viewport.height(getViewportHeight());
  280. // make sure everything is positioned just right (same as a window resize)
  281. el.redrawSlider();
  282. // onSliderLoad callback
  283. slider.settings.onSliderLoad(slider.active.index);
  284. // slider has been fully initialized
  285. slider.initialized = true;
  286. // bind the resize call to the window
  287. if (slider.settings.responsive) $(window).bind('resize', resizeWindow);
  288. // if auto is true, start the show
  289. if (slider.settings.auto && slider.settings.autoStart) initAuto();
  290. // if ticker is true, start the ticker
  291. if (slider.settings.ticker) initTicker();
  292. // if pager is requested, make the appropriate pager link active
  293. if (slider.settings.pager) updatePagerActive(slider.settings.startSlide);
  294. // check for any updates to the controls (like hideControlOnEnd updates)
  295. if (slider.settings.controls) updateDirectionControls();
  296. // if touchEnabled is true, setup the touch events
  297. if (slider.settings.touchEnabled && !slider.settings.ticker) initTouch();
  298. }
  299. /**
  300. * Returns the calculated height of the viewport, used to determine either adaptiveHeight or the maxHeight value
  301. */
  302. var getViewportHeight = function(){
  303. var height = 0;
  304. // first determine which children (slides) should be used in our height calculation
  305. var children = $();
  306. // if mode is not "vertical" and adaptiveHeight is false, include all children
  307. if(slider.settings.mode != 'vertical' && !slider.settings.adaptiveHeight){
  308. children = slider.children;
  309. }else{
  310. // if not carousel, return the single active child
  311. if(!slider.carousel){
  312. children = slider.children.eq(slider.active.index);
  313. // if carousel, return a slice of children
  314. }else{
  315. // get the individual slide index
  316. var currentIndex = slider.settings.moveSlides == 1 ? slider.active.index : slider.active.index * getMoveBy();
  317. // add the current slide to the children
  318. children = slider.children.eq(currentIndex);
  319. // cycle through the remaining "showing" slides
  320. for (i = 1; i <= slider.settings.maxSlides - 1; i++){
  321. // if looped back to the start
  322. if(currentIndex + i >= slider.children.length){
  323. children = children.add(slider.children.eq(i - 1));
  324. }else{
  325. children = children.add(slider.children.eq(currentIndex + i));
  326. }
  327. }
  328. }
  329. }
  330. // if "vertical" mode, calculate the sum of the heights of the children
  331. if(slider.settings.mode == 'vertical'){
  332. children.each(function(index) {
  333. height += $(this).outerHeight();
  334. });
  335. // add user-supplied margins
  336. if(slider.settings.slideMargin > 0){
  337. height += slider.settings.slideMargin * (slider.settings.minSlides - 1);
  338. }
  339. // if not "vertical" mode, calculate the max height of the children
  340. }else{
  341. height = Math.max.apply(Math, children.map(function(){
  342. return $(this).outerHeight(false);
  343. }).get());
  344. }
  345. return height;
  346. }
  347. /**
  348. * Returns the calculated width to be used for the outer wrapper / viewport
  349. */
  350. var getViewportMaxWidth = function(){
  351. var width = '100%';
  352. if(slider.settings.slideWidth > 0){
  353. if(slider.settings.mode == 'horizontal'){
  354. width = (slider.settings.maxSlides * slider.settings.slideWidth) + ((slider.settings.maxSlides - 1) * slider.settings.slideMargin);
  355. }else{
  356. width = slider.settings.slideWidth;
  357. }
  358. }
  359. return width;
  360. }
  361. /**
  362. * Returns the calculated width to be applied to each slide
  363. */
  364. var getSlideWidth = function(){
  365. // start with any user-supplied slide width
  366. var newElWidth = slider.settings.slideWidth;
  367. // get the current viewport width
  368. var wrapWidth = slider.viewport.width();
  369. // if slide width was not supplied, or is larger than the viewport use the viewport width
  370. if(slider.settings.slideWidth == 0 ||
  371. (slider.settings.slideWidth > wrapWidth && !slider.carousel) ||
  372. slider.settings.mode == 'vertical'){
  373. newElWidth = wrapWidth;
  374. // if carousel, use the thresholds to determine the width
  375. }else if(slider.settings.maxSlides > 1 && slider.settings.mode == 'horizontal'){
  376. if(wrapWidth > slider.maxThreshold){
  377. // newElWidth = (wrapWidth - (slider.settings.slideMargin * (slider.settings.maxSlides - 1))) / slider.settings.maxSlides;
  378. }else if(wrapWidth < slider.minThreshold){
  379. newElWidth = (wrapWidth - (slider.settings.slideMargin * (slider.settings.minSlides - 1))) / slider.settings.minSlides;
  380. }
  381. }
  382. return newElWidth;
  383. }
  384. /**
  385. * Returns the number of slides currently visible in the viewport (includes partially visible slides)
  386. */
  387. var getNumberSlidesShowing = function(){
  388. var slidesShowing = 1;
  389. if(slider.settings.mode == 'horizontal' && slider.settings.slideWidth > 0){
  390. // if viewport is smaller than minThreshold, return minSlides
  391. if(slider.viewport.width() < slider.minThreshold){
  392. slidesShowing = slider.settings.minSlides;
  393. // if viewport is larger than minThreshold, return maxSlides
  394. }else if(slider.viewport.width() > slider.maxThreshold){
  395. slidesShowing = slider.settings.maxSlides;
  396. // if viewport is between min / max thresholds, divide viewport width by first child width
  397. }else{
  398. var childWidth = slider.children.first().width();
  399. slidesShowing = Math.floor(slider.viewport.width() / childWidth);
  400. }
  401. // if "vertical" mode, slides showing will always be minSlides
  402. }else if(slider.settings.mode == 'vertical'){
  403. slidesShowing = slider.settings.minSlides;
  404. }
  405. return slidesShowing;
  406. }
  407. /**
  408. * Returns the number of pages (one full viewport of slides is one "page")
  409. */
  410. var getPagerQty = function(){
  411. var pagerQty = 0;
  412. // if moveSlides is specified by the user
  413. if(slider.settings.moveSlides > 0){
  414. if(slider.settings.infiniteLoop){
  415. pagerQty = slider.children.length / getMoveBy();
  416. }else{
  417. // use a while loop to determine pages
  418. var breakPoint = 0;
  419. var counter = 0
  420. // when breakpoint goes above children length, counter is the number of pages
  421. while (breakPoint < slider.children.length){
  422. ++pagerQty;
  423. breakPoint = counter + getNumberSlidesShowing();
  424. counter += slider.settings.moveSlides <= getNumberSlidesShowing() ? slider.settings.moveSlides : getNumberSlidesShowing();
  425. }
  426. }
  427. // if moveSlides is 0 (auto) divide children length by sides showing, then round up
  428. }else{
  429. pagerQty = Math.ceil(slider.children.length / getNumberSlidesShowing());
  430. }
  431. return pagerQty;
  432. }
  433. /**
  434. * Returns the number of indivual slides by which to shift the slider
  435. */
  436. var getMoveBy = function(){
  437. // if moveSlides was set by the user and moveSlides is less than number of slides showing
  438. if(slider.settings.moveSlides > 0 && slider.settings.moveSlides <= getNumberSlidesShowing()){
  439. return slider.settings.moveSlides;
  440. }
  441. // if moveSlides is 0 (auto)
  442. return getNumberSlidesShowing();
  443. }
  444. /**
  445. * Sets the slider's (el) left or top position
  446. */
  447. var setSlidePosition = function(){
  448. // if last slide, not infinite loop, and number of children is larger than specified maxSlides
  449. if(slider.children.length > slider.settings.maxSlides && slider.active.last && !slider.settings.infiniteLoop){
  450. if (slider.settings.mode == 'horizontal'){
  451. // get the last child's position
  452. var lastChild = slider.children.last();
  453. var position = lastChild.position();
  454. // set the left position
  455. setPositionProperty(-(position.left - (slider.viewport.width() - lastChild.width())), 'reset', 0);
  456. }else if(slider.settings.mode == 'vertical'){
  457. // get the last showing index's position
  458. var lastShowingIndex = slider.children.length - slider.settings.minSlides;
  459. var position = slider.children.eq(lastShowingIndex).position();
  460. // set the top position
  461. setPositionProperty(-position.top, 'reset', 0);
  462. }
  463. // if not last slide
  464. }else{
  465. // get the position of the first showing slide
  466. var position = slider.children.eq(slider.active.index * getMoveBy()).position();
  467. // check for last slide
  468. if (slider.active.index == getPagerQty() - 1) slider.active.last = true;
  469. // set the repective position
  470. if (position != undefined){
  471. if (slider.settings.mode == 'horizontal') setPositionProperty(-position.left, 'reset', 0);
  472. else if (slider.settings.mode == 'vertical') setPositionProperty(-position.top, 'reset', 0);
  473. }
  474. }
  475. }
  476. /**
  477. * Sets the el's animating property position (which in turn will sometimes animate el).
  478. * If using CSS, sets the transform property. If not using CSS, sets the top / left property.
  479. *
  480. * @param value (int)
  481. * - the animating property's value
  482. *
  483. * @param type (string) 'slider', 'reset', 'ticker'
  484. * - the type of instance for which the function is being
  485. *
  486. * @param duration (int)
  487. * - the amount of time (in ms) the transition should occupy
  488. *
  489. * @param params (array) optional
  490. * - an optional parameter containing any variables that need to be passed in
  491. */
  492. var setPositionProperty = function(value, type, duration, params){
  493. // use CSS transform
  494. if(slider.usingCSS){
  495. // determine the translate3d value
  496. var propValue = slider.settings.mode == 'vertical' ? 'translate3d(0, ' + value + 'px, 0)' : 'translate3d(' + value + 'px, 0, 0)';
  497. // add the CSS transition-duration
  498. el.css('-' + slider.cssPrefix + '-transition-duration', duration / 1000 + 's');
  499. if(type == 'slide'){
  500. // set the property value
  501. el.css(slider.animProp, propValue);
  502. // bind a callback method - executes when CSS transition completes
  503. el.bind('transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd', function(){
  504. // unbind the callback
  505. el.unbind('transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd');
  506. updateAfterSlideTransition();
  507. });
  508. }else if(type == 'reset'){
  509. el.css(slider.animProp, propValue);
  510. }else if(type == 'ticker'){
  511. // make the transition use 'linear'
  512. el.css('-' + slider.cssPrefix + '-transition-timing-function', 'linear');
  513. el.css(slider.animProp, propValue);
  514. // bind a callback method - executes when CSS transition completes
  515. el.bind('transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd', function(){
  516. // unbind the callback
  517. el.unbind('transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd');
  518. // reset the position
  519. setPositionProperty(params['resetValue'], 'reset', 0);
  520. // start the loop again
  521. tickerLoop();
  522. });
  523. }
  524. // use JS animate
  525. }else{
  526. var animateObj = {};
  527. animateObj[slider.animProp] = value;
  528. if(type == 'slide'){
  529. el.animate(animateObj, duration, slider.settings.easing, function(){
  530. updateAfterSlideTransition();
  531. });
  532. }else if(type == 'reset'){
  533. el.css(slider.animProp, value)
  534. }else if(type == 'ticker'){
  535. el.animate(animateObj, speed, 'linear', function(){
  536. setPositionProperty(params['resetValue'], 'reset', 0);
  537. // run the recursive loop after animation
  538. tickerLoop();
  539. });
  540. }
  541. }
  542. }
  543. /**
  544. * Populates the pager with proper amount of pages
  545. */
  546. var populatePager = function(){
  547. var pagerHtml = '';
  548. var pagerQty = getPagerQty();
  549. // loop through each pager item
  550. for(var i=0; i < pagerQty; i++){
  551. var linkContent = '';
  552. // if a buildPager function is supplied, use it to get pager link value, else use index + 1
  553. if(slider.settings.buildPager && $.isFunction(slider.settings.buildPager)){
  554. linkContent = slider.settings.buildPager(i);
  555. slider.pagerEl.addClass('bx-custom-pager');
  556. }else{
  557. linkContent = i + 1;
  558. slider.pagerEl.addClass('bx-default-pager');
  559. }
  560. // var linkContent = slider.settings.buildPager && $.isFunction(slider.settings.buildPager) ? slider.settings.buildPager(i) : i + 1;
  561. // add the markup to the string
  562. pagerHtml += '<div class="bx-pager-item"><a href="" data-slide-index="' + i + '" class="bx-pager-link">' + linkContent + '</a></div>';
  563. };
  564. // populate the pager element with pager links
  565. slider.pagerEl.html(pagerHtml);
  566. }
  567. /**
  568. * Appends the pager to the controls element
  569. */
  570. var appendPager = function(){
  571. if(!slider.settings.pagerCustom){
  572. // create the pager DOM element
  573. slider.pagerEl = $('<div class="bx-pager" />');
  574. // if a pager selector was supplied, populate it with the pager
  575. if(slider.settings.pagerSelector){
  576. $(slider.settings.pagerSelector).html(slider.pagerEl);
  577. // if no pager selector was supplied, add it after the wrapper
  578. }else{
  579. slider.controls.el.addClass('bx-has-pager').append(slider.pagerEl);
  580. }
  581. // populate the pager
  582. populatePager();
  583. }else{
  584. slider.pagerEl = $(slider.settings.pagerCustom);
  585. }
  586. // assign the pager click binding
  587. slider.pagerEl.delegate('a', 'click', clickPagerBind);
  588. }
  589. /**
  590. * Appends prev / next controls to the controls element
  591. */
  592. var appendControls = function(){
  593. slider.controls.next = $('<a class="bx-next" href="">' + slider.settings.nextText + '</a>');
  594. slider.controls.prev = $('<a class="bx-prev" href="">' + slider.settings.prevText + '</a>');
  595. // bind click actions to the controls
  596. slider.controls.next.bind('click', clickNextBind);
  597. slider.controls.prev.bind('click', clickPrevBind);
  598. // if nextSlector was supplied, populate it
  599. if(slider.settings.nextSelector){
  600. $(slider.settings.nextSelector).append(slider.controls.next);
  601. }
  602. // if prevSlector was supplied, populate it
  603. if(slider.settings.prevSelector){
  604. $(slider.settings.prevSelector).append(slider.controls.prev);
  605. }
  606. // if no custom selectors were supplied
  607. if(!slider.settings.nextSelector && !slider.settings.prevSelector){
  608. // add the controls to the DOM
  609. slider.controls.directionEl = $('<div class="bx-controls-direction" />');
  610. // add the control elements to the directionEl
  611. slider.controls.directionEl.append(slider.controls.prev).append(slider.controls.next);
  612. // slider.viewport.append(slider.controls.directionEl);
  613. slider.controls.el.addClass('bx-has-controls-direction').append(slider.controls.directionEl);
  614. }
  615. }
  616. /**
  617. * Appends start / stop auto controls to the controls element
  618. */
  619. var appendControlsAuto = function(){
  620. slider.controls.start = $('<div class="bx-controls-auto-item"><a class="bx-start" href="">' + slider.settings.startText + '</a></div>');
  621. slider.controls.stop = $('<div class="bx-controls-auto-item"><a class="bx-stop" href="">' + slider.settings.stopText + '</a></div>');
  622. // add the controls to the DOM
  623. slider.controls.autoEl = $('<div class="bx-controls-auto" />');
  624. // bind click actions to the controls
  625. slider.controls.autoEl.delegate('.bx-start', 'click', clickStartBind);
  626. slider.controls.autoEl.delegate('.bx-stop', 'click', clickStopBind);
  627. // if autoControlsCombine, insert only the "start" control
  628. if(slider.settings.autoControlsCombine){
  629. slider.controls.autoEl.append(slider.controls.start);
  630. // if autoControlsCombine is false, insert both controls
  631. }else{
  632. slider.controls.autoEl.append(slider.controls.start).append(slider.controls.stop);
  633. }
  634. // if auto controls selector was supplied, populate it with the controls
  635. if(slider.settings.autoControlsSelector){
  636. $(slider.settings.autoControlsSelector).html(slider.controls.autoEl);
  637. // if auto controls selector was not supplied, add it after the wrapper
  638. }else{
  639. slider.controls.el.addClass('bx-has-controls-auto').append(slider.controls.autoEl);
  640. }
  641. // update the auto controls
  642. updateAutoControls(slider.settings.autoStart ? 'stop' : 'start');
  643. }
  644. /**
  645. * Appends image captions to the DOM
  646. */
  647. var appendCaptions = function(){
  648. // cycle through each child
  649. slider.children.each(function(index){
  650. // get the image title attribute
  651. var title = $(this).find('img:first').attr('title');
  652. // append the caption
  653. if (title != undefined && ('' + title).length) {
  654. $(this).append('<div class="bx-caption"><span>' + title + '</span></div>');
  655. }
  656. });
  657. }
  658. /**
  659. * Click next binding
  660. *
  661. * @param e (event)
  662. * - DOM event object
  663. */
  664. var clickNextBind = function(e){
  665. // if auto show is running, stop it
  666. if (slider.settings.auto) el.stopAuto();
  667. el.goToNextSlide();
  668. e.preventDefault();
  669. }
  670. /**
  671. * Click prev binding
  672. *
  673. * @param e (event)
  674. * - DOM event object
  675. */
  676. var clickPrevBind = function(e){
  677. // if auto show is running, stop it
  678. if (slider.settings.auto) el.stopAuto();
  679. el.goToPrevSlide();
  680. e.preventDefault();
  681. }
  682. /**
  683. * Click start binding
  684. *
  685. * @param e (event)
  686. * - DOM event object
  687. */
  688. var clickStartBind = function(e){
  689. el.startAuto();
  690. e.preventDefault();
  691. }
  692. /**
  693. * Click stop binding
  694. *
  695. * @param e (event)
  696. * - DOM event object
  697. */
  698. var clickStopBind = function(e){
  699. el.stopAuto();
  700. e.preventDefault();
  701. }
  702. /**
  703. * Click pager binding
  704. *
  705. * @param e (event)
  706. * - DOM event object
  707. */
  708. var clickPagerBind = function(e){
  709. // if auto show is running, stop it
  710. if (slider.settings.auto) el.stopAuto();
  711. var pagerLink = $(e.currentTarget);
  712. var pagerIndex = parseInt(pagerLink.attr('data-slide-index'));
  713. // if clicked pager link is not active, continue with the goToSlide call
  714. if(pagerIndex != slider.active.index) el.goToSlide(pagerIndex);
  715. e.preventDefault();
  716. }
  717. /**
  718. * Updates the pager links with an active class
  719. *
  720. * @param slideIndex (int)
  721. * - index of slide to make active
  722. */
  723. var updatePagerActive = function(slideIndex){
  724. // if "short" pager type
  725. var len = slider.children.length; // nb of children
  726. if(slider.settings.pagerType == 'short'){
  727. if(slider.settings.maxSlides > 1) {
  728. len = Math.ceil(slider.children.length/slider.settings.maxSlides);
  729. }
  730. slider.pagerEl.html( (slideIndex + 1) + slider.settings.pagerShortSeparator + len);
  731. return;
  732. }
  733. // remove all pager active classes
  734. slider.pagerEl.find('a').removeClass('active');
  735. // apply the active class for all pagers
  736. slider.pagerEl.each(function(i, el) { $(el).find('a').eq(slideIndex).addClass('active'); });
  737. }
  738. /**
  739. * Performs needed actions after a slide transition
  740. */
  741. var updateAfterSlideTransition = function(){
  742. // if infinte loop is true
  743. if(slider.settings.infiniteLoop){
  744. var position = '';
  745. // first slide
  746. if(slider.active.index == 0){
  747. // set the new position
  748. position = slider.children.eq(0).position();
  749. // carousel, last slide
  750. }else if(slider.active.index == getPagerQty() - 1 && slider.carousel){
  751. position = slider.children.eq((getPagerQty() - 1) * getMoveBy()).position();
  752. // last slide
  753. }else if(slider.active.index == slider.children.length - 1){
  754. position = slider.children.eq(slider.children.length - 1).position();
  755. }
  756. if (slider.settings.mode == 'horizontal') { setPositionProperty(-position.left, 'reset', 0);; }
  757. else if (slider.settings.mode == 'vertical') { setPositionProperty(-position.top, 'reset', 0);; }
  758. }
  759. // declare that the transition is complete
  760. slider.working = false;
  761. // onSlideAfter callback
  762. slider.settings.onSlideAfter(slider.children.eq(slider.active.index), slider.oldIndex, slider.active.index);
  763. }
  764. /**
  765. * Updates the auto controls state (either active, or combined switch)
  766. *
  767. * @param state (string) "start", "stop"
  768. * - the new state of the auto show
  769. */
  770. var updateAutoControls = function(state){
  771. // if autoControlsCombine is true, replace the current control with the new state
  772. if(slider.settings.autoControlsCombine){
  773. slider.controls.autoEl.html(slider.controls[state]);
  774. // if autoControlsCombine is false, apply the "active" class to the appropriate control
  775. }else{
  776. slider.controls.autoEl.find('a').removeClass('active');
  777. slider.controls.autoEl.find('a:not(.bx-' + state + ')').addClass('active');
  778. }
  779. }
  780. /**
  781. * Updates the direction controls (checks if either should be hidden)
  782. */
  783. var updateDirectionControls = function(){
  784. if(getPagerQty() == 1){
  785. slider.controls.prev.addClass('disabled');
  786. slider.controls.next.addClass('disabled');
  787. }else if(!slider.settings.infiniteLoop && slider.settings.hideControlOnEnd){
  788. // if first slide
  789. if (slider.active.index == 0){
  790. slider.controls.prev.addClass('disabled');
  791. slider.controls.next.removeClass('disabled');
  792. // if last slide
  793. }else if(slider.active.index == getPagerQty() - 1){
  794. slider.controls.next.addClass('disabled');
  795. slider.controls.prev.removeClass('disabled');
  796. // if any slide in the middle
  797. }else{
  798. slider.controls.prev.removeClass('disabled');
  799. slider.controls.next.removeClass('disabled');
  800. }
  801. }
  802. }
  803. /**
  804. * Initialzes the auto process
  805. */
  806. var initAuto = function(){
  807. // if autoDelay was supplied, launch the auto show using a setTimeout() call
  808. if(slider.settings.autoDelay > 0){
  809. var timeout = setTimeout(el.startAuto, slider.settings.autoDelay);
  810. // if autoDelay was not supplied, start the auto show normally
  811. }else{
  812. el.startAuto();
  813. }
  814. // if autoHover is requested
  815. if(slider.settings.autoHover){
  816. // on el hover
  817. el.hover(function(){
  818. // if the auto show is currently playing (has an active interval)
  819. if(slider.interval){
  820. // stop the auto show and pass true agument which will prevent control update
  821. el.stopAuto(true);
  822. // create a new autoPaused value which will be used by the relative "mouseout" event
  823. slider.autoPaused = true;
  824. }
  825. }, function(){
  826. // if the autoPaused value was created be the prior "mouseover" event
  827. if(slider.autoPaused){
  828. // start the auto show and pass true agument which will prevent control update
  829. el.startAuto(true);
  830. // reset the autoPaused value
  831. slider.autoPaused = null;
  832. }
  833. });
  834. }
  835. }
  836. /**
  837. * Initialzes the ticker process
  838. */
  839. var initTicker = function(){
  840. var startPosition = 0;
  841. // if autoDirection is "next", append a clone of the entire slider
  842. if(slider.settings.autoDirection == 'next'){
  843. el.append(slider.children.clone().addClass('bx-clone'));
  844. // if autoDirection is "prev", prepend a clone of the entire slider, and set the left position
  845. }else{
  846. el.prepend(slider.children.clone().addClass('bx-clone'));
  847. var position = slider.children.first().position();
  848. startPosition = slider.settings.mode == 'horizontal' ? -position.left : -position.top;
  849. }
  850. setPositionProperty(startPosition, 'reset', 0);
  851. // do not allow controls in ticker mode
  852. slider.settings.pager = false;
  853. slider.settings.controls = false;
  854. slider.settings.autoControls = false;
  855. // if autoHover is requested
  856. if(slider.settings.tickerHover && !slider.usingCSS){
  857. // on el hover
  858. slider.viewport.hover(function(){
  859. el.stop();
  860. }, function(){
  861. // calculate the total width of children (used to calculate the speed ratio)
  862. var totalDimens = 0;
  863. slider.children.each(function(index){
  864. totalDimens += slider.settings.mode == 'horizontal' ? $(this).outerWidth(true) : $(this).outerHeight(true);
  865. });
  866. // calculate the speed ratio (used to determine the new speed to finish the paused animation)
  867. var ratio = slider.settings.speed / totalDimens;
  868. // determine which property to use
  869. var property = slider.settings.mode == 'horizontal' ? 'left' : 'top';
  870. // calculate the new speed
  871. var newSpeed = ratio * (totalDimens - (Math.abs(parseInt(el.css(property)))));
  872. tickerLoop(newSpeed);
  873. });
  874. }
  875. // start the ticker loop
  876. tickerLoop();
  877. }
  878. /**
  879. * Runs a continuous loop, news ticker-style
  880. */
  881. var tickerLoop = function(resumeSpeed){
  882. speed = resumeSpeed ? resumeSpeed : slider.settings.speed;
  883. var position = {left: 0, top: 0};
  884. var reset = {left: 0, top: 0};
  885. // if "next" animate left position to last child, then reset left to 0
  886. if(slider.settings.autoDirection == 'next'){
  887. position = el.find('.bx-clone').first().position();
  888. // if "prev" animate left position to 0, then reset left to first non-clone child
  889. }else{
  890. reset = slider.children.first().position();
  891. }
  892. var animateProperty = slider.settings.mode == 'horizontal' ? -position.left : -position.top;
  893. var resetValue = slider.settings.mode == 'horizontal' ? -reset.left : -reset.top;
  894. var params = {resetValue: resetValue};
  895. setPositionProperty(animateProperty, 'ticker', speed, params);
  896. }
  897. /**
  898. * Initializes touch events
  899. */
  900. var initTouch = function(){
  901. // initialize object to contain all touch values
  902. slider.touch = {
  903. start: {x: 0, y: 0},
  904. end: {x: 0, y: 0}
  905. }
  906. slider.viewport.bind('touchstart', onTouchStart);
  907. }
  908. /**
  909. * Event handler for "touchstart"
  910. *
  911. * @param e (event)
  912. * - DOM event object
  913. */
  914. var onTouchStart = function(e){
  915. if(slider.working){
  916. e.preventDefault();
  917. }else{
  918. // record the original position when touch starts
  919. slider.touch.originalPos = el.position();
  920. var orig = e.originalEvent;
  921. // record the starting touch x, y coordinates
  922. slider.touch.start.x = orig.changedTouches[0].pageX;
  923. slider.touch.start.y = orig.changedTouches[0].pageY;
  924. // bind a "touchmove" event to the viewport
  925. slider.viewport.bind('touchmove', onTouchMove);
  926. // bind a "touchend" event to the viewport
  927. slider.viewport.bind('touchend', onTouchEnd);
  928. }
  929. }
  930. /**
  931. * Event handler for "touchmove"
  932. *
  933. * @param e (event)
  934. * - DOM event object
  935. */
  936. var onTouchMove = function(e){
  937. var orig = e.originalEvent;
  938. // if scrolling on y axis, do not prevent default
  939. var xMovement = Math.abs(orig.changedTouches[0].pageX - slider.touch.start.x);
  940. var yMovement = Math.abs(orig.changedTouches[0].pageY - slider.touch.start.y);
  941. // x axis swipe
  942. if((xMovement * 3) > yMovement && slider.settings.preventDefaultSwipeX){
  943. e.preventDefault();
  944. // y axis swipe
  945. }else if((yMovement * 3) > xMovement && slider.settings.preventDefaultSwipeY){
  946. e.preventDefault();
  947. }
  948. if(slider.settings.mode != 'fade' && slider.settings.oneToOneTouch){
  949. var value = 0;
  950. // if horizontal, drag along x axis
  951. if(slider.settings.mode == 'horizontal'){
  952. var change = orig.changedTouches[0].pageX - slider.touch.start.x;
  953. value = slider.touch.originalPos.left + change;
  954. // if vertical, drag along y axis
  955. }else{
  956. var change = orig.changedTouches[0].pageY - slider.touch.start.y;
  957. value = slider.touch.originalPos.top + change;
  958. }
  959. setPositionProperty(value, 'reset', 0);
  960. }
  961. }
  962. /**
  963. * Event handler for "touchend"
  964. *
  965. * @param e (event)
  966. * - DOM event object
  967. */
  968. var onTouchEnd = function(e){
  969. slider.viewport.unbind('touchmove', onTouchMove);
  970. var orig = e.originalEvent;
  971. var value = 0;
  972. // record end x, y positions
  973. slider.touch.end.x = orig.changedTouches[0].pageX;
  974. slider.touch.end.y = orig.changedTouches[0].pageY;
  975. // if fade mode, check if absolute x distance clears the threshold
  976. if(slider.settings.mode == 'fade'){
  977. var distance = Math.abs(slider.touch.start.x - slider.touch.end.x);
  978. if(distance >= slider.settings.swipeThreshold){
  979. slider.touch.start.x > slider.touch.end.x ? el.goToNextSlide() : el.goToPrevSlide();
  980. el.stopAuto();
  981. }
  982. // not fade mode
  983. }else{
  984. var distance = 0;
  985. // calculate distance and el's animate property
  986. if(slider.settings.mode == 'horizontal'){
  987. distance = slider.touch.end.x - slider.touch.start.x;
  988. value = slider.touch.originalPos.left;
  989. }else{
  990. distance = slider.touch.end.y - slider.touch.start.y;
  991. value = slider.touch.originalPos.top;
  992. }
  993. // if not infinite loop and first / last slide, do not attempt a slide transition
  994. if(!slider.settings.infiniteLoop && ((slider.active.index == 0 && distance > 0) || (slider.active.last && distance < 0))){
  995. setPositionProperty(value, 'reset', 200);
  996. }else{
  997. // check if distance clears threshold
  998. if(Math.abs(distance) >= slider.settings.swipeThreshold){
  999. distance < 0 ? el.goToNextSlide() : el.goToPrevSlide();
  1000. el.stopAuto();
  1001. }else{
  1002. // el.animate(property, 200);
  1003. setPositionProperty(value, 'reset', 200);
  1004. }
  1005. }
  1006. }
  1007. slider.viewport.unbind('touchend', onTouchEnd);
  1008. }
  1009. /**
  1010. * Window resize event callback
  1011. */
  1012. var resizeWindow = function(e){
  1013. // get the new window dimens (again, thank you IE)
  1014. var windowWidthNew = $(window).width();
  1015. var windowHeightNew = $(window).height();
  1016. // make sure that it is a true window resize
  1017. // *we must check this because our dinosaur friend IE fires a window resize event when certain DOM elements
  1018. // are resized. Can you just die already?*
  1019. if(windowWidth != windowWidthNew || windowHeight != windowHeightNew){
  1020. // set the new window dimens
  1021. windowWidth = windowWidthNew;
  1022. windowHeight = windowHeightNew;
  1023. // update all dynamic elements
  1024. el.redrawSlider();
  1025. }
  1026. }
  1027. /**
  1028. * ===================================================================================
  1029. * = PUBLIC FUNCTIONS
  1030. * ===================================================================================
  1031. */
  1032. /**
  1033. * Performs slide transition to the specified slide
  1034. *
  1035. * @param slideIndex (int)
  1036. * - the destination slide's index (zero-based)
  1037. *
  1038. * @param direction (string)
  1039. * - INTERNAL USE ONLY - the direction of travel ("prev" / "next")
  1040. */
  1041. el.goToSlide = function(slideIndex, direction){
  1042. // if plugin is currently in motion, ignore request
  1043. if(slider.working || slider.active.index == slideIndex) return;
  1044. // declare that plugin is in motion
  1045. slider.working = true;
  1046. // store the old index
  1047. slider.oldIndex = slider.active.index;
  1048. // if slideIndex is less than zero, set active index to last child (this happens during infinite loop)
  1049. if(slideIndex < 0){
  1050. slider.active.index = getPagerQty() - 1;
  1051. // if slideIndex is greater than children length, set active index to 0 (this happens during infinite loop)
  1052. }else if(slideIndex >= getPagerQty()){
  1053. slider.active.index = 0;
  1054. // set active index to requested slide
  1055. }else{
  1056. slider.active.index = slideIndex;
  1057. }
  1058. // onSlideBefore, onSlideNext, onSlidePrev callbacks
  1059. slider.settings.onSlideBefore(slider.children.eq(slider.active.index), slider.oldIndex, slider.active.index);
  1060. if(direction == 'next'){
  1061. slider.settings.onSlideNext(slider.children.eq(slider.active.index), slider.oldIndex, slider.active.index);
  1062. }else if(direction == 'prev'){
  1063. slider.settings.onSlidePrev(slider.children.eq(slider.active.index), slider.oldIndex, slider.active.index);
  1064. }
  1065. // check if last slide
  1066. slider.active.last = slider.active.index >= getPagerQty() - 1;
  1067. // update the pager with active class
  1068. if(slider.settings.pager) updatePagerActive(slider.active.index);
  1069. // // check for direction control update
  1070. if(slider.settings.controls) updateDirectionControls();
  1071. // if slider is set to mode: "fade"
  1072. if(slider.settings.mode == 'fade'){
  1073. // if adaptiveHeight is true and next height is different from current height, animate to the new height
  1074. if(slider.settings.adaptiveHeight && slider.viewport.height() != getViewportHeight()){
  1075. slider.viewport.animate({height: getViewportHeight()}, slider.settings.adaptiveHeightSpeed);
  1076. }
  1077. // fade out the visible child and reset its z-index value
  1078. slider.children.filter(':visible').fadeOut(slider.settings.speed).css({zIndex: 0});
  1079. // fade in the newly requested slide
  1080. slider.children.eq(slider.active.index).css('zIndex', 51).fadeIn(slider.settings.speed, function(){
  1081. $(this).css('zIndex', 50);
  1082. updateAfterSlideTransition();
  1083. });
  1084. // slider mode is not "fade"
  1085. }else{
  1086. // if adaptiveHeight is true and next height is different from current height, animate to the new height
  1087. if(slider.settings.adaptiveHeight && slider.viewport.height() != getViewportHeight()){
  1088. slider.viewport.animate({height: getViewportHeight()}, slider.settings.adaptiveHeightSpeed);
  1089. }
  1090. var moveBy = 0;
  1091. var position = {left: 0, top: 0};
  1092. // if carousel and not infinite loop
  1093. if(!slider.settings.infiniteLoop && slider.carousel && slider.active.last){
  1094. if(slider.settings.mode == 'horizontal'){
  1095. // get the last child position
  1096. var lastChild = slider.children.eq(slider.children.length - 1);
  1097. position = lastChild.position();
  1098. // calculate the position of the last slide
  1099. moveBy = slider.viewport.width() - lastChild.outerWidth();
  1100. }else{
  1101. // get last showing index position
  1102. var lastShowingIndex = slider.children.length - slider.settings.minSlides;
  1103. position = slider.children.eq(lastShowingIndex).position();
  1104. }
  1105. // horizontal carousel, going previous while on first slide (infiniteLoop mode)
  1106. }else if(slider.carousel && slider.active.last && direction == 'prev'){
  1107. // get the last child position
  1108. var eq = slider.settings.moveSlides == 1 ? slider.settings.maxSlides - getMoveBy() : ((getPagerQty() - 1) * getMoveBy()) - (slider.children.length - slider.settings.maxSlides);
  1109. var lastChild = el.children('.bx-clone').eq(eq);
  1110. position = lastChild.position();
  1111. // if infinite loop and "Next" is clicked on the last slide
  1112. }else if(direction == 'next' && slider.active.index == 0){
  1113. // get the last clone position
  1114. position = el.find('> .bx-clone').eq(slider.settings.maxSlides).position();
  1115. slider.active.last = false;
  1116. // normal non-zero requests
  1117. }else if(slideIndex >= 0){
  1118. var requestEl = slideIndex * getMoveBy();
  1119. position = slider.children.eq(requestEl).position();
  1120. }
  1121. /* If the position doesn't exist
  1122. * (e.g. if you destroy the slider on a next click),
  1123. * it doesn't throw an error.
  1124. */
  1125. if ("undefined" !== typeof(position)) {
  1126. var value = slider.settings.mode == 'horizontal' ? -(position.left - moveBy) : -position.top;
  1127. // plugin values to be animated
  1128. setPositionProperty(value, 'slide', slider.settings.speed);
  1129. }
  1130. }
  1131. }
  1132. /**
  1133. * Transitions to the next slide in the show
  1134. */
  1135. el.goToNextSlide = function(){
  1136. // if infiniteLoop is false and last page is showing, disregard call
  1137. if (!slider.settings.infiniteLoop && slider.active.last) return;
  1138. var pagerIndex = parseInt(slider.active.index) + 1;
  1139. el.goToSlide(pagerIndex, 'next');
  1140. }
  1141. /**
  1142. * Transitions to the prev slide in the show
  1143. */
  1144. el.goToPrevSlide = function(){
  1145. // if infiniteLoop is false and last page is showing, disregard call
  1146. if (!slider.settings.infiniteLoop && slider.active.index == 0) return;
  1147. var pagerIndex = parseInt(slider.active.index) - 1;
  1148. el.goToSlide(pagerIndex, 'prev');
  1149. }
  1150. /**
  1151. * Starts the auto show
  1152. *
  1153. * @param preventControlUpdate (boolean)
  1154. * - if true, auto controls state will not be updated
  1155. */
  1156. el.startAuto = function(preventControlUpdate){
  1157. // if an interval already exists, disregard call
  1158. if(slider.interval) return;
  1159. // create an interval
  1160. slider.interval = setInterval(function(){
  1161. slider.settings.autoDirection == 'next' ? el.goToNextSlide() : el.goToPrevSlide();
  1162. }, slider.settings.pause);
  1163. // if auto controls are displayed and preventControlUpdate is not true
  1164. if (slider.settings.autoControls && preventControlUpdate != true) updateAutoControls('stop');
  1165. }
  1166. /**
  1167. * Stops the auto show
  1168. *
  1169. * @param preventControlUpdate (boolean)
  1170. * - if true, auto controls state will not be updated
  1171. */
  1172. el.stopAuto = function(preventControlUpdate){
  1173. // if no interval exists, disregard call
  1174. if(!slider.interval) return;
  1175. // clear the interval
  1176. clearInterval(slider.interval);
  1177. slider.interval = null;
  1178. // if auto controls are displayed and preventControlUpdate is not true
  1179. if (slider.settings.autoControls && preventControlUpdate != true) updateAutoControls('start');
  1180. }
  1181. /**
  1182. * Returns current slide index (zero-based)
  1183. */
  1184. el.getCurrentSlide = function(){
  1185. return slider.active.index;
  1186. }
  1187. /**
  1188. * Returns number of slides in show
  1189. */
  1190. el.getSlideCount = function(){
  1191. return slider.children.length;
  1192. }
  1193. /**
  1194. * Update all dynamic slider elements
  1195. */
  1196. el.redrawSlider = function(){
  1197. // resize all children in ratio to new screen size
  1198. slider.children.add(el.find('.bx-clone')).outerWidth(getSlideWidth());
  1199. // adjust the height
  1200. slider.viewport.css('height', getViewportHeight());
  1201. // update the slide position
  1202. if(!slider.settings.ticker) setSlidePosition();
  1203. // if active.last was true before the screen resize, we want
  1204. // to keep it last no matter what screen size we end on
  1205. if (slider.active.last) slider.active.index = getPagerQty() - 1;
  1206. // if the active index (page) no longer exists due to the resize, simply set the index as last
  1207. if (slider.active.index >= getPagerQty()) slider.active.last = true;
  1208. // if a pager is being displayed and a custom pager is not being used, update it
  1209. if(slider.settings.pager && !slider.settings.pagerCustom){
  1210. populatePager();
  1211. updatePagerActive(slider.active.index);
  1212. }
  1213. }
  1214. /**
  1215. * Destroy the current instance of the slider (revert everything back to original state)
  1216. */
  1217. el.destroySlider = function(){
  1218. // don't do anything if slider has already been destroyed
  1219. if(!slider.initialized) return;
  1220. slider.initialized = false;
  1221. $('.bx-clone', this).remove();
  1222. slider.children.each(function() {
  1223. $(this).data("origStyle") != undefined ? $(this).attr("style", $(this).data("origStyle")) : $(this).removeAttr('style');
  1224. });
  1225. $(this).data("origStyle") != undefined ? this.attr("style", $(this).data("origStyle")) : $(this).removeAttr('style');
  1226. $(this).unwrap().unwrap();
  1227. if(slider.controls.el) slider.controls.el.remove();
  1228. if(slider.controls.next) slider.controls.next.remove();
  1229. if(slider.controls.prev) slider.controls.prev.remove();
  1230. if(slider.pagerEl) slider.pagerEl.remove();
  1231. $('.bx-caption', this).remove();
  1232. if(slider.controls.autoEl) slider.controls.autoEl.remove();
  1233. clearInterval(slider.interval);
  1234. if(slider.settings.responsive) $(window).unbind('resize', resizeWindow);
  1235. }
  1236. /**
  1237. * Reload the slider (revert all DOM changes, and re-initialize)
  1238. */
  1239. el.reloadSlider = function(settings){
  1240. if (settings != undefined) options = settings;
  1241. el.destroySlider();
  1242. init();
  1243. }
  1244. init();
  1245. // returns the current jQuery object
  1246. return this;
  1247. }
  1248. })(jQuery);