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

              
                <div class="clock">
  <div class="clock__inner">
    <div class="scale">
<!--       <div class="scale__unit"></div> -->
    </div>
    <div class="hand hand-hour"></div>
    <div class="hand hand-min"></div>
    <div class="hand hand-sec">
      <div class="dec dec1"></div>
      <div class="dec dec2"></div>
      <div class="dec dec3"></div>
      <div class="dec dec4"></div>
      <div class="dec dec5">
        <div class="dot"></div>
      </div>
  </div>
</div>
              
            
!

CSS

              
                %positionCenter {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
@mixin flex($justify-content, $align-item) {
  display: flex;
  justify-content: $justify-content;
  align-items: $align-item;
}

body {
  color: #fff;
  background-color: #cbbcf6;
}

.clock {
  background-color: #a06ee1;
  width: 350px;
  height: 350px;
  border-radius: 100%;
  box-shadow: 8px 8px 15px #0000007d;
  @extend %positionCenter;

  &__inner {
    background-color: #421b9b;
    border: 2px solid #212F0B;
    width: 90%;
    height: 90%;
    border-radius: 100%;
    position: relative;
    @extend %positionCenter;
  }
  .scale {
    position: relative;
    width: 100%;
    height: 100%;
    &__unit {
      // transform: rotate(5deg) translateY(115px);
      background-color: #fff;
      width: 2px;
      height: 2px;
      border-radius: 100%;
      position: absolute;
      top: 50%;
      left: 50%;
      transform-origin: 0 0;
      // 小時刻度
      &:nth-child(6n+1) {
        height: 24px;
        width: .5px;
        background-color: #CEF9E2;
      }
      &:nth-child(6n+4) {
        background-color: transparent;
        &::before {
          content: '★';
          display: block;
          position: relative;
          left: -3px;
          top: -7px;
          color: #CEF9E2;
          font-size: 2px;
        }
      }
    }
    .twenty, .twelve {
      font-size: 10px;
      position: absolute;
      left: -5px;
    }
    .twenty {
      top: 25px;
    }
    .twelve {
      top: -20px;
    }
  }
  .hand {
    position: absolute;
    top: 49%;
    left: 49%;
    transform-origin: 50% 4px;
  }
  .hand-hour {
      transform: rotate(-45deg);
      width: 8px;
      height: 65px;
      background-color: #fff;
      border-radius: 2px;
      &::before {
        content: '';
        display: block;
        background-color: #421b9b;
        width: 3px;
        height: 20px;
        position: absolute;
        left: 50%;
        bottom: 5px;
        transform: translateX(-50%);
      }
  }
  .hand-min {
    transform: rotate(180deg);
    width: 8px;
    height: 80px;
    background-color: #CEF9E2;
    border-radius: 4px;
    &::after {
      content: '';
      display: block;
      position: absolute;
      left: 50%;
      top: 5px;
      transform: translate(-50%, -50%);
      width: 4px;
      height: 4px;
      border-radius: 100%;
      background-color: #000;
    }
    &::before {
      content: '';
      display: block;
      height: 30px;
      width: 1px;
      background-color: #000;
      position: absolute;
      left: 50%;
      top: 5px;
      transform: translateX(-50%);
    }
  }
  .hand-sec {
    transform: rotate(90deg);
    width: 10px;
    height: 100px;
    z-index: -1;
    .dec {
      background-color: #cbbcf6;
      position: absolute;
      width: 1px;
    }
    .dec1 {
      height: 30px;
      left: 50%;
      top: 0;
      transform: translateX(-50%);
    }
    .dec2 {
      height: 10px;
      left: 25%;
      top: 29px;
      transform: rotate(25deg);
    }
    .dec3 {
      height: 20px;
      left: 45%;
      top: 37px;
      transform: rotate(-24deg);
    }
    .dec4 {
      height: 13px;
      left: 68%;
      top: 55px;
      transform: rotate(16deg);
    }
    .dec5 {
      height: 40px;
      left: 53%;
      top: 67px;
      .dot {
        width: 8px;
        height: 8px;
        border-radius: 100%;
        border: .5px solid #cbbcf6;
        position: absolute;
        top: 39px;
        left: -3px;
        @include flex(center, center);
        &::before {
          content: '';
          display: block;
          width: 3px;
          height: 3px;
          border-radius: 100%;
          background-color: #b1ff00;
        }
      }
    }
  }
}
              
            
!

JS

              
                // 渲染刻度和時間數字畫面
function renderClockUnit() {
  let deg = 180; //刻度角度,180度為12時的位置
  let time_deg = 180; // 數字角度
  let twelve_hours = 0; // 十二時制
  let twenty_hours = 12; // 二十四時制

  for (let i = 0; i<72; i++) {
    const unit_wrap = document.querySelector('.scale'); //刻度父元素
    const unit = document.createElement('div'); // 創造刻度DOM
    unit.classList.add('scale__unit');

    // 小時的刻度處理
    if(i%6 == 0) {
      unit.style.transform = `rotate(${deg}deg) translateY(110px)`;
      unit.innerHTML = `
      <span class="twelve" style="transform:rotate(${time_deg}deg)">${i==0?12:twelve_hours}</span>
      <span class="twenty" style="transform:rotate(${time_deg}deg)">${i==0?24:twenty_hours}</span>
      `;
      twelve_hours++;
      twenty_hours++;
      time_deg -= 30; // 每一小刻度為5度,每小時間隔六個刻度,因此需減去6*5=30將數字轉正
    // 小刻度處理
    } else {
      unit.style.transform = `rotate(${deg}deg) translateY(120px)`;
    }
    deg += 5;
    unit_wrap.appendChild(unit);
  }
}

// 時鐘運作
function clockPrimaryFeature() {
    // 選取指針DOM
    const hour_hand = document.querySelector('.hand-hour');
    const min_hand = document.querySelector('.hand-min');
    const sec_hand = document.querySelector('.hand-sec');
    // 抓時間
    const time = new Date();
    const hour = time.getHours();
    const min = time.getMinutes();
    const sec = time.getSeconds();
    // 計算指針角度
    const hour_deg = (hour*30 + min*30/60) - 180;
    const min_deg = min*6 + sec*6/60 - 180;
    const sec_deg = sec*6 - 180;

    hour_hand.style.transform = `rotate(${hour_deg}deg)`;
    min_hand.style.transform = `rotate(${min_deg}deg)`;
    sec_hand.style.transform = `rotate(${sec_deg}deg)`;
}

function runClock(callback){
  renderClockUnit();
  callback();
  setInterval(callback, 1000);
}

runClock(clockPrimaryFeature);
              
            
!
999px

Console