Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

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