Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

Save Automatically?

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

              
                <style>
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@100,800&display=swap');
</style>

<div>
<h1>Animated JS Countdown</h1>
  <p>No extra elements needed - it a uses :before pseudo element and a data attribute to set the next number in the transition</p>
</div>

<div class="countdown">
  <div class="col">
    <p id="hoursEl" class="time">
      <span>0</span>
      <span>1</span>
    </p>
    <p>Hours</p>
  </div>
  <div class="col">
    <p id="minutesEl" class="time">
      <span>0</span>
      <span>0</span>
    </p>
    <p>Minutes</p>
  </div>
  <div class="col">
    <p id="secondsEl" class="time">
      <span>0</span>
      <span>5</span>
    </p>
    <p>Seconds</p>
  </div>
</div>

<p>&lt;/&gt; by <a href="https://www.djdmorrison.co.uk" target="_blank">DJDMorrison</p>
              
            
!

CSS

              
                .countdown {
  display: flex;
  overflow: hidden;
  font-weight: bold;
  .col {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-right: 2em;
    .time {
      display: flex;
      overflow: hidden;
      font-size: 5em;
      span {
        position: relative;
        &:before {
          content: attr(data-nextvalue);
          position: absolute;
          top: -80px;
          z-index: -1;
        }
        &.moving {
          transition: transform 500ms ease;
          transform: translateY(80px);
        }
      }
    }
  }
}

// Presentation CSS
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: 'Roboto', sans-serif;
  font-weight: 100;
  text-align: center;
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
  align-items: center;
  height: 100vh;
  width: 100%;
}

h1 {
  font-weight: 100;
  margin-bottom: 10px;
}

p {
  margin: 0;
}
              
            
!

JS

              
                const goalTime = Date.now() + 3605500;
const hoursEl = document.getElementById("hoursEl");
const minutesEl = document.getElementById("minutesEl");
const secondsEl = document.getElementById("secondsEl");

updateCountdown();
setInterval(() => {
  updateCountdown();
}, 1000);

function updateCountdown() {
  const currentTime = Date.now();

  const diff = goalTime - currentTime;

  const hoursDiff = Math.floor((diff - 60) / (60 * 60 * 1000));
  const minutesDiff = Math.floor((diff - 60) / (60 * 1000)) % 60;
  const secondsDiff = Math.floor(diff / 1000) % 60;

  updateElement(hoursEl, hoursDiff);
  updateElement(minutesEl, minutesDiff);
  updateElement(secondsEl, secondsDiff);
}

function updateElement(element, nextValue) {
  const [firstDigitEl, secondDigitEl] = element.children;
  nextValue = nextValue.toString().padStart(2, "0");

  const firstDigitNextValue = nextValue.substr(0, 1);
  const secondDigitNextValue = nextValue.substr(1, 1);

  firstDigitEl.dataset.nextvalue = firstDigitNextValue;
  secondDigitEl.dataset.nextvalue = secondDigitNextValue;

  if (firstDigitNextValue !== firstDigitEl.innerHTML) {
    this.animateNumber(firstDigitEl, firstDigitNextValue);
  }

  if (secondDigitNextValue !== secondDigitEl.innerHTML) {
    this.animateNumber(secondDigitEl, secondDigitNextValue);
  }
}

function animateNumber(element, nextValue,) {
  element.classList.add("moving");

  setTimeout(() => {
    element.classList.remove("moving");
    element.innerHTML = nextValue;
  }, 500);
}

              
            
!
999px

Console