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.
 
 
 
 

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