lxslider.js 50 KB

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