js+cs 3シンプルクロック効果を実現


本論文の例では、js+css 3の簡単なクロックを実現するための具体的なコードを共有します。
1.時計の特殊効果を実現しました。回転できます。時間が正確で、画面が綺麗で、大気が綺麗です。
2.cs 3を使ったtranform:rotate、tranform-origgin:疑似元素、border-radius、位置付け、z-indxなど
効果は図の通りです

コードは以下の通りです

<!DOCTYPE html>
<html>

 <head>
 <meta charset="UTF-8">
 <title>CSS3    </title>
 <link rel="shortcut icon" type="image/x-icon" href="img/an.ico" />
 <style>
  /*    */
  
  .clock {
  /*      */
  width: 400px;
  height: 400px;
  position: relative;
  margin: 40px auto;
  /*   */
  border-radius: 50%;
  /*  */
  box-shadow: 5px 5px 20px rgba(0, 0, 0, 0.5);
  /*    */
  background: #F5DEB3;
  border: 10px solid #FFFF00;
  }
  /*      */
  
  .box {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  }
  /*         div*/
  
  .box div {
  width: 0px;
  height: 200px;
  position: absolute;
  left: 200px;
  /*  */
  transform: rotate(0deg);
  /*        */
  transform-origin: bottom right;
  background: rgba(255, 0, 0, 0.5);
  }
  /*  */
  
  .box div i {
  float: left;
  margin-top: 20px;
  margin-left: -10px;
  font-style: normal;
  width: 20px;
  text-align: center;
  font-style: 18px;
  }
  /*   */
  
  .box div::after {
  content: "";
  position: absolute;
  background: #484848;
  width: 2px;
  height: 10px;
  left: -1px;
  }
  /*   */
  
  .box div.five::after {
  position: absolute;
  content: "";
  width: 4px;
  height: 20px;
  left: -2px;
  top: 0;
  background: #484848;
  border-radius: 0 0 2px 2px;
  }
  /*    */
  
  .second {
  width: 1px;
  height: 200px;
  background: red;
  position: absolute;
  left: 200px;
  /*        */
  margin-top: 30px;
  z-index: 10;
  transform: rotate(0deg);
  transform-origin: center 170px;
  /*      */
  }
  /*    */
  
  .second::after {
  content: "";
  position: absolute;
  width: 20px;
  height: 20px;
  background: red;
  border-radius: 50%;
  bottom: 20px;
  left: -10px;
  }
  /*    */
  .minute {
  width: 2px;
  height: 140px;
  background: #8b8b8d;
  position: absolute;
  left: 199px;
  margin-top: 60px;
  z-index: 9;
  transform-origin: center bottom;
  transform: rotate(12deg);
  animation: minute 60s linear infinite;
  }
  /*    */
  
  .hour {
  width: 6px;
  height: 100px;
  background: #333;
  position: absolute;
  left: 197px;
  margin-top: 100px;
  z-index: 8;
  border-radius: 3px;
  transform: rotate(2deg);
  transform-origin: center bottom;
  animation: minute 60s linear infinite;
  }
 </style>
 </head>

 <body>
 <div class="clock">
  <div class="box"></div>
  <div class="second"></div>
  <div class="minute"></div>
  <div class="hour"></div>
 </div>

 <script>
  var box = document.getElementsByClassName("box")[0];
  var ssObj = document.getElementsByClassName("second")[0];
  var mmObj = document.getElementsByClassName("minute")[0];
  var hhObj = document.getElementsByClassName("hour")[0];
  /*      */
  var date = new Date();
  var hh = date.getHours();
  var mm = date.getMinutes();
  var ss = date.getSeconds();
  /*            */
  hhDeg = 360 * (hh % 12) / 12;
  mmDeg = 360 * mm / 60;
  ssDeg = 360 * ss / 60;
  hhObj.style.transform = "rotate(" + hhDeg + "deg)";
  mmObj.style.transform = "rotate(" + mmDeg + "deg)";
  ssObj.style.transform = "rotate(" + ssDeg + "deg)";
      //          
  var Deg = 0;
  /*   */
  for (var i = 0; i < 60; i++) {
  var div1 = document.createElement("div"); //    div
  var hourNum = i / 5;
  //  5 
  if (hourNum == 0) hourNum = 12;
  if (i % 5 == 0) { //   
   div1.className = "five";
   div1.innerHTML = "<i>" + hourNum + "</i>"
  }
  div1.style.transform = "rotate(" + Deg + "deg)";
  box.appendChild(div1);
  Deg += 6;//         6 
  }
  /*       */
  function drawSS() {
  //      
  ssDeg = 360 * ss / 60;
  //      
  mmDeg1 = 360 * mm / 60;
  //      
  hhDeg1 = 360 * (hh % 12) / 12;
  //         
  mmDeg = mmDeg1 + (6 * ss / 60);
  //          
  hhDeg = hhDeg1 + (30 * mm / 60);
  
  hhObj.style.transform = "rotate(" + hhDeg + "deg)";
  mmObj.style.transform = "rotate(" + mmDeg + "deg)";
  ssObj.style.transform = "rotate(" + ssDeg + "deg)";
  ss += 1;
  if (ss > 60) {
   ss = 1;
   mm += 1;
  }
  if (mm == 60) {
   mm = 0;
   hh += 1;
  }
  setTimeout(function() {
   drawSS();
  }, 1000);
  }
  drawSS();
 </script>
 </body>

</html>
縮小版:

<!DOCTYPE html>
<html>

 <head>
 <meta charset="UTF-8">
 <title>CSS3    </title>
 <link rel="shortcut icon" type="image/x-icon" href="img/an.ico" />
 <style>
  /*    */
  
  .clock {
  /*      */
  width: 400px;
  height: 400px;
  position: relative;
  margin: 40px auto;
  /*   */
  border-radius: 50%;
  /*  */
  box-shadow: 5px 5px 20px rgba(0, 0, 0, 0.5);
  /*    */
  background: #F5DEB3;
  border: 10px solid #FFFF00;
  }
  /*      */
  
  .box {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  }
  /*      div*/
  
  .box div {
  width: 0px;
  height: 200px;
  position: absolute;
  left: 200px;
  /*  */
  transform: rotate(0deg);
  /*        */
  transform-origin: bottom right;
  background: rgba(255, 0, 0, 0.5);
  }
  /*   */
  
  .box div:after {
  content: "";
  position: absolute;
  background: #484848;
  width: 2px;
  height: 10px;
  left: -1px;
  }
  /*   */
  
  .box div.five:after {
  position: absolute;
  content: "";
  width: 4px;
  height: 20px;
  left: -2px;
  top: 0;
  background: #484848;
  border-radius: 0 0 2px 2px;
  }
  /*    */
  
  .second {
  width: 1px;
  height: 200px;
  background: red;
  position: absolute;
  left: 200px;
  /*        */
  margin-top: 30px;
  z-index: 10;
  transform: rotate(0deg);
  transform-origin: center 170px;
  /*      */
  }
  /*    */
  
  .second:after {
  content: "";
  position: absolute;
  width: 20px;
  height: 20px;
  background: red;
  border-radius: 50%;
  bottom: 20px;
  left: -10px;
  }
  /*    */
  .minute {
  width: 2px;
  height: 140px;
  background: #8b8b8d;
  position: absolute;
  left: 199px;
  margin-top: 60px;
  z-index: 9;
  transform-origin: center bottom;
  transform: rotate(12deg);
  }
  /*    */
  
  .hour {
  width: 6px;
  height: 100px;
  background: #333;
  position: absolute;
  left: 197px;
  margin-top: 100px;
  z-index: 8;
  border-radius: 3px;
  transform: rotate(2deg);
  transform-origin: center bottom;
  }
 </style>
 </head>

 <body>
 <div class="clock">
  <div class="box"></div>
  <div class="second"></div>
  <div class="minute"></div>
  <div class="hour"></div>
 </div>

 <script>
  var box = document.getElementsByClassName("box")[0];
  var ssObj = document.getElementsByClassName("second")[0];
  var mmObj = document.getElementsByClassName("minute")[0];
  var hhObj = document.getElementsByClassName("hour")[0];
  /*      */
  var date = new Date();
  var hh = date.getHours();
  var mm = date.getMinutes();
  var ss = date.getSeconds();
  /*            */
  drawSS();
      //          
  var Deg = 0;
  /*   */
  for (var i = 0; i < 60; i++) {
  var div1 = document.createElement("div"); //    div
  //  5 
  if (i % 5 == 0) { //   
   div1.className = "five";
  }
  div1.style.transform = "rotate(" + Deg + "deg)";
  box.appendChild(div1);
  Deg += 6;//         6 
  }
  /*       */
  function drawSS() {
  //      
  ssDeg = 360 * ss / 60;
  //      
  mmDeg = 360 * mm / 60 + (6 * ss / 60);
  //      
  hhDeg = 360 * (hh % 12) / 12 + (30 * mm / 60);
  //   
  hhObj.style.transform = "rotate(" + hhDeg + "deg)";
  mmObj.style.transform = "rotate(" + mmDeg + "deg)";
  ssObj.style.transform = "rotate(" + ssDeg + "deg)";
  ss += 1;
  //        
  setTimeout(function() {
   drawSS();
  }, 1000);
  }
  
 </script>
 </body>

</html>
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。