Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container overflow-hidden text-center">
  <div class="row my-5">
    <div class="col-12">
      <h1 class="display-3">
        PureJS Counter<br />
        <small class="display-6">
          ...scroll down to start the counter...
        </small>
      </h1>
    </div>
  </div>
  <div class="row extra-height row-cols-2 row-cols-lg-5 g-2 g-lg-3">
    <div class="col-4 bg-info text-white border d-flex align-items-center justify-content-center">
      <h2 class="text-center" 
          data-counter="true"
          data-counter-after="<small>%</small>" 
          data-counter-step="0" 
          data-counter-start="0"
          >
        100
      </h2>
    </div>
    <div class="col-4 bg-info text-white border d-flex align-items-center justify-content-center">
      <h2 class="text-center" 
          data-counter="true" 
          data-counter-after="<small>$</small>" 
          data-counter-step="0" 
          data-counter-start="0"
          >
        100
      </h2>
    </div>
    <div class="col-4 bg-info text-white border d-flex align-items-center justify-content-center">
      <h2 class="text-center" 
          data-counter="true" 
          data-counter-before="$" 
          data-counter-step="0" 
          data-counter-start="0"
          >
        100
      </h2>
    </div>
  </div>

  <div class="row extra-height row-cols-2 row-cols-lg-5 g-2 g-lg-3">
    <div class="col-4 bg-warning text-white border d-flex align-items-center justify-content-center">
      <h2 class="text-center" 
          data-counter="true" 
          data-counter-after="<small>%</small>" 
          data-counter-step="0" 
          data-counter-start="0"
          >
        100
      </h2>
    </div>
    <div class="col-4 bg-warning text-white border d-flex align-items-center justify-content-center">
      <h2 class="text-center" 
          data-counter="true" 
          data-counter-after="<small>$</small>" 
          data-counter-step="0" 
          data-counter-start="0"
          >
        100
      </h2>
    </div>
    <div class="col-4 bg-warning text-white border d-flex align-items-center justify-content-center">
      <h2 class="text-center" 
          data-counter="true" 
          data-counter-before="$" 
          data-counter-step="0" 
          data-counter-start="0"
          >
        100
      </h2>
    </div>
  </div>

  <div class="row extra-height row-cols-2 row-cols-lg-5 g-2 g-lg-3">
    <div class="col-4 bg-success text-white border d-flex align-items-center justify-content-center">
      <h2 class="text-center" 
          data-counter="true" 
          data-counter-after="<small>%</small>" 
          data-counter-step="0" 
          data-counter-start="0"
          >
        100
      </h2>
    </div>
    <div class="col-4 bg-success text-white border d-flex align-items-center justify-content-center">
      <h2 class="text-center" 
          data-counter="true" 
          data-counter-after="<small>$</small>" 
          data-counter-step="0"
          data-counter-start="0"
          >
        100
      </h2>
    </div>
    <div class="col-4 bg-success text-white border d-flex align-items-center justify-content-center">
      <h2 class="text-center" 
          data-counter="true" 
          data-counter-before="$" 
          data-counter-step="0" 
          data-counter-start="0"
          >
        100
      </h2>
    </div>
  </div>
  <div class="row extra-height row-cols-2 row-cols-lg-5 g-2 g-lg-3">
    <div class="col-4 bg-secondary text-white border d-flex align-items-center justify-content-center">
      <h2 class="text-center" 
          data-counter="true"
          data-counter-after="<small>%</small>" 
          data-counter-step="0" 
          data-counter-start="0"
          >
        100
      </h2>
    </div>
    <div class="col-4 bg-secondary text-white border d-flex align-items-center justify-content-center">
      <h2 class="text-center" 
          data-counter="true" 
          data-counter-after="<small>$</small>" 
          data-counter-step="0" 
          data-counter-start="0"
          >
        100
      </h2>
    </div>
    <div class="col-4 bg-secondary text-white border d-flex align-items-center justify-content-center">
      <h2 class="text-center" 
          data-counter="true" 
          data-counter-before="$" 
          data-counter-step="0" 
          data-counter-start="0"
          >
        100
      </h2>
    </div>
  </div>

</div>
              
            
!

CSS

              
                .extra-height {
  height: 800px;
}
h2 {
  font-size: 4rem;
}

.bg-info, .bg-warning, .bg-success, .bg-secondary {
  background-size: cover;
  background-position: center center;
  background-repeat: no-repeat;
}

.bg-info {
  background-image:  url(https://img.freepik.com/darmowe-zdjecie/artystyczne-rozmyte-kolorowe-tapety-tlo_58702-8663.jpg);
}

.bg-warning {
  background-image:  url(https://img.freepik.com/darmowe-zdjecie/streszczenie-rozowa-mgla-z-bitami_23-2148101934.jpg);
}

.bg-success {
  background-image:  url(https://img.freepik.com/darmowe-zdjecie/vintage-grunge-niebieski-beton-tekstura-tlo-sciany-z-winiety_1258-28373.jpg);
}

.bg-secondary {
  background-image:  url(https://img.freepik.com/darmowe-zdjecie/stare-czarne-tlo-grunge-tekstur-tablica-szkolna-tablica-beton_1258-52289.jpg);
}
              
            
!

JS

              
                document.addEventListener("DOMContentLoaded", function() {
  function counterStartUp(element) {
    let start = parseInt(element.getAttribute("data-counter-start")) || 0;
    let end = parseInt(element.textContent) || 100;
    let step = parseInt(element.getAttribute("data-counter-step")) || 1;
    let counterAfter = element.getAttribute("data-counter-after") || "";
    let counterBefore = element.getAttribute("data-counter-before") || "";
    let duration = parseInt(element.getAttribute("data-counter-duration")) || 4000; 
    let interval = Math.ceil(duration / (end - start) * step);

    // Counter animation function
    let counter = start;
    let intervalId = setInterval(function() {
      counter += step;
      if (counter >= end) {
        counter = end; 
        clearInterval(intervalId);
        element.innerHTML = counterBefore + counter + counterAfter; 
      } else {
        element.innerHTML = counterBefore + counter + counterAfter;
      }
    }, interval);
  }
  
    // Intersection Observer to trigger counterStartUp when element enters the viewport
  const observer = new IntersectionObserver((entries) => {
    entries.forEach(entry => {
      if (entry.isIntersecting) {
        counterStartUp(entry.target);
        observer.unobserve(entry.target);
      }
    });
  }, { threshold: 0.5 }); // Trigger when 50% of the element is visible

  // Select elements with data-counter="true" and observe them
  document.querySelectorAll('[data-counter="true"]').forEach(function(element) {
    observer.observe(element);
  });
});
              
            
!
999px

Console