lightslider.js 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. (function ($, undefined) {
  2. 'use strict';
  3. var defaults = {
  4. item: 3,
  5. autoWidth: false,
  6. slideMove: 1,
  7. slideMargin: 10,
  8. addClass: '',
  9. mode: 'slide',
  10. useCSS: true,
  11. cssEasing: 'ease', //'cubic-bezier(0.25, 0, 0.25, 1)',
  12. easing: 'linear', //'for jquery animation',//
  13. speed: 400, //ms'
  14. auto: false,
  15. pauseOnHover: false,
  16. loop: false,
  17. slideEndAnimation: true,
  18. pause: 2000,
  19. keyPress: false,
  20. controls: true,
  21. prevHtml: '',
  22. nextHtml: '',
  23. rtl: false,
  24. adaptiveHeight: false,
  25. vertical: false,
  26. verticalHeight: 500,
  27. vThumbWidth: 100,
  28. thumbItem: 10,
  29. pager: true,
  30. gallery: false,
  31. galleryMargin: 5,
  32. thumbMargin: 5,
  33. currentPagerPosition: 'middle',
  34. enableTouch: true,
  35. enableDrag: true,
  36. freeMove: true,
  37. swipeThreshold: 40,
  38. responsive: [],
  39. /* jshint ignore:start */
  40. onBeforeStart: function ($el) {},
  41. onSliderLoad: function ($el) {},
  42. onBeforeSlide: function ($el, scene) {},
  43. onAfterSlide: function ($el, scene) {},
  44. onBeforeNextSlide: function ($el, scene) {},
  45. onBeforePrevSlide: function ($el, scene) {}
  46. /* jshint ignore:end */
  47. };
  48. $.fn.lightSlider = function (options) {
  49. if (this.length === 0) {
  50. return this;
  51. }
  52. if (this.length > 1) {
  53. this.each(function () {
  54. $(this).lightSlider(options);
  55. });
  56. return this;
  57. }
  58. var plugin = {},
  59. settings = $.extend(true, {}, defaults, options),
  60. settingsTemp = {},
  61. $el = this;
  62. plugin.$el = this;
  63. if (settings.mode === 'fade') {
  64. settings.vertical = false;
  65. }
  66. var $children = $el.children(),
  67. windowW = $(window).width(),
  68. breakpoint = null,
  69. resposiveObj = null,
  70. length = 0,
  71. w = 0,
  72. on = false,
  73. elSize = 0,
  74. $slide = '',
  75. scene = 0,
  76. property = (settings.vertical === true) ? 'height' : 'width',
  77. gutter = (settings.vertical === true) ? 'margin-bottom' : 'margin-right',
  78. slideValue = 0,
  79. pagerWidth = 0,
  80. slideWidth = 0,
  81. thumbWidth = 0,
  82. interval = null,
  83. isTouch = ('ontouchstart' in document.documentElement);
  84. var refresh = {};
  85. refresh.chbreakpoint = function () {
  86. windowW = $(window).width();
  87. if (settings.responsive.length) {
  88. var item;
  89. if (settings.autoWidth === false) {
  90. item = settings.item;
  91. }
  92. if (windowW < settings.responsive[0].breakpoint) {
  93. for (var i = 0; i < settings.responsive.length; i++) {
  94. if (windowW < settings.responsive[i].breakpoint) {
  95. breakpoint = settings.responsive[i].breakpoint;
  96. resposiveObj = settings.responsive[i];
  97. }
  98. }
  99. }
  100. if (typeof resposiveObj !== 'undefined' && resposiveObj !== null) {
  101. for (var j in resposiveObj.settings) {
  102. if (resposiveObj.settings.hasOwnProperty(j)) {
  103. if (typeof settingsTemp[j] === 'undefined' || settingsTemp[j] === null) {
  104. settingsTemp[j] = settings[j];
  105. }
  106. settings[j] = resposiveObj.settings[j];
  107. }
  108. }
  109. }
  110. if (!$.isEmptyObject(settingsTemp) && windowW > settings.responsive[0].breakpoint) {
  111. for (var k in settingsTemp) {
  112. if (settingsTemp.hasOwnProperty(k)) {
  113. settings[k] = settingsTemp[k];
  114. }
  115. }
  116. }
  117. if (settings.autoWidth === false) {
  118. if (slideValue > 0 && slideWidth > 0) {
  119. if (item !== settings.item) {
  120. scene = Math.round(slideValue / ((slideWidth + settings.slideMargin) * settings.slideMove));
  121. }
  122. }
  123. }
  124. }
  125. };
  126. refresh.calSW = function () {
  127. if (settings.autoWidth === false) {
  128. slideWidth = (elSize - ((settings.item * (settings.slideMargin)) - settings.slideMargin)) / settings.item;
  129. }
  130. };
  131. refresh.calWidth = function (cln) {
  132. var ln = cln === true ? $slide.find('.lslide').length : $children.length;
  133. if (settings.autoWidth === false) {
  134. w = ln * (slideWidth + settings.slideMargin);
  135. } else {
  136. w = 0;
  137. for (var i = 0; i < ln; i++) {
  138. w += (parseInt($children.eq(i).width()) + settings.slideMargin);
  139. }
  140. }
  141. return w;
  142. };
  143. plugin = {
  144. doCss: function () {
  145. var support = function () {
  146. var transition = ['transition', 'MozTransition', 'WebkitTransition', 'OTransition', 'msTransition', 'KhtmlTransition'];
  147. var root = document.documentElement;
  148. for (var i = 0; i < transition.length; i++) {
  149. if (transition[i] in root.style) {
  150. return true;
  151. }
  152. }
  153. };
  154. if (settings.useCSS && support()) {
  155. return true;
  156. }
  157. return false;
  158. },
  159. keyPress: function () {
  160. if (settings.keyPress) {
  161. $(document).on('keyup.lightslider', function (e) {
  162. if (!$(':focus').is('input, textarea')) {
  163. if (e.preventDefault) {
  164. e.preventDefault();
  165. } else {
  166. e.returnValue = false;
  167. }
  168. if (e.keyCode === 37) {
  169. $el.goToPrevSlide();
  170. } else if (e.keyCode === 39) {
  171. $el.goToNextSlide();
  172. }
  173. }
  174. });
  175. }
  176. },
  177. controls: function () {
  178. if (settings.controls) {
  179. $el.after('<div class="lSAction"><a class="lSPrev">' + settings.prevHtml + '</a><a class="lSNext">' + settings.nextHtml + '</a></div>');
  180. if (!settings.autoWidth) {
  181. if (length <= settings.item) {
  182. $slide.find('.lSAction').hide();
  183. }
  184. } else {
  185. if (refresh.calWidth(false) < elSize) {
  186. $slide.find('.lSAction').hide();
  187. }
  188. }
  189. $slide.find('.lSAction a').on('click', function (e) {
  190. if (e.preventDefault) {
  191. e.preventDefault();
  192. } else {
  193. e.returnValue = false;
  194. }
  195. if ($(this).attr('class') === 'lSPrev') {
  196. $el.goToPrevSlide();
  197. } else {
  198. $el.goToNextSlide();
  199. }
  200. return false;
  201. });
  202. }
  203. },
  204. initialStyle: function () {
  205. var $this = this;
  206. if (settings.mode === 'fade') {
  207. settings.autoWidth = false;
  208. settings.slideEndAnimation = false;
  209. }
  210. if (settings.auto) {
  211. settings.slideEndAnimation = false;
  212. }
  213. if (settings.autoWidth) {
  214. settings.slideMove = 1;
  215. settings.item = 1;
  216. }
  217. if (settings.loop) {
  218. settings.slideMove = 1;
  219. settings.freeMove = false;
  220. }
  221. settings.onBeforeStart.call(this, $el);
  222. refresh.chbreakpoint();
  223. $el.addClass('lightSlider').wrap('<div class="lSSlideOuter ' + settings.addClass + '"><div class="lSSlideWrapper"></div></div>');
  224. $slide = $el.parent('.lSSlideWrapper');
  225. if (settings.rtl === true) {
  226. $slide.parent().addClass('lSrtl');
  227. }
  228. if (settings.vertical) {
  229. $slide.parent().addClass('vertical');
  230. elSize = settings.verticalHeight;
  231. $slide.css('height', elSize + 'px');
  232. } else {
  233. elSize = $el.outerWidth();
  234. }
  235. $children.addClass('lslide');
  236. if (settings.loop === true && settings.mode === 'slide') {
  237. refresh.calSW();
  238. refresh.clone = function () {
  239. if (refresh.calWidth(true) > elSize) {
  240. /**/
  241. var tWr = 0,
  242. tI = 0;
  243. for (var k = 0; k < $children.length; k++) {
  244. tWr += (parseInt($el.find('.lslide').eq(k).width()) + settings.slideMargin);
  245. tI++;
  246. if (tWr >= (elSize + settings.slideMargin)) {
  247. break;
  248. }
  249. }
  250. var tItem = settings.autoWidth === true ? tI : settings.item;
  251. /**/
  252. if (tItem < $el.find('.clone.left').length) {
  253. for (var i = 0; i < $el.find('.clone.left').length - tItem; i++) {
  254. $children.eq(i).remove();
  255. }
  256. }
  257. if (tItem < $el.find('.clone.right').length) {
  258. for (var j = $children.length - 1; j > ($children.length - 1 - $el.find('.clone.right').length); j--) {
  259. scene--;
  260. $children.eq(j).remove();
  261. }
  262. }
  263. /**/
  264. for (var n = $el.find('.clone.right').length; n < tItem; n++) {
  265. $el.find('.lslide').eq(n).clone().removeClass('lslide').addClass('clone right').appendTo($el);
  266. scene++;
  267. }
  268. for (var m = $el.find('.lslide').length - $el.find('.clone.left').length; m > ($el.find('.lslide').length - tItem); m--) {
  269. $el.find('.lslide').eq(m - 1).clone().removeClass('lslide').addClass('clone left').prependTo($el);
  270. }
  271. $children = $el.children();
  272. } else {
  273. if ($children.hasClass('clone')) {
  274. $el.find('.clone').remove();
  275. $this.move($el, 0);
  276. }
  277. }
  278. };
  279. refresh.clone();
  280. }
  281. refresh.sSW = function () {
  282. length = $children.length;
  283. if (settings.rtl === true && settings.vertical === false) {
  284. gutter = 'margin-left';
  285. }
  286. if (settings.autoWidth === false) {
  287. $children.css(property, slideWidth + 'px');
  288. }
  289. $children.css(gutter, settings.slideMargin + 'px');
  290. w = refresh.calWidth(false);
  291. $el.css(property, w + 'px');
  292. if (settings.loop === true && settings.mode === 'slide') {
  293. if (on === false) {
  294. scene = $el.find('.clone.left').length;
  295. }
  296. }
  297. };
  298. refresh.calL = function () {
  299. $children = $el.children();
  300. length = $children.length;
  301. };
  302. if (this.doCss()) {
  303. $slide.addClass('usingCss');
  304. }
  305. refresh.calL();
  306. if (settings.mode === 'slide') {
  307. refresh.calSW();
  308. refresh.sSW();
  309. if (settings.loop === true) {
  310. slideValue = $this.slideValue();
  311. this.move($el, slideValue);
  312. }
  313. if (settings.vertical === false) {
  314. this.setHeight($el, false);
  315. }
  316. } else {
  317. this.setHeight($el, true);
  318. $el.addClass('lSFade');
  319. if (!this.doCss()) {
  320. $children.fadeOut(0);
  321. $children.eq(scene).fadeIn(0);
  322. }
  323. }
  324. if (settings.loop === true && settings.mode === 'slide') {
  325. $children.eq(scene).addClass('active');
  326. } else {
  327. $children.first().addClass('active');
  328. }
  329. },
  330. pager: function () {
  331. var $this = this;
  332. refresh.createPager = function () {
  333. thumbWidth = (elSize - ((settings.thumbItem * (settings.thumbMargin)) - settings.thumbMargin)) / settings.thumbItem;
  334. var $children = $slide.find('.lslide');
  335. var length = $slide.find('.lslide').length;
  336. var i = 0,
  337. pagers = '',
  338. v = 0;
  339. for (i = 0; i < length; i++) {
  340. if (settings.mode === 'slide') {
  341. // calculate scene * slide value
  342. if (!settings.autoWidth) {
  343. v = i * ((slideWidth + settings.slideMargin) * settings.slideMove);
  344. } else {
  345. v += ((parseInt($children.eq(i).width()) + settings.slideMargin) * settings.slideMove);
  346. }
  347. }
  348. var thumb = $children.eq(i * settings.slideMove).attr('data-thumb');
  349. if (settings.gallery === true) {
  350. pagers += '<li style="width:100%;' + property + ':' + thumbWidth + 'px;' + gutter + ':' + settings.thumbMargin + 'px"><a href="#"><img src="' + thumb + '" /></a></li>';
  351. } else {
  352. pagers += '<li><a href="#">' + (i + 1) + '</a></li>';
  353. }
  354. if (settings.mode === 'slide') {
  355. if ((v) >= w - elSize - settings.slideMargin) {
  356. i = i + 1;
  357. var minPgr = 2;
  358. if (settings.autoWidth) {
  359. pagers += '<li><a href="#">' + (i + 1) + '</a></li>';
  360. minPgr = 1;
  361. }
  362. if (i < minPgr) {
  363. pagers = null;
  364. $slide.parent().addClass('noPager');
  365. } else {
  366. $slide.parent().removeClass('noPager');
  367. }
  368. break;
  369. }
  370. }
  371. }
  372. var $cSouter = $slide.parent();
  373. $cSouter.find('.lSPager').html(pagers);
  374. if (settings.gallery === true) {
  375. if (settings.vertical === true) {
  376. // set Gallery thumbnail width
  377. $cSouter.find('.lSPager').css('width', settings.vThumbWidth + 'px');
  378. }
  379. pagerWidth = (i * (settings.thumbMargin + thumbWidth)) + 0.5;
  380. $cSouter.find('.lSPager').css({
  381. property: pagerWidth + 'px',
  382. 'transition-duration': settings.speed + 'ms'
  383. });
  384. if (settings.vertical === true) {
  385. $slide.parent().css('padding-right', (settings.vThumbWidth + settings.galleryMargin) + 'px');
  386. }
  387. $cSouter.find('.lSPager').css(property, pagerWidth + 'px');
  388. }
  389. var $pager = $cSouter.find('.lSPager').find('li');
  390. $pager.first().addClass('active');
  391. $pager.on('click', function () {
  392. if (settings.loop === true && settings.mode === 'slide') {
  393. scene = scene + ($pager.index(this) - $cSouter.find('.lSPager').find('li.active').index());
  394. } else {
  395. scene = $pager.index(this);
  396. }
  397. $el.mode(false);
  398. if (settings.gallery === true) {
  399. $this.slideThumb();
  400. }
  401. return false;
  402. });
  403. };
  404. if (settings.pager) {
  405. var cl = 'lSpg';
  406. if (settings.gallery) {
  407. cl = 'lSGallery';
  408. }
  409. $slide.after('<ul class="lSPager ' + cl + '"></ul>');
  410. var gMargin = (settings.vertical) ? 'margin-left' : 'margin-top';
  411. $slide.parent().find('.lSPager').css(gMargin, settings.galleryMargin + 'px');
  412. refresh.createPager();
  413. }
  414. setTimeout(function () {
  415. refresh.init();
  416. }, 0);
  417. },
  418. setHeight: function (ob, fade) {
  419. var obj = null,
  420. $this = this;
  421. if (settings.loop) {
  422. obj = ob.children('.lslide ').first();
  423. } else {
  424. obj = ob.children().first();
  425. }
  426. var setCss = function () {
  427. var tH = obj.outerHeight(),
  428. tP = 0,
  429. tHT = tH;
  430. if (fade) {
  431. tH = 0;
  432. tP = ((tHT) * 100) / elSize;
  433. }
  434. ob.css({
  435. 'height': tH + 'px',
  436. 'padding-bottom': tP + '%'
  437. });
  438. };
  439. setCss();
  440. if (obj.find('img').length) {
  441. if ( obj.find('img')[0].complete) {
  442. setCss();
  443. if (!interval) {
  444. $this.auto();
  445. }
  446. }else{
  447. obj.find('img').load(function () {
  448. setTimeout(function () {
  449. setCss();
  450. if (!interval) {
  451. $this.auto();
  452. }
  453. }, 100);
  454. });
  455. }
  456. }else{
  457. if (!interval) {
  458. $this.auto();
  459. }
  460. }
  461. },
  462. active: function (ob, t) {
  463. if (this.doCss() && settings.mode === 'fade') {
  464. $slide.addClass('on');
  465. }
  466. var sc = 0;
  467. if (scene * settings.slideMove < length) {
  468. ob.removeClass('active');
  469. if (!this.doCss() && settings.mode === 'fade' && t === false) {
  470. ob.fadeOut(settings.speed);
  471. }
  472. if (t === true) {
  473. sc = scene;
  474. } else {
  475. sc = scene * settings.slideMove;
  476. }
  477. //t === true ? sc = scene : sc = scene * settings.slideMove;
  478. var l, nl;
  479. if (t === true) {
  480. l = ob.length;
  481. nl = l - 1;
  482. if (sc + 1 >= l) {
  483. sc = nl;
  484. }
  485. }
  486. if (settings.loop === true && settings.mode === 'slide') {
  487. //t === true ? sc = scene - $el.find('.clone.left').length : sc = scene * settings.slideMove;
  488. if (t === true) {
  489. sc = scene - $el.find('.clone.left').length;
  490. } else {
  491. sc = scene * settings.slideMove;
  492. }
  493. if (t === true) {
  494. l = ob.length;
  495. nl = l - 1;
  496. if (sc + 1 === l) {
  497. sc = nl;
  498. } else if (sc + 1 > l) {
  499. sc = 0;
  500. }
  501. }
  502. }
  503. if (!this.doCss() && settings.mode === 'fade' && t === false) {
  504. ob.eq(sc).fadeIn(settings.speed);
  505. }
  506. ob.eq(sc).addClass('active');
  507. } else {
  508. ob.removeClass('active');
  509. ob.eq(ob.length - 1).addClass('active');
  510. if (!this.doCss() && settings.mode === 'fade' && t === false) {
  511. ob.fadeOut(settings.speed);
  512. ob.eq(sc).fadeIn(settings.speed);
  513. }
  514. }
  515. },
  516. move: function (ob, v) {
  517. if (settings.rtl === true) {
  518. v = -v;
  519. }
  520. if (this.doCss()) {
  521. if (settings.vertical === true) {
  522. ob.css({
  523. 'transform': 'translate3d(0px, ' + (-v) + 'px, 0px)',
  524. '-webkit-transform': 'translate3d(0px, ' + (-v) + 'px, 0px)'
  525. });
  526. } else {
  527. ob.css({
  528. 'transform': 'translate3d(' + (-v) + 'px, 0px, 0px)',
  529. '-webkit-transform': 'translate3d(' + (-v) + 'px, 0px, 0px)',
  530. });
  531. }
  532. } else {
  533. if (settings.vertical === true) {
  534. ob.css('position', 'relative').animate({
  535. top: -v + 'px'
  536. }, settings.speed, settings.easing);
  537. } else {
  538. ob.css('position', 'relative').animate({
  539. left: -v + 'px'
  540. }, settings.speed, settings.easing);
  541. }
  542. }
  543. var $thumb = $slide.parent().find('.lSPager').find('li');
  544. this.active($thumb, true);
  545. },
  546. fade: function () {
  547. this.active($children, false);
  548. var $thumb = $slide.parent().find('.lSPager').find('li');
  549. this.active($thumb, true);
  550. },
  551. slide: function () {
  552. var $this = this;
  553. refresh.calSlide = function () {
  554. if (w > elSize) {
  555. slideValue = $this.slideValue();
  556. $this.active($children, false);
  557. if ((slideValue) > w - elSize - settings.slideMargin) {
  558. slideValue = w - elSize - settings.slideMargin;
  559. } else if (slideValue < 0) {
  560. slideValue = 0;
  561. }
  562. $this.move($el, slideValue);
  563. if (settings.loop === true && settings.mode === 'slide') {
  564. if (scene >= (length - ($el.find('.clone.left').length / settings.slideMove))) {
  565. $this.resetSlide($el.find('.clone.left').length);
  566. }
  567. if (scene === 0) {
  568. $this.resetSlide($slide.find('.lslide').length);
  569. }
  570. }
  571. }
  572. };
  573. refresh.calSlide();
  574. },
  575. resetSlide: function (s) {
  576. var $this = this;
  577. $slide.find('.lSAction a').addClass('disabled');
  578. setTimeout(function () {
  579. scene = s;
  580. $slide.css('transition-duration', '0ms');
  581. slideValue = $this.slideValue();
  582. $this.active($children, false);
  583. plugin.move($el, slideValue);
  584. setTimeout(function () {
  585. $slide.css('transition-duration', settings.speed + 'ms');
  586. $slide.find('.lSAction a').removeClass('disabled');
  587. }, 50);
  588. }, settings.speed + 100);
  589. },
  590. slideValue: function () {
  591. var _sV = 0;
  592. if (settings.autoWidth === false) {
  593. _sV = scene * ((slideWidth + settings.slideMargin) * settings.slideMove);
  594. } else {
  595. _sV = 0;
  596. for (var i = 0; i < scene; i++) {
  597. _sV += (parseInt($children.eq(i).width()) + settings.slideMargin);
  598. }
  599. }
  600. return _sV;
  601. },
  602. slideThumb: function () {
  603. var position;
  604. switch (settings.currentPagerPosition) {
  605. case 'left':
  606. position = 0;
  607. break;
  608. case 'middle':
  609. position = (elSize / 2) - (thumbWidth / 2);
  610. break;
  611. case 'right':
  612. position = elSize - thumbWidth;
  613. }
  614. var sc = scene - $el.find('.clone.left').length;
  615. var $pager = $slide.parent().find('.lSPager');
  616. if (settings.mode === 'slide' && settings.loop === true) {
  617. if (sc >= $pager.children().length) {
  618. sc = 0;
  619. } else if (sc < 0) {
  620. sc = $pager.children().length;
  621. }
  622. }
  623. var thumbSlide = sc * ((thumbWidth + settings.thumbMargin)) - (position);
  624. if ((thumbSlide + elSize) > pagerWidth) {
  625. thumbSlide = pagerWidth - elSize - settings.thumbMargin;
  626. }
  627. if (thumbSlide < 0) {
  628. thumbSlide = 0;
  629. }
  630. this.move($pager, thumbSlide);
  631. },
  632. auto: function () {
  633. if (settings.auto) {
  634. clearInterval(interval);
  635. interval = setInterval(function () {
  636. $el.goToNextSlide();
  637. }, settings.pause);
  638. }
  639. },
  640. pauseOnHover: function(){
  641. var $this = this;
  642. if (settings.auto && settings.pauseOnHover) {
  643. $slide.on('mouseenter', function(){
  644. $(this).addClass('ls-hover');
  645. $el.pause();
  646. settings.auto = true;
  647. });
  648. $slide.on('mouseleave',function(){
  649. $(this).removeClass('ls-hover');
  650. if (!$slide.find('.lightSlider').hasClass('lsGrabbing')) {
  651. $this.auto();
  652. }
  653. });
  654. }
  655. },
  656. touchMove: function (endCoords, startCoords) {
  657. $slide.css('transition-duration', '0ms');
  658. if (settings.mode === 'slide') {
  659. var distance = endCoords - startCoords;
  660. var swipeVal = slideValue - distance;
  661. if ((swipeVal) >= w - elSize - settings.slideMargin) {
  662. if (settings.freeMove === false) {
  663. swipeVal = w - elSize - settings.slideMargin;
  664. } else {
  665. var swipeValT = w - elSize - settings.slideMargin;
  666. swipeVal = swipeValT + ((swipeVal - swipeValT) / 5);
  667. }
  668. } else if (swipeVal < 0) {
  669. if (settings.freeMove === false) {
  670. swipeVal = 0;
  671. } else {
  672. swipeVal = swipeVal / 5;
  673. }
  674. }
  675. this.move($el, swipeVal);
  676. }
  677. },
  678. touchEnd: function (distance) {
  679. $slide.css('transition-duration', settings.speed + 'ms');
  680. if (settings.mode === 'slide') {
  681. var mxVal = false;
  682. var _next = true;
  683. slideValue = slideValue - distance;
  684. if ((slideValue) > w - elSize - settings.slideMargin) {
  685. slideValue = w - elSize - settings.slideMargin;
  686. if (settings.autoWidth === false) {
  687. mxVal = true;
  688. }
  689. } else if (slideValue < 0) {
  690. slideValue = 0;
  691. }
  692. var gC = function (next) {
  693. var ad = 0;
  694. if (!mxVal) {
  695. if (next) {
  696. ad = 1;
  697. }
  698. }
  699. if (!settings.autoWidth) {
  700. var num = slideValue / ((slideWidth + settings.slideMargin) * settings.slideMove);
  701. scene = parseInt(num) + ad;
  702. if (slideValue >= (w - elSize - settings.slideMargin)) {
  703. if (num % 1 !== 0) {
  704. scene++;
  705. }
  706. }
  707. } else {
  708. var tW = 0;
  709. for (var i = 0; i < $children.length; i++) {
  710. tW += (parseInt($children.eq(i).width()) + settings.slideMargin);
  711. scene = i + ad;
  712. if (tW >= slideValue) {
  713. break;
  714. }
  715. }
  716. }
  717. };
  718. if (distance >= settings.swipeThreshold) {
  719. gC(false);
  720. _next = false;
  721. } else if (distance <= -settings.swipeThreshold) {
  722. gC(true);
  723. _next = false;
  724. }
  725. $el.mode(_next);
  726. this.slideThumb();
  727. } else {
  728. if (distance >= settings.swipeThreshold) {
  729. $el.goToPrevSlide();
  730. } else if (distance <= -settings.swipeThreshold) {
  731. $el.goToNextSlide();
  732. }
  733. }
  734. },
  735. enableDrag: function () {
  736. var $this = this;
  737. if (!isTouch) {
  738. var startCoords = 0,
  739. endCoords = 0,
  740. isDraging = false;
  741. $slide.find('.lightSlider').addClass('lsGrab');
  742. $slide.on('mousedown', function (e) {
  743. if (w < elSize) {
  744. if (w !== 0) {
  745. return false;
  746. }
  747. }
  748. if ($(e.target).attr('class') !== ('lSPrev') && $(e.target).attr('class') !== ('lSNext')) {
  749. startCoords = (settings.vertical === true) ? e.pageY : e.pageX;
  750. isDraging = true;
  751. if (e.preventDefault) {
  752. e.preventDefault();
  753. } else {
  754. e.returnValue = false;
  755. }
  756. // ** Fix for webkit cursor issue https://code.google.com/p/chromium/issues/detail?id=26723
  757. $slide.scrollLeft += 1;
  758. $slide.scrollLeft -= 1;
  759. // *
  760. $slide.find('.lightSlider').removeClass('lsGrab').addClass('lsGrabbing');
  761. clearInterval(interval);
  762. }
  763. });
  764. $(window).on('mousemove', function (e) {
  765. if (isDraging) {
  766. endCoords = (settings.vertical === true) ? e.pageY : e.pageX;
  767. $this.touchMove(endCoords, startCoords);
  768. }
  769. });
  770. $(window).on('mouseup', function (e) {
  771. if (isDraging) {
  772. $slide.find('.lightSlider').removeClass('lsGrabbing').addClass('lsGrab');
  773. isDraging = false;
  774. endCoords = (settings.vertical === true) ? e.pageY : e.pageX;
  775. var distance = endCoords - startCoords;
  776. if (Math.abs(distance) >= settings.swipeThreshold) {
  777. $(window).on('click.ls', function (e) {
  778. if (e.preventDefault) {
  779. e.preventDefault();
  780. } else {
  781. e.returnValue = false;
  782. }
  783. e.stopImmediatePropagation();
  784. e.stopPropagation();
  785. $(window).off('click.ls');
  786. });
  787. }
  788. $this.touchEnd(distance);
  789. }
  790. });
  791. }
  792. },
  793. enableTouch: function () {
  794. var $this = this;
  795. if (isTouch) {
  796. var startCoords = {},
  797. endCoords = {};
  798. $slide.on('touchstart', function (e) {
  799. endCoords = e.originalEvent.targetTouches[0];
  800. startCoords.pageX = e.originalEvent.targetTouches[0].pageX;
  801. startCoords.pageY = e.originalEvent.targetTouches[0].pageY;
  802. clearInterval(interval);
  803. });
  804. $slide.on('touchmove', function (e) {
  805. if (w < elSize) {
  806. if (w !== 0) {
  807. return false;
  808. }
  809. }
  810. var orig = e.originalEvent;
  811. endCoords = orig.targetTouches[0];
  812. var xMovement = Math.abs(endCoords.pageX - startCoords.pageX);
  813. var yMovement = Math.abs(endCoords.pageY - startCoords.pageY);
  814. if (settings.vertical === true) {
  815. if ((yMovement * 3) > xMovement) {
  816. e.preventDefault();
  817. }
  818. $this.touchMove(endCoords.pageY, startCoords.pageY);
  819. } else {
  820. if ((xMovement * 3) > yMovement) {
  821. e.preventDefault();
  822. }
  823. $this.touchMove(endCoords.pageX, startCoords.pageX);
  824. }
  825. });
  826. $slide.on('touchend', function () {
  827. if (w < elSize) {
  828. if (w !== 0) {
  829. return false;
  830. }
  831. }
  832. var distance;
  833. if (settings.vertical === true) {
  834. distance = endCoords.pageY - startCoords.pageY;
  835. } else {
  836. distance = endCoords.pageX - startCoords.pageX;
  837. }
  838. $this.touchEnd(distance);
  839. });
  840. }
  841. },
  842. build: function () {
  843. var $this = this;
  844. $this.initialStyle();
  845. if (this.doCss()) {
  846. if (settings.enableTouch === true) {
  847. $this.enableTouch();
  848. }
  849. if (settings.enableDrag === true) {
  850. $this.enableDrag();
  851. }
  852. }
  853. $(window).on('focus', function(){
  854. $this.auto();
  855. });
  856. $(window).on('blur', function(){
  857. clearInterval(interval);
  858. });
  859. $this.pager();
  860. $this.pauseOnHover();
  861. $this.controls();
  862. $this.keyPress();
  863. }
  864. };
  865. plugin.build();
  866. refresh.init = function () {
  867. refresh.chbreakpoint();
  868. if (settings.vertical === true) {
  869. if (settings.item > 1) {
  870. elSize = settings.verticalHeight;
  871. } else {
  872. elSize = $children.outerHeight();
  873. }
  874. $slide.css('height', elSize + 'px');
  875. } else {
  876. elSize = $slide.outerWidth();
  877. }
  878. if (settings.loop === true && settings.mode === 'slide') {
  879. refresh.clone();
  880. }
  881. refresh.calL();
  882. if (settings.mode === 'slide') {
  883. $el.removeClass('lSSlide');
  884. }
  885. if (settings.mode === 'slide') {
  886. refresh.calSW();
  887. refresh.sSW();
  888. }
  889. setTimeout(function () {
  890. if (settings.mode === 'slide') {
  891. $el.addClass('lSSlide');
  892. }
  893. }, 1000);
  894. if (settings.pager) {
  895. refresh.createPager();
  896. }
  897. if (settings.adaptiveHeight === true && settings.vertical === false) {
  898. $el.css('height', $children.eq(scene).outerHeight(true));
  899. }
  900. if (settings.adaptiveHeight === false) {
  901. if (settings.mode === 'slide') {
  902. if (settings.vertical === false) {
  903. plugin.setHeight($el, false);
  904. }else{
  905. plugin.auto();
  906. }
  907. } else {
  908. plugin.setHeight($el, true);
  909. }
  910. }
  911. if (settings.gallery === true) {
  912. plugin.slideThumb();
  913. }
  914. if (settings.mode === 'slide') {
  915. plugin.slide();
  916. }
  917. if (settings.autoWidth === false) {
  918. if ($children.length <= settings.item) {
  919. $slide.find('.lSAction').hide();
  920. } else {
  921. $slide.find('.lSAction').show();
  922. }
  923. } else {
  924. if ((refresh.calWidth(false) < elSize) && (w !== 0)) {
  925. $slide.find('.lSAction').hide();
  926. } else {
  927. $slide.find('.lSAction').show();
  928. }
  929. }
  930. };
  931. $el.goToPrevSlide = function () {
  932. if (scene > 0) {
  933. settings.onBeforePrevSlide.call(this, $el, scene);
  934. scene--;
  935. $el.mode(false);
  936. if (settings.gallery === true) {
  937. plugin.slideThumb();
  938. }
  939. } else {
  940. if (settings.loop === true) {
  941. settings.onBeforePrevSlide.call(this, $el, scene);
  942. if (settings.mode === 'fade') {
  943. var l = (length - 1);
  944. scene = parseInt(l / settings.slideMove);
  945. }
  946. $el.mode(false);
  947. if (settings.gallery === true) {
  948. plugin.slideThumb();
  949. }
  950. } else if (settings.slideEndAnimation === true) {
  951. $el.addClass('leftEnd');
  952. setTimeout(function () {
  953. $el.removeClass('leftEnd');
  954. }, 400);
  955. }
  956. }
  957. };
  958. $el.goToNextSlide = function () {
  959. var nextI = true;
  960. if (settings.mode === 'slide') {
  961. var _slideValue = plugin.slideValue();
  962. nextI = _slideValue < w - elSize - settings.slideMargin;
  963. }
  964. if (((scene * settings.slideMove) < length - settings.slideMove) && nextI) {
  965. settings.onBeforeNextSlide.call(this, $el, scene);
  966. scene++;
  967. $el.mode(false);
  968. if (settings.gallery === true) {
  969. plugin.slideThumb();
  970. }
  971. } else {
  972. if (settings.loop === true) {
  973. settings.onBeforeNextSlide.call(this, $el, scene);
  974. scene = 0;
  975. $el.mode(false);
  976. if (settings.gallery === true) {
  977. plugin.slideThumb();
  978. }
  979. } else if (settings.slideEndAnimation === true) {
  980. $el.addClass('rightEnd');
  981. setTimeout(function () {
  982. $el.removeClass('rightEnd');
  983. }, 400);
  984. }
  985. }
  986. };
  987. $el.mode = function (_touch) {
  988. if (settings.adaptiveHeight === true && settings.vertical === false) {
  989. $el.css('height', $children.eq(scene).outerHeight(true));
  990. }
  991. if (on === false) {
  992. if (settings.mode === 'slide') {
  993. if (plugin.doCss()) {
  994. $el.addClass('lSSlide');
  995. if (settings.speed !== '') {
  996. $slide.css('transition-duration', settings.speed + 'ms');
  997. }
  998. if (settings.cssEasing !== '') {
  999. $slide.css('transition-timing-function', settings.cssEasing);
  1000. }
  1001. }
  1002. } else {
  1003. if (plugin.doCss()) {
  1004. if (settings.speed !== '') {
  1005. $el.css('transition-duration', settings.speed + 'ms');
  1006. }
  1007. if (settings.cssEasing !== '') {
  1008. $el.css('transition-timing-function', settings.cssEasing);
  1009. }
  1010. }
  1011. }
  1012. }
  1013. if (!_touch) {
  1014. settings.onBeforeSlide.call(this, $el, scene);
  1015. }
  1016. if (settings.mode === 'slide') {
  1017. plugin.slide();
  1018. } else {
  1019. plugin.fade();
  1020. }
  1021. if (!$slide.hasClass('ls-hover')) {
  1022. plugin.auto();
  1023. }
  1024. setTimeout(function () {
  1025. if (!_touch) {
  1026. settings.onAfterSlide.call(this, $el, scene);
  1027. }
  1028. }, settings.speed);
  1029. on = true;
  1030. };
  1031. $el.play = function () {
  1032. $el.goToNextSlide();
  1033. settings.auto = true;
  1034. plugin.auto();
  1035. };
  1036. $el.pause = function () {
  1037. settings.auto = false;
  1038. clearInterval(interval);
  1039. };
  1040. $el.refresh = function () {
  1041. refresh.init();
  1042. };
  1043. $el.getCurrentSlideCount = function () {
  1044. var sc = scene;
  1045. if (settings.loop) {
  1046. var ln = $slide.find('.lslide').length,
  1047. cl = $el.find('.clone.left').length;
  1048. if (scene <= cl - 1) {
  1049. sc = ln + (scene - cl);
  1050. } else if (scene >= (ln + cl)) {
  1051. sc = scene - ln - cl;
  1052. } else {
  1053. sc = scene - cl;
  1054. }
  1055. }
  1056. return sc + 1;
  1057. };
  1058. $el.getTotalSlideCount = function () {
  1059. return $slide.find('.lslide').length;
  1060. };
  1061. $el.goToSlide = function (s) {
  1062. if (settings.loop) {
  1063. scene = (s + $el.find('.clone.left').length - 1);
  1064. } else {
  1065. scene = s;
  1066. }
  1067. $el.mode(false);
  1068. if (settings.gallery === true) {
  1069. plugin.slideThumb();
  1070. }
  1071. };
  1072. $el.destroy = function () {
  1073. if ($el.lightSlider) {
  1074. $el.goToPrevSlide = function(){};
  1075. $el.goToNextSlide = function(){};
  1076. $el.mode = function(){};
  1077. $el.play = function(){};
  1078. $el.pause = function(){};
  1079. $el.refresh = function(){};
  1080. $el.getCurrentSlideCount = function(){};
  1081. $el.getTotalSlideCount = function(){};
  1082. $el.goToSlide = function(){};
  1083. $el.lightSlider = null;
  1084. refresh = {
  1085. init : function(){}
  1086. };
  1087. $el.parent().parent().find('.lSAction, .lSPager').remove();
  1088. $el.removeClass('lightSlider lSFade lSSlide lsGrab lsGrabbing leftEnd right').removeAttr('style').unwrap().unwrap();
  1089. $el.children().removeAttr('style');
  1090. $children.removeClass('lslide active');
  1091. $el.find('.clone').remove();
  1092. $children = null;
  1093. interval = null;
  1094. on = false;
  1095. scene = 0;
  1096. }
  1097. };
  1098. setTimeout(function () {
  1099. settings.onSliderLoad.call(this, $el);
  1100. }, 10);
  1101. $(window).on('resize orientationchange', function (e) {
  1102. setTimeout(function () {
  1103. if (e.preventDefault) {
  1104. e.preventDefault();
  1105. } else {
  1106. e.returnValue = false;
  1107. }
  1108. refresh.init();
  1109. }, 200);
  1110. });
  1111. return this;
  1112. };
  1113. }(jQuery));