Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

461 linhas
13 KiB

  1. import $ from 'jquery';
  2. import Hammer from 'hammerjs';
  3. require('jquery.easing');
  4. class IHKSlider {
  5. constructor(section) {
  6. this.section = section.addClass('initiated');
  7. this.slides = section.children();
  8. this.slideOverflow = $('<div class="slide-overflow" />').appendTo(section);
  9. this.slideWrapper = $('<div class="slide-wrapper" />').appendTo(this.slideOverflow).append(this.slides);
  10. this.currentSlide = 0;
  11. this.autoplayTimeout = 0;
  12. this.isHovered = false;
  13. this.sectionInitialClicked = false;
  14. this.dragging = false;
  15. this.inViewport = false;
  16. this.positionAnimation = {
  17. x: 0
  18. }
  19. this.settings = {
  20. loop: false,
  21. autoplay: this.section.data('autoplay'),
  22. infinite: true,
  23. autoplaySpeed: this.section.data('autoplay-speed') ? this.section.data('autoplay-speed') : 5000,
  24. animationSpeed: 600,
  25. startSlide: 0,
  26. };
  27. this.initSlides();
  28. if (this.settings.infinite) {
  29. this.initInfinity();
  30. }
  31. this.initUI();
  32. this.checkSize();
  33. if (this.slides.length > 1) {
  34. this.initHammerDragging();
  35. this.initTabbing();
  36. }
  37. this.changeSlide(this.settings.startSlide);
  38. this.initAutoplay();
  39. this.checkSize();
  40. if (this.section.closest('.steps').length) {
  41. this.setupSteps();
  42. }
  43. $(window).on('resize', () => {
  44. this.checkSize();
  45. })
  46. }
  47. initSlides() {
  48. this.slides.each(function (i) {
  49. const slide = $(this).attr('data-index', i);
  50. const h3 = slide.find('h3');
  51. const p = h3.next('p');
  52. const dotsString = '&hellip;'
  53. if (!slide.find('.image-box').length) {
  54. slide.addClass('text-only');
  55. } else {
  56. if (h3.text().length > 48) {
  57. h3.html(h3.text().substring(0, 48) + dotsString);
  58. }
  59. if (p.text().length > 148) {
  60. p.html(p.text().substring(0, 148) + dotsString);
  61. }
  62. }
  63. })
  64. }
  65. checkSize() {
  66. if (this.controls.find('.tabs').width() > this.slideOverflow.width() * 0.6) {
  67. this.section.addClass('many-slides');
  68. } else {
  69. this.section.removeClass('many-slides');
  70. }
  71. }
  72. initInfinity() {
  73. this.nextWrapper = this.slideWrapper.clone();
  74. this.prevWrapper = this.slideWrapper.clone();
  75. this.nextWrapper
  76. .removeClass('slide-wrapper')
  77. .addClass('next-clone')
  78. .appendTo(this.slideWrapper)
  79. .css({'left': this.slides.length + '%'});
  80. this.prevWrapper
  81. .removeClass('slide-wrapper')
  82. .addClass('prev-clone')
  83. .appendTo(this.slideWrapper);
  84. this.nextWrapper.find('a, button, input, select, textarea').attr('tabindex', -1);
  85. this.prevWrapper.find('a, button, input, select, textarea').attr('tabindex', -1);
  86. this.nextWrapper.find('img').removeClass('loading');
  87. this.prevWrapper.find('img').removeClass('loading');
  88. }
  89. initUI() {
  90. this.count = $('<span />');
  91. this.prevButton = $('<button class="prev" aria-label="Previous Slide" />');
  92. this.nextButton = $('<button class="next" aria-label="Next Slide" />');
  93. const tabsWrapper = $('<ul class="slider-tabs" />');
  94. const countWrapper = $('<span class="count" />')
  95. .html('/<span class="total">' + this.slides.length + '</span></span>')
  96. .prepend(this.count);
  97. this.slides.each(function (i) {
  98. const s = $(this);
  99. const li = $('<li/>').appendTo(tabsWrapper);
  100. $('<button />').addClass('btn').appendTo(li).text(s.data('title') ? s.data('title') : i + 1);
  101. });
  102. this.tabs = tabsWrapper.children();
  103. tabsWrapper.find('button').on('click', (e) => {
  104. e.preventDefault();
  105. this.sectionInitialClicked = true;
  106. this.changeSlide($(e.currentTarget).parent().index());
  107. });
  108. this.prevButton.on('click', (e) => {
  109. e.preventDefault();
  110. this.sectionInitialClicked = true;
  111. this.onPrev();
  112. });
  113. this.nextButton.on('click', (e) => {
  114. e.preventDefault();
  115. this.sectionInitialClicked = true;
  116. this.onNext();
  117. });
  118. this.controls = $('<div class="controls" />')
  119. .append(this.prevButton)
  120. .append(tabsWrapper)
  121. .append(countWrapper)
  122. .append(this.nextButton)
  123. .appendTo(this.section);
  124. }
  125. changeSlide(index, offset, transition = true) {
  126. const t = this;
  127. const prevIndex = index === 0 ? this.slides.length - 1 : index - 1;
  128. const nextIndex = index === this.slides.length - 1 ? 0 : index + 1;
  129. const allWidth = t.nextWrapper.position().left;
  130. if (!offset) {
  131. offset = 0;
  132. }
  133. if (index === this.currentSlide && this.slideOverflow.outerHeight() === 0) {
  134. transition = false;
  135. }
  136. t.positionAnimation = {
  137. x: this.slideWrapper.position().left
  138. }
  139. let target = (index + offset) / -100 * this.slideWrapper.width();
  140. let time = Math.round(Math.abs(this.positionAnimation.x - target) / 5 + 200);
  141. if (time > 500) {
  142. time = 500;
  143. }
  144. if (!transition) {
  145. time = 0;
  146. }
  147. t.slideOverflow.css({
  148. 'transition-duration': time + 'ms', 'height': this.slideOverflow.outerHeight() + 'px'
  149. });
  150. t.slideWrapper.find('.current').removeClass('current');
  151. t.slideWrapper.find('[data-index="' + index + '"]').addClass('current');
  152. t.slideWrapper.find('.is-prev').removeClass('is-prev');
  153. t.slideWrapper.find('[data-index="' + prevIndex + '"]').addClass('is-prev');
  154. t.slideWrapper.find('.is-next').removeClass('is-next');
  155. t.slideWrapper.find('[data-index="' + nextIndex + '"]').addClass('is-next');
  156. if (target > 0) {
  157. target = target - allWidth;
  158. this.positionAnimation.x = this.positionAnimation.x - allWidth;
  159. }
  160. if (target <= allWidth * -1 + 1) {
  161. target = target + allWidth;
  162. this.positionAnimation.x = this.positionAnimation.x + allWidth;
  163. }
  164. requestAnimationFrame(() => {
  165. this.slideOverflow.css({
  166. 'height': this.slides.eq(index).outerHeight() + 'px'
  167. });
  168. })
  169. $(this.positionAnimation).animate({
  170. x: target
  171. }, {
  172. duration: time, easing: 'easeOutCubic', step: (now) => {
  173. this.slideWrapper.css({'transform': 'translate3d(' + Math.round(now) + 'px, 0, 0)'});
  174. }, complete: () => {
  175. requestAnimationFrame(() => {
  176. this.slideWrapper.css({'transform': 'translate3d(' + (index * -1) + '%, 0, 0)'});
  177. this.slideOverflow.css('height', 'auto');
  178. });
  179. }
  180. })
  181. this.tabs.eq(index).addClass('active').siblings('.active').removeClass('active');
  182. this.count.text(index + 1);
  183. if (this.settings.autoplay && !this.isHovered) {
  184. this.handleAutoplay();
  185. }
  186. if (offset !== 0) {
  187. setTimeout(() => {
  188. this.slideWrapper.addClass('no-transition');
  189. this.slideWrapper.css({'transform': 'translate3d(' + (index * -1) + '%, 0, 0)'});
  190. setTimeout(() => {
  191. this.slideWrapper.removeClass('no-transition');
  192. }, 20);
  193. }, this.settings.animationSpeed);
  194. }
  195. this.currentSlide = index;
  196. this.section.trigger('slide-change');
  197. if (this.section.closest('.steps').length && this.sectionInitialClicked) {
  198. this.scrollToTop();
  199. }
  200. }
  201. goTo(index, transition) {
  202. if (transition === false) {
  203. this.slideWrapper.addClass('no-transition');
  204. }
  205. this.sectionInitialClicked = true;
  206. this.changeSlide(index, 0, transition);
  207. setTimeout(() => {
  208. this.slideWrapper.removeClass('no-transition');
  209. }, 20);
  210. }
  211. onNext() {
  212. const nextSlide = this.currentSlide === this.slides.length - 1 ? 0 : this.currentSlide + 1;
  213. const offset = this.settings.infinite && nextSlide === 0 ? this.slides.length : 0;
  214. this.changeSlide(nextSlide, offset);
  215. }
  216. onPrev() {
  217. const prevSlide = this.currentSlide === 0 ? this.slides.length - 1 : this.currentSlide - 1;
  218. const offset = this.settings.infinite && prevSlide === this.slides.length - 1 ? this.slides.length * -1 : 0;
  219. this.changeSlide(prevSlide, offset);
  220. }
  221. initAutoplay() {
  222. this.section.on('mouseenter', () => {
  223. this.isHovered = true;
  224. clearTimeout(this.autoplayTimeout);
  225. })
  226. this.section.on('mouseleave', () => {
  227. this.isHovered = false;
  228. this.handleAutoplay();
  229. })
  230. this.initScrollCheck();
  231. this.handleAutoplay();
  232. }
  233. handleAutoplay() {
  234. if (this.dragging) {
  235. return false;
  236. }
  237. clearTimeout(this.autoplayTimeout);
  238. if (this.settings.autoplaySpeed > 0 && this.settings.autoplay) {
  239. this.autoplayTimeout = setTimeout(() => {
  240. if (this.inViewport) {
  241. this.onNext();
  242. } else {
  243. this.handleAutoplay();
  244. }
  245. }, this.settings.autoplaySpeed);
  246. }
  247. }
  248. initScrollCheck() {
  249. window.addEventListener("scroll", () => {
  250. window.requestAnimationFrame(() => {
  251. this.scrollCheck();
  252. })
  253. }, {passive: true});
  254. window.requestAnimationFrame(() => {
  255. this.scrollCheck();
  256. })
  257. }
  258. scrollCheck() {
  259. const w = $(window);
  260. if (w.scrollTop() + w.height() - 200 > this.section.offset().top && w.scrollTop() < this.section.offset().top + this.section.outerHeight() / 2) {
  261. if (!this.inViewport) {
  262. this.inViewport = true;
  263. this.section.addClass('in-viewport').trigger('in-viewport');
  264. }
  265. } else if (this.inViewport) {
  266. this.inViewport = false;
  267. this.section.removeClass('in-viewport');
  268. }
  269. }
  270. initHammerDragging() {
  271. const hammer = new Hammer(this.slideWrapper.get(0));
  272. let sliderWidth = 0;
  273. let startValue = 0;
  274. this.dragging = false;
  275. this.slideWrapper.find('img, a').attr('draggable', 'false').attr('ondragstart', 'return false;');
  276. hammer.on('panstart', (e) => {
  277. sliderWidth = this.slideOverflow.width();
  278. this.dragging = true;
  279. startValue = this.slideWrapper.position().left;
  280. $(this.positionAnimation).stop();
  281. requestAnimationFrame(() => {
  282. $('html').addClass('slider-dragging');
  283. })
  284. if (e.center.x !== 0 && e.center.y !== 0) {
  285. clearTimeout(this.autoplayTimeout);
  286. }
  287. });
  288. hammer.on('pan', (e) => {
  289. if (e.center.x !== 0 && e.center.y !== 0) {
  290. const transformX = Math.round(startValue + e.deltaX);
  291. this.slideWrapper.css({'transform': 'translate3d(' + transformX + 'px, 0, 0)'});
  292. }
  293. });
  294. hammer.on('panend', (e) => {
  295. this.dragging = false;
  296. requestAnimationFrame(() => {
  297. $('html').removeClass('slider-dragging');
  298. if (e.deltaX > sliderWidth / 5) {
  299. this.onPrev();
  300. } else if (e.deltaX < sliderWidth / -5) {
  301. this.onNext();
  302. } else {
  303. this.changeSlide(this.currentSlide);
  304. }
  305. })
  306. });
  307. }
  308. initTabbing() {
  309. $(window).keydown((e) => {
  310. const code = (e.keyCode ? e.keyCode : e.which);
  311. if (code === 9 && this.slideWrapper.find(':focus').length) {
  312. this.tabScrollActive = true;
  313. this.resetTabScroll();
  314. }
  315. })
  316. $(window).keyup((e) => {
  317. const code = (e.keyCode ? e.keyCode : e.which);
  318. if (code === 9 && this.slideWrapper.find(':focus').length) {
  319. const slide = this.section.find(':focus').closest('.slide');
  320. this.slideOverflow.scrollLeft(0).scrollTop(0).find('.outer, .text-box').scrollLeft(0).scrollTop(0);
  321. this.goTo(parseInt(slide.attr('data-index')), true);
  322. this.tabScrollActive = false;
  323. }
  324. });
  325. }
  326. loadImage(index) {
  327. const t = this;
  328. if (!index) {
  329. index = this.currentSlide;
  330. }
  331. if (t.slides.eq(index).hasClass('preload') && !t.slides.eq(index).find('img').length) {
  332. const ib = t.slides.eq(index).find('.image-box');
  333. const img = $('<img/>').one('load', function (e) {
  334. const loadedImage = $(this).removeClass('loading');
  335. const slide = loadedImage.closest('.slide').removeClass('preload');
  336. const index = parseInt(slide.attr('data-index'));
  337. t.nextWrapper.find('[data-index="' + index + '"]').removeClass('preload').find('.image-box').append(loadedImage.clone());
  338. t.prevWrapper.find('[data-index="' + index + '"]').removeClass('preload').find('.image-box').append(loadedImage.clone());
  339. })
  340. img.attr('src', ib.attr('data-src')).appendTo(ib);
  341. img.attr('data-download', ib.attr('data-download')).appendTo(ib);
  342. img.attr('draggable', 'false').attr('ondragstart', 'return false;');
  343. }
  344. }
  345. resetTabScroll() {
  346. this.slideOverflow.scrollLeft(0).scrollTop(0).find('.outer, .text-box').scrollLeft(0).scrollTop(0);
  347. if (this.tabScrollActive) {
  348. window.requestAnimationFrame(() => {
  349. this.resetTabScroll();
  350. })
  351. }
  352. }
  353. scrollToTop() {
  354. const target = this.section.closest('.steps').offset().top - 120;
  355. const time = 600;
  356. $('html, body').animate({'scrollTop': target}, time, 'easeOutQuad');
  357. };
  358. setupSteps() {
  359. this.nextButton.text('Weiter').addClass('btn').addClass('has-icon').addClass('icon-right').addClass('icon-pfeil-rechts');
  360. }
  361. }
  362. export default IHKSlider;
  363. $('body').on('ihk-init dynamic-component-loaded gfi-dynamic-init', () => {
  364. $('.rotation .slider:not(.initiated), .steps .slider:not(.initiated)').each((i, el) => {
  365. const selector = $(el);
  366. if (!selector.find('dynamic-content').length) {
  367. new IHKSlider(selector);
  368. }
  369. })
  370. /*
  371. let slidersSelector;
  372. if (e.type === "ihk-init") {
  373. slidersSelector = $('.rotation .slider:not(.initiated):not(:has(dynamic-content)), .steps .slider:not(.initiated):not(:has(dynamic-content))')
  374. } else {
  375. slidersSelector = $('.rotation .slider:not(.initiated), .steps .slider:not(.initiated)');
  376. }
  377. slidersSelector.each(function () {
  378. new Slider($(this));
  379. });
  380. */
  381. });