You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

479 lines
14 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.tabs = tabsWrapper.children();
  98. if (this.slides.length > 1) {
  99. this.slides.each(function (i) {
  100. const s = $(this);
  101. const li = $('<li/>').appendTo(tabsWrapper);
  102. var button = $('<button />').addClass('btn').appendTo(li);
  103. const span = $('<span/>').appendTo(button);
  104. span.text(s.data('title') ? s.data('title') : i + 1);
  105. });
  106. this.tabs = tabsWrapper.children();
  107. tabsWrapper.find('button').on('click', (e) => {
  108. e.preventDefault();
  109. this.sectionInitialClicked = true;
  110. this.changeSlide($(e.currentTarget).parent().index());
  111. });
  112. this.prevButton.on('click', (e) => {
  113. e.preventDefault();
  114. this.sectionInitialClicked = true;
  115. this.onPrev();
  116. });
  117. this.nextButton.on('click', (e) => {
  118. e.preventDefault();
  119. this.sectionInitialClicked = true;
  120. this.onNext();
  121. });
  122. this.controls = $('<div class="controls" />')
  123. .append(this.prevButton)
  124. .append(tabsWrapper)
  125. .append(countWrapper)
  126. .append(this.nextButton)
  127. .appendTo(this.section);
  128. } else {
  129. this.controls = $('<div class="controls" />')
  130. .append(tabsWrapper)
  131. .append(countWrapper)
  132. .appendTo(this.section);
  133. }
  134. }
  135. changeSlide(index, offset, transition = true) {
  136. const t = this;
  137. const prevIndex = index === 0 ? this.slides.length - 1 : index - 1;
  138. const nextIndex = index === this.slides.length - 1 ? 0 : index + 1;
  139. const allWidth = t.nextWrapper.position().left;
  140. if (!offset) {
  141. offset = 0;
  142. }
  143. if (index === this.currentSlide && this.slideOverflow.outerHeight() === 0) {
  144. transition = false;
  145. }
  146. t.positionAnimation = {
  147. x: this.slideWrapper.position().left
  148. }
  149. let target = (index + offset) / -100 * this.slideWrapper.width();
  150. let time = Math.round(Math.abs(this.positionAnimation.x - target) / 5 + 200);
  151. if (time > 500) {
  152. time = 500;
  153. }
  154. if (!transition) {
  155. time = 0;
  156. }
  157. t.slideOverflow.css({
  158. 'transition-duration': time + 'ms', 'height': this.slideOverflow.outerHeight() + 'px'
  159. });
  160. t.slideWrapper.find('.current').removeClass('current');
  161. t.slideWrapper.find('[data-index="' + index + '"]').addClass('current');
  162. t.slideWrapper.find('.is-prev').removeClass('is-prev');
  163. t.slideWrapper.find('[data-index="' + prevIndex + '"]').addClass('is-prev');
  164. t.slideWrapper.find('.is-next').removeClass('is-next');
  165. t.slideWrapper.find('[data-index="' + nextIndex + '"]').addClass('is-next');
  166. if (target > 0) {
  167. target = target - allWidth;
  168. this.positionAnimation.x = this.positionAnimation.x - allWidth;
  169. }
  170. if (target <= allWidth * -1 + 1) {
  171. target = target + allWidth;
  172. this.positionAnimation.x = this.positionAnimation.x + allWidth;
  173. }
  174. requestAnimationFrame(() => {
  175. this.slideOverflow.css({
  176. 'height': this.slides.eq(index).outerHeight() + 'px'
  177. });
  178. })
  179. $(this.positionAnimation).animate({
  180. x: target
  181. }, {
  182. duration: time, easing: 'easeOutCubic', step: (now) => {
  183. this.slideWrapper.css({'transform': 'translate3d(' + Math.round(now) + 'px, 0, 0)'});
  184. }, complete: () => {
  185. requestAnimationFrame(() => {
  186. this.slideWrapper.css({'transform': 'translate3d(' + (index * -1) + '%, 0, 0)'});
  187. this.slideOverflow.css('height', 'auto');
  188. });
  189. }
  190. })
  191. this.tabs.eq(index).addClass('active').siblings('.active').removeClass('active');
  192. this.count.text(index + 1);
  193. if (this.settings.autoplay && !this.isHovered) {
  194. this.handleAutoplay();
  195. }
  196. if (offset !== 0) {
  197. setTimeout(() => {
  198. this.slideWrapper.addClass('no-transition');
  199. this.slideWrapper.css({'transform': 'translate3d(' + (index * -1) + '%, 0, 0)'});
  200. setTimeout(() => {
  201. this.slideWrapper.removeClass('no-transition');
  202. }, 20);
  203. }, this.settings.animationSpeed);
  204. }
  205. this.currentSlide = index;
  206. this.section.trigger('slide-change');
  207. if (this.section.closest('.steps').length && this.sectionInitialClicked) {
  208. this.scrollToTop();
  209. }
  210. }
  211. goTo(index, transition) {
  212. if (transition === false) {
  213. this.slideWrapper.addClass('no-transition');
  214. }
  215. this.sectionInitialClicked = true;
  216. this.changeSlide(index, 0, transition);
  217. setTimeout(() => {
  218. this.slideWrapper.removeClass('no-transition');
  219. }, 20);
  220. }
  221. onNext() {
  222. const nextSlide = this.currentSlide === this.slides.length - 1 ? 0 : this.currentSlide + 1;
  223. const offset = this.settings.infinite && nextSlide === 0 ? this.slides.length : 0;
  224. this.changeSlide(nextSlide, offset);
  225. }
  226. onPrev() {
  227. const prevSlide = this.currentSlide === 0 ? this.slides.length - 1 : this.currentSlide - 1;
  228. const offset = this.settings.infinite && prevSlide === this.slides.length - 1 ? this.slides.length * -1 : 0;
  229. this.changeSlide(prevSlide, offset);
  230. }
  231. initAutoplay() {
  232. this.section.on('mouseenter', () => {
  233. this.isHovered = true;
  234. clearTimeout(this.autoplayTimeout);
  235. this.section.removeClass("btnanimation");
  236. })
  237. this.section.on('mouseleave', () => {
  238. this.isHovered = false;
  239. this.handleAutoplay();
  240. this.section.addClass("btnanimation");
  241. })
  242. this.initScrollCheck();
  243. this.handleAutoplay();
  244. }
  245. handleAutoplay() {
  246. if (this.dragging) {
  247. return false;
  248. }
  249. clearTimeout(this.autoplayTimeout);
  250. if (this.settings.autoplaySpeed > 0 && this.settings.autoplay) {
  251. this.autoplayTimeout = setTimeout(() => {
  252. if (this.inViewport) {
  253. this.onNext();
  254. } else {
  255. this.handleAutoplay();
  256. }
  257. }, this.settings.autoplaySpeed);
  258. }
  259. }
  260. initScrollCheck() {
  261. window.addEventListener("scroll", () => {
  262. window.requestAnimationFrame(() => {
  263. this.scrollCheck();
  264. })
  265. }, {passive: true});
  266. window.requestAnimationFrame(() => {
  267. this.scrollCheck();
  268. })
  269. }
  270. scrollCheck() {
  271. const w = $(window);
  272. if (w.scrollTop() + w.height() - 200 > this.section.offset().top && w.scrollTop() < this.section.offset().top + this.section.outerHeight() / 2) {
  273. if (!this.inViewport) {
  274. this.inViewport = true;
  275. this.section.addClass('in-viewport').trigger('in-viewport');
  276. }
  277. } else if (this.inViewport) {
  278. this.inViewport = false;
  279. this.section.removeClass('in-viewport');
  280. }
  281. }
  282. initHammerDragging() {
  283. const hammer = new Hammer(this.slideWrapper.get(0));
  284. let sliderWidth = 0;
  285. let startValue = 0;
  286. this.dragging = false;
  287. this.slideWrapper.find('img, a').attr('draggable', 'false').attr('ondragstart', 'return false;');
  288. hammer.on('panstart', (e) => {
  289. sliderWidth = this.slideOverflow.width();
  290. // Disable click on drag
  291. this.slideWrapper.find('a').css('pointer-events', 'none');
  292. this.dragging = true;
  293. startValue = this.slideWrapper.position().left;
  294. $(this.positionAnimation).stop();
  295. requestAnimationFrame(() => {
  296. $('html').addClass('slider-dragging');
  297. })
  298. if (e.center.x !== 0 && e.center.y !== 0) {
  299. clearTimeout(this.autoplayTimeout);
  300. }
  301. });
  302. hammer.on('pan', (e) => {
  303. if (e.center.x !== 0 && e.center.y !== 0) {
  304. const transformX = Math.round(startValue + e.deltaX);
  305. this.slideWrapper.css({'transform': 'translate3d(' + transformX + 'px, 0, 0)'});
  306. }
  307. });
  308. hammer.on('panend', (e) => {
  309. this.slideWrapper.find('a').addClass('dragging');
  310. this.dragging = false;
  311. requestAnimationFrame(() => {
  312. $('html').removeClass('slider-dragging');
  313. if (e.deltaX > sliderWidth / 5) {
  314. this.onPrev();
  315. } else if (e.deltaX < sliderWidth / -5) {
  316. this.onNext();
  317. } else {
  318. this.changeSlide(this.currentSlide);
  319. }
  320. });
  321. // Enable click on drag
  322. this.slideWrapper.find('a').css('pointer-events', 'all');
  323. });
  324. }
  325. initTabbing() {
  326. $(window).keydown((e) => {
  327. const code = (e.keyCode ? e.keyCode : e.which);
  328. if (code === 9 && this.slideWrapper.find(':focus').length) {
  329. this.tabScrollActive = true;
  330. this.resetTabScroll();
  331. }
  332. })
  333. $(window).keyup((e) => {
  334. const code = (e.keyCode ? e.keyCode : e.which);
  335. if (code === 9 && this.slideWrapper.find(':focus').length) {
  336. const slide = this.section.find(':focus').closest('.slide');
  337. this.slideOverflow.scrollLeft(0).scrollTop(0).find('.outer, .text-box').scrollLeft(0).scrollTop(0);
  338. this.goTo(parseInt(slide.attr('data-index')), true);
  339. this.tabScrollActive = false;
  340. }
  341. });
  342. }
  343. loadImage(index) {
  344. const t = this;
  345. if (!index) {
  346. index = this.currentSlide;
  347. }
  348. if (t.slides.eq(index).hasClass('preload') && !t.slides.eq(index).find('img').length) {
  349. const ib = t.slides.eq(index).find('.image-box');
  350. const img = $('<img/>').one('load', function (e) {
  351. const loadedImage = $(this).removeClass('loading');
  352. const slide = loadedImage.closest('.slide').removeClass('preload');
  353. const index = parseInt(slide.attr('data-index'));
  354. t.nextWrapper.find('[data-index="' + index + '"]').removeClass('preload').find('.image-box').append(loadedImage.clone());
  355. t.prevWrapper.find('[data-index="' + index + '"]').removeClass('preload').find('.image-box').append(loadedImage.clone());
  356. })
  357. img.attr('src', ib.attr('data-src')).appendTo(ib);
  358. img.attr('data-download', ib.attr('data-download')).appendTo(ib);
  359. img.attr('draggable', 'false').attr('ondragstart', 'return false;');
  360. }
  361. }
  362. resetTabScroll() {
  363. this.slideOverflow.scrollLeft(0).scrollTop(0).find('.outer, .text-box').scrollLeft(0).scrollTop(0);
  364. if (this.tabScrollActive) {
  365. window.requestAnimationFrame(() => {
  366. this.resetTabScroll();
  367. })
  368. }
  369. }
  370. scrollToTop() {
  371. const target = this.section.closest('.steps').offset().top - 120;
  372. const time = 600;
  373. $('html, body').animate({'scrollTop': target}, time, 'easeOutQuad');
  374. };
  375. setupSteps() {
  376. this.nextButton.text('Weiter').addClass('btn').addClass('has-icon').addClass('icon-right').addClass('icon-pfeil-rechts');
  377. }
  378. }
  379. export default IHKSlider;
  380. $('body').on('ihk-init dynamic-component-loaded gfi-dynamic-init', () => {
  381. $('.rotation .slider:not(.initiated), .steps .slider:not(.initiated)').each((i, el) => {
  382. const selector = $(el);
  383. if (!selector.find('dynamic-content').length) {
  384. new IHKSlider(selector);
  385. }
  386. })
  387. /*
  388. let slidersSelector;
  389. if (e.type === "ihk-init") {
  390. slidersSelector = $('.rotation .slider:not(.initiated):not(:has(dynamic-content)), .steps .slider:not(.initiated):not(:has(dynamic-content))')
  391. } else {
  392. slidersSelector = $('.rotation .slider:not(.initiated), .steps .slider:not(.initiated)');
  393. }
  394. slidersSelector.each(function () {
  395. new Slider($(this));
  396. });
  397. */
  398. });