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

466 строки
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. $('<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. // Disable click on drag
  279. this.slideWrapper.find('a').css('pointer-events', 'none');
  280. this.dragging = true;
  281. startValue = this.slideWrapper.position().left;
  282. $(this.positionAnimation).stop();
  283. requestAnimationFrame(() => {
  284. $('html').addClass('slider-dragging');
  285. })
  286. if (e.center.x !== 0 && e.center.y !== 0) {
  287. clearTimeout(this.autoplayTimeout);
  288. }
  289. });
  290. hammer.on('pan', (e) => {
  291. if (e.center.x !== 0 && e.center.y !== 0) {
  292. const transformX = Math.round(startValue + e.deltaX);
  293. this.slideWrapper.css({'transform': 'translate3d(' + transformX + 'px, 0, 0)'});
  294. }
  295. });
  296. hammer.on('panend', (e) => {
  297. this.slideWrapper.find('a').addClass('dragging');
  298. this.dragging = false;
  299. requestAnimationFrame(() => {
  300. $('html').removeClass('slider-dragging');
  301. if (e.deltaX > sliderWidth / 5) {
  302. this.onPrev();
  303. } else if (e.deltaX < sliderWidth / -5) {
  304. this.onNext();
  305. } else {
  306. this.changeSlide(this.currentSlide);
  307. }
  308. });
  309. // Enable click on drag
  310. this.slideWrapper.find('a').css('pointer-events', 'all');
  311. });
  312. }
  313. initTabbing() {
  314. $(window).keydown((e) => {
  315. const code = (e.keyCode ? e.keyCode : e.which);
  316. if (code === 9 && this.slideWrapper.find(':focus').length) {
  317. this.tabScrollActive = true;
  318. this.resetTabScroll();
  319. }
  320. })
  321. $(window).keyup((e) => {
  322. const code = (e.keyCode ? e.keyCode : e.which);
  323. if (code === 9 && this.slideWrapper.find(':focus').length) {
  324. const slide = this.section.find(':focus').closest('.slide');
  325. this.slideOverflow.scrollLeft(0).scrollTop(0).find('.outer, .text-box').scrollLeft(0).scrollTop(0);
  326. this.goTo(parseInt(slide.attr('data-index')), true);
  327. this.tabScrollActive = false;
  328. }
  329. });
  330. }
  331. loadImage(index) {
  332. const t = this;
  333. if (!index) {
  334. index = this.currentSlide;
  335. }
  336. if (t.slides.eq(index).hasClass('preload') && !t.slides.eq(index).find('img').length) {
  337. const ib = t.slides.eq(index).find('.image-box');
  338. const img = $('<img/>').one('load', function (e) {
  339. const loadedImage = $(this).removeClass('loading');
  340. const slide = loadedImage.closest('.slide').removeClass('preload');
  341. const index = parseInt(slide.attr('data-index'));
  342. t.nextWrapper.find('[data-index="' + index + '"]').removeClass('preload').find('.image-box').append(loadedImage.clone());
  343. t.prevWrapper.find('[data-index="' + index + '"]').removeClass('preload').find('.image-box').append(loadedImage.clone());
  344. })
  345. img.attr('src', ib.attr('data-src')).appendTo(ib);
  346. img.attr('data-download', ib.attr('data-download')).appendTo(ib);
  347. img.attr('draggable', 'false').attr('ondragstart', 'return false;');
  348. }
  349. }
  350. resetTabScroll() {
  351. this.slideOverflow.scrollLeft(0).scrollTop(0).find('.outer, .text-box').scrollLeft(0).scrollTop(0);
  352. if (this.tabScrollActive) {
  353. window.requestAnimationFrame(() => {
  354. this.resetTabScroll();
  355. })
  356. }
  357. }
  358. scrollToTop() {
  359. const target = this.section.closest('.steps').offset().top - 120;
  360. const time = 600;
  361. $('html, body').animate({'scrollTop': target}, time, 'easeOutQuad');
  362. };
  363. setupSteps() {
  364. this.nextButton.text('Weiter').addClass('btn').addClass('has-icon').addClass('icon-right').addClass('icon-pfeil-rechts');
  365. }
  366. }
  367. export default IHKSlider;
  368. $('body').on('ihk-init dynamic-component-loaded gfi-dynamic-init', () => {
  369. $('.rotation .slider:not(.initiated), .steps .slider:not(.initiated)').each((i, el) => {
  370. const selector = $(el);
  371. if (!selector.find('dynamic-content').length) {
  372. new IHKSlider(selector);
  373. }
  374. })
  375. /*
  376. let slidersSelector;
  377. if (e.type === "ihk-init") {
  378. slidersSelector = $('.rotation .slider:not(.initiated):not(:has(dynamic-content)), .steps .slider:not(.initiated):not(:has(dynamic-content))')
  379. } else {
  380. slidersSelector = $('.rotation .slider:not(.initiated), .steps .slider:not(.initiated)');
  381. }
  382. slidersSelector.each(function () {
  383. new Slider($(this));
  384. });
  385. */
  386. });