点击查看html编辑器说明文档

JS + CSS 模拟时钟edit icon

|
|
Fork(复制)
|
|
作者:
lynn-ssk

👉 新版编辑器已上线,点击进行体验吧!

BUG反馈
嵌入
设置
下载
HTML
格式化
支持Emmet,输入 p 后按 Tab键试试吧!
<head> ...
展开
</head>
<body>
            
              <div class="clock">
      <div class="clock-face">
          <div class="hand hour-hand"></div>
          <div class="hand min-hand"></div>
          <div class="hand second-hand"></div>
      </div>
  </div>
        
</body>
CSS
格式化
            
            /**
    Style related to this page
    */
    
html {
    background: #018DED url(http://unsplash.it/1500/1000?image=881&blur=50);
    background-size: cover;
    font-family: 'helvetica neue';
    text-align: center;
    font-size: 10px;
}
body {
    font-size: 2rem;
    display: flex;
    flex: 1;
    min-height: 100vh;
    align-items: center;
}
.clock {
    width: 30rem;
    height: 30rem;
    border: 20px solid white;
    border-radius: 50%;
    margin: 50px auto;
    position: relative;
    padding: 2rem;
    box-shadow: 0 0 0 4px rgba(0, 0, 0, 0.1), inset 0 0 0 3px #EFEFEF, inset 0 0 10px black, 0 0 10px rgba(0, 0, 0, 0.2);
}
.clock-face {
    position: relative;
    width: 100%;
    height: 100%;
    transform: translateY(-3px);
    /* account for the height of the clock hands */
}
.hand {
    width: 50%;
    height: 6px;
    background: black;
    position: absolute;
    top: 50%;
    transform-origin: 100%;
    transform: rotate(90deg);
    transition: all 0.05s;
    transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1);
}
        
JS
格式化
            
            
  // Create references to the HTML elements that we want to transform
  const secondHand = document.querySelector('.second-hand');
  const minsHand = document.querySelector('.min-hand');
  const hourHand = document.querySelector('.hour-hand');

  const now = new Date();
  const seconds = now.getSeconds();
  const mins = now.getMinutes();
  const hour = now.getHours();

  // Set the degrees values on initial page load
  var secondsDegrees = ((360 / 60) * seconds) + 90;
  var minsDegrees = ((360 / 60) * mins) + 90;
  var hourDegrees = ((360 / 12) * hour) + 90;

  function setDate() {

      // Calculate the degree shift
      secondsDegrees += 6; // 360 deg/ 60s => per sec 6 degree change for second hand
      minsDegrees += 0.1; // 360 deg/ 60s/ 60m => per sec 0.1 degree change for min hand
      hourDegrees += 0.0083; // 360 deg/ 60s/ 60m / 12h => per sec 0.0083 degree change for hour hand

      // Apply the styling
      secondHand.style.transform = `rotate(${secondsDegrees}deg)`;
      minsHand.style.transform = `rotate(${minsDegrees}deg)`;
      hourHand.style.transform = `rotate(${hourDegrees}deg)`;

      // console.log(seconds, secondsDegrees, minsDegrees, hourDegrees);
  }

  // Call setDate function once every second
  setInterval(setDate, 1000);
  setDate();

        
预览
控制台
清空