Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

85 rader
2.2 KiB

  1. import $ from 'jquery';
  2. import 'jquery.easing';
  3. import Slider from "../slider/slider";
  4. class IHKQuickFacts {
  5. constructor(section) {
  6. this.section = section.addClass('initiated');
  7. this.initSlides();
  8. this.slider = new Slider(section.find('.slider'));
  9. this.slider.section.on('in-viewport slide-change', () => {
  10. const curr = this.slider.currentSlide;
  11. if (this.slides[curr].hasCounter) {
  12. this.startCounter(curr);
  13. }
  14. })
  15. }
  16. initSlides() {
  17. this.slides = [];
  18. this.section.find('.slider').children().each((i, element) => {
  19. const span = $(element).find('.counter');
  20. const c = {};
  21. c.hasCounter = span.length === 1;
  22. if (c.hasCounter) {
  23. c.element = span;
  24. c.from = parseFloat(span.attr('data-from'));
  25. c.to = parseFloat(span.attr('data-to'));
  26. c.duration = parseFloat(span.attr('data-duration')) * 1000;
  27. c.decimals = parseFloat(span.attr('data-decimals'));
  28. c.animation = $({num: c.from});
  29. }
  30. this.slides.push(c);
  31. })
  32. }
  33. startCounter(index) {
  34. const t = this;
  35. const c = this.slides[index];
  36. c.stringLength = 0;
  37. c.animation.stop();
  38. c.animation = $({num: c.from});
  39. t.setText(c, t.formatNumber(c.from, c.decimals));
  40. c.animation.animate({num: c.to}, {
  41. duration: c.duration,
  42. easing: 'easeOutCubic',
  43. step(num) {
  44. t.setText(c, t.formatNumber(num, c.decimals));
  45. },
  46. complete() {
  47. t.setText(c, t.formatNumber(c.to, c.decimals));
  48. }
  49. })
  50. }
  51. setText(counter, str) {
  52. if (str.length !== counter.stringLength) {
  53. counter.element.css('width', 'auto');
  54. counter.element.text(str);
  55. counter.element.css('width', Math.round(counter.element.outerWidth()) + 'px');
  56. counter.stringLength = str.length;
  57. }
  58. else {
  59. counter.element.text(str);
  60. }
  61. }
  62. formatNumber(num, decimals) {
  63. return new Intl.NumberFormat('de-DE',{
  64. minimumFractionDigits: decimals,
  65. maximumFractionDigits: decimals,
  66. }).format(num);
  67. }
  68. }
  69. export default IHKQuickFacts;
  70. $('body').on('ihk-init dynamic-component-loaded gfi-dynamic-init', function () {
  71. $('.quick-facts:not(.initiated)').each(function() {
  72. new IHKQuickFacts($(this));
  73. });
  74. });