Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

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