javascript - 如何在 CSS 中制作时钟并在 JS 中工作

标签 javascript html css sass

enter image description here

我想在上面的图片中制作时钟,但我也做不到,因为我的 CSS 效率低下。任何人都可以帮助我制作它,或者指导我完成制作吗?

我试过这个:

.try {
  background-color: #CDCDCD;
  border-radius: 150px;
  width: 300px;
  height: 300px;
}

.clc {
  display: inline-block;
  position: relative;
  bottom: 75px;
  left: 63px;
}

.cl {
  display: block;
  line-height: 220px;
  font-size: xx-large;
}
<div class="try">
  <div class="clc"><span class="cl">10</span><span class="cl">8</span></div>
  <div class="clc"><span class="cl">11</span><span class="cl">7</span></div>
  <div class="clc"><span class="cl">12</span><span class="cl">6</span></div>
  <div class="clc"><span class="cl">1</span><span class="cl">5</span></div>
  <div class="clc"><span class="cl">2</span><span class="cl">4</span></div>
  <div class="clc"><span class="cl">3</span><span class="cl">9</span></div>

</div>

最佳答案

一种方法是使用 svg 和 rotate 属性。这是一个很好的方法。

var fixHands = function () {
  var d = new Date()
  var t = Math.floor((d.getTime() - d.getTimezoneOffset() * 60000) / 1000);

  var h = t % (12 * 3600) / 120;
  var n = t % 3600 / 10;
  var s = t % 60 * 6;
  
  document.getElementById('hour').setAttribute('transform', 'rotate(' + h + ' 50 50)');
  document.getElementById('minute').setAttribute('transform', 'rotate(' + n + ' 50 50)');
  document.getElementById('second').setAttribute('transform', 'rotate(' + s + ' 50 50)');
};

setInterval(fixHands, 200);
fixHands();
<svg width="100" height="100">

  <g stroke="steelblue" stroke-width="2" stroke-linecap="round">
   <line x1="50" y1="0" x2="50" y2="15" transform="rotate(0  50 50)" />
   <line x1="50" y1="0" x2="50" y2="15" transform="rotate(90  50 50)" />
   <line x1="50" y1="0" x2="50" y2="15" transform="rotate(180  50 50)" />
   <line x1="50" y1="0" x2="50" y2="15" transform="rotate(270  50 50)" />
  </g>
  
  <g stroke="steelblue" stroke-width="2" stroke-linecap="round">
   <line x1="50" y1="0" x2="50" y2="15" transform="rotate(30  50 50)" />
   <line x1="50" y1="0" x2="50" y2="15" transform="rotate(60  50 50)" />
   <line x1="50" y1="0" x2="50" y2="15" transform="rotate(120  50 50)" />
   <line x1="50" y1="0" x2="50" y2="15" transform="rotate(150  50 50)" />
   <line x1="50" y1="0" x2="50" y2="15" transform="rotate(210  50 50)" />
   <line x1="50" y1="0" x2="50" y2="15" transform="rotate(240  50 50)" />
   <line x1="50" y1="0" x2="50" y2="15" transform="rotate(300  50 50)" />
   <line x1="50" y1="0" x2="50" y2="15" transform="rotate(330  50 50)" />
  </g>
  
  <g stroke="steelblue" stroke-width="1" stroke-linecap="round">
   <line id="second" x1="50" y1="5" x2="50" y2="60" transform="rotate(0  50 50)" />
  </g>
  
  <g stroke="steelblue" stroke-width="2" stroke-linecap="round">
   <line id="minute" x1="50" y1="10" x2="50" y2="55" transform="rotate(0  50 50)" />
  </g>
  
  <g stroke="steelblue" stroke-width="3" stroke-linecap="round">
   <line id="hour" x1="50" y1="25" x2="50" y2="55" transform="rotate(0  50 50)" />
  </g>
</svg>

关于javascript - 如何在 CSS 中制作时钟并在 JS 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46261583/

相关文章:

php - 如何将选择选项(产品属性)转换为 WooCommerce 可变产品页面中的可点击 div?

javascript - 如何隐藏列表中不必要的重新排序箭头

当要替换两个以上相同的单词时,Javascript .replace 不适用于 .match 单词

html - 为什么带有背景颜色的div在下面显示固定元素?

css - 媒体查询忽略 "medium"选择

javascript - 使用 jquery 更改视口(viewport)元标记

javascript - 如何在表格行元素中绑定(bind)多个CSS类?

Javascript 数组重置没有给出所需的输出

javascript - 使 bootstrap-select 下拉菜单成为必需的

javascript - 无法在 Bootstrap 5 中使用 JavaScript 打开 Modal