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.
 
 
 
 

86 line
2.2 KiB

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