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="clock">
  <div class="hour-marks">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
  </div>
  <div class="hands">
    <div class="hand hour no-transition"></div>
    <div class="hand minute no-transition"></div>
    <div class="hand second no-transition"></div>
  </div>
</div>
              
            
!

CSS

              
                $color-dark-blue: #2c3e50;
$color-light-gray: #ecf0f1;
$color-yellow: #f1c40f;
$color-concrete: #95a5a6;
$color-red: #e74c3c;

$shadow-color: rgba(0, 0, 0, 0.03);

$clock-height: 70vh;
$clock-width: $clock-height;

body, html {
  width: 100%;
  height: 100%;
}

body {
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: $color-light-gray;
}

.clock {
  overflow: hidden;
  position: relative;
  width: $clock-width;
  height: $clock-height;
  background-color: $color-light-gray;
  border: 2vh solid $color-yellow;
  border-radius: 4vh;
  box-shadow: 
    1vh 1vh 0 $shadow-color,
    1.5vh 1.5vh 0 $shadow-color,
    2vh 2vh 0 $shadow-color, 
    2.5vh 2.5vh 0 $shadow-color, 
    3vh 3vh 0 $shadow-color, 
    3.5vh 3.5vh 0 $shadow-color, 
    4vh 4vh 0 $shadow-color, 
    5vh 5vh 0 $shadow-color, 
    6vh 6vh 0 $shadow-color,
    8vh 8vh 1vh $shadow-color,
    10vh 10vh 1vh $shadow-color,
    13vh 13vh 2vh $shadow-color,
    16vh 16vh 4vh $shadow-color,
    20vh 20vh 6vh $shadow-color,
    30vh 30vh 8vh $shadow-color,
    40vh 40vh 10vh $shadow-color;
  
  &:before {
    content: "";
    width: 150%;
    height: 150%;
    background-color: rgba(255, 255, 255, 0.7);
    position: absolute;
    border-radius: 0 0 100% 0;
    top: -40%;
    left: -40%;
  }
}

.hour-marks {
  width: 100%;
  height: 100%;
  position: relative;
  
  div {
    background-color: $color-concrete;
    width: 1vh;
    height: 7vh;
    position: absolute;
    transform-origin: $clock-width/2 $clock-width/2;
    
    @for $i from 1 through 12 {
      &:nth-child(#{$i}) {
        transform: rotateZ(30deg * $i) translate($clock-width/2 - 0.5, 2vh);
      }
    }
  }
}

.hands {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  
  .hand {
    position: absolute;
    
    &.no-transition {
      transition: none !important;
    }
    
    &.second {
      transition: transform 1s linear;
      width: 0.5vh;
      height: $clock-height/2;
      background-color: $color-red;
      left: $clock-height/2 - 0.25;
      top: 5vh;
      transform-origin: .25vh 30vh;
    }
    
    &.minute {
      transition: transform 0.2s ease-out;
      width: 1vh;
      height: $clock-height/2;
      background-color: $color-dark-blue;
      left: $clock-height/2 - 0.5;
      top: 5vh;
      transform-origin: 0.5vh 30vh;
    }
    
    &.hour {
      transition: transform 0.2s ease-out;
      width: 1vh;
      height: $clock-height/2 - 10vh;
      background-color: $color-dark-blue;
      left: $clock-height/2 - 0.5;
      top: 15vh;
      transform-origin: 0.5vh 20vh;
    }
  }
}


              
            
!

JS

              
                const hands = {
  second: document.querySelector('.hand.second'),
  minute: document.querySelector('.hand.minute'),
  hour: document.querySelector('.hand.hour')
};

let prevTime = new Date();

const rotate = (el, deg) => {
  el.style.transform = `rotate(${deg}deg)`;
};

const resetCycle = el => {
  rotate(el, 360);
  setTimeout(() => {
    el.classList.add('no-transition');
    rotate(el, 0);
  },900);
}

const updateHand = (el, value, denominator) => {
  el.classList.remove('no-transition');
  if(value === 0) {
    resetCycle(el);
  }
  else {
    rotate(el, (360/denominator)*value);
  }
}

const updateSeconds = () => {
  const date = new Date();
  if(prevTime.getSeconds() !== date.getSeconds()) {
    updateHand(hands.second, date.getSeconds(), 60);
  }
};

const updateMinutes = () => {
  const date = new Date();
  if(prevTime.getMinutes() !== date.getMinutes()) {
    updateHand(hands.minute, date.getMinutes(), 60);
  }
};

const updateHours = () => {
  const date = new Date();
  if(prevTime.getHours() !== date.getHours()) {
    updateHand(hands.hour, date.getHours(), 12);
  }
};

const updateTime = () => {
  setTimeout(() => {
    const d = new Date();
    
    updateSeconds();
    updateMinutes();
    updateHours();
    
    prevTime = d;
    updateTime();
  }, 50);
};

// Set the hands initial rotation (without a transition)
rotate(hands.second, (360/60)*prevTime.getSeconds());
rotate(hands.minute, (360/60)*prevTime.getMinutes());
rotate(hands.hour, (360/12/60)*(prevTime.getHours()*60 + prevTime.getMinutes()));

// Begin updating the time
updateTime();
              
            
!
999px

Console