bxslider.js 49 KB

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