纯javascript+CSS3制作精美时钟

纯javascript+CSS3制作精美时钟。该实例主要应用了CSS3中transform属性(translate、rotate)、border-radius属性。 CSS样式: style type=text/css body { background: black; } .fill { position: absolute; top: 0; right: 0; bottom: 0; left: 0; } a {

纯javascript+CSS3制作精美时钟。该实例主要应用了CSS3中transform属性(translate、rotate)、border-radius属性。

 

CSS样式:

<style type="text/css">
body {
background: black;
}

.fill {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}

a {
display: block;
position: fixed;
top: 0px; left: 0px;
color: #fcc;
text-decoration: none;
padding: 4px;
background: rgba(52,52,52,0.5);
font: 12px Verdana, sans-serif;
z-index: 1;
}

.clock {
position: absolute;
opacity: 1;
}

.fill .clock {
left: 50%;
top: 50%;
}

.centre {
position: absolute;
top: 50%;
left: 50%;
width: 0;
height: 0;
}

.expand {
position: absolute;
top: 0;
left: 0;
transform: translate(-50%, -50%);
}

.anchor {
position: absolute;
top: 0;
left: 0;
width: 0;
height: 0;
}

.element {
position: absolute;
top: 0;
left: 0;
}

.round {
border-radius: 296px;
}

.circle-1 {
background: white;
width: 12px;
height: 12px;
}

.circle-2 {
background: #FA9F22;
width: 8px;
height: 8px;
}

.circle-3 {
background: black;
width: 4px;
height: 4px;
}

.second {
transform: rotate(180deg);
}

.minute {
transform: rotate(54deg);
}

.second-hand {
width: 2px;
height: 164px;
background: #FA9F22;
transform: translate(-50%,-100%) translateY(24px);
}

.hour {
transform: rotate(304.5deg);
}

.thin-hand {
width: 4px;
height: 50px;
background: white;
transform: translate(-50%,-100%);
}

.fat-hand {
width: 10px;
height: 57px;
border-radius: 10px;
background: white;
transform: translate(-50%,-100%) translateY(-18px);
}

.minute-hand {
height: 112px;
}

.hour-text {
position: absolute;
font: 40px Hei, Helvetica, Arial, sans-serif;
color: white;
transform: translate(-50%,-50%);
}

.hour-10 {
padding-left: 0.4ex;
}
.hour-11 {
padding-left: 0.25ex;
}

.minute-text {
position: absolute;
font: 12px Avenir Next, Helvetica, Arial, sans-serif;
color: white;
transform: translate(-50%,-50%);
}

.minute-line {
background: white;
width: 1px;
height: 9px;
transform: translate(-50%,-100%) translateY(-131px);
opacity: 0.34;
}
</style>

 

HTML代码:

<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
</head>
<body>
<div class="fill">
<div class="reference"></div>
<div class="clock" id="utility-clock">
<div class="centre">
<div class="dynamic"></div>
<div class="expand round circle-1"></div>
<div class="anchor hour">
<div class="element thin-hand"></div>
<div class="element fat-hand"></div>
</div>
<div class="anchor minute">
<div class="element thin-hand"></div>
<div class="element fat-hand minute-hand"></div>
</div>
<div class="anchor second">
<div class="element second-hand"></div>
</div>
<div class="expand round circle-2"></div>
<div class="expand round circle-3"></div>
</div>
</div>
</div>
</body>
</html>

 

JS代码:

<script language="javascript">
var clock = document.querySelector('#utility-clock')
utilityClock(clock)

autoResize(clock, 295 + 32)

function utilityClock(container) {
var dynamic = container.querySelector('.dynamic')
var hourElement = container.querySelector('.hour')
var minuteElement = container.querySelector('.minute')
var secondElement = container.querySelector('.second')
var minute = function(n) {
return n % 5 == 0 ? minuteText(n) : minuteLine(n)
}
var minuteText = function(n) {
var element = document.createElement('div')
element.className = 'minute-text'
element.innerHTML = (n < 10 ? '0' : '') + n
position(element, n / 60, 135)
dynamic.appendChild(element)
}
var minuteLine = function(n) {
var anchor = document.createElement('div')
anchor.className = 'anchor'
var element = document.createElement('div')
element.className = 'element minute-line'
rotate(anchor, n)
anchor.appendChild(element)
dynamic.appendChild(anchor)
}
var hour = function(n) {
var element = document.createElement('div')
element.className = 'hour-text hour-' + n
element.innerHTML = n
position(element, n / 12, 105)
dynamic.appendChild(element)
}
var position = function(element, phase, r) {
var theta = phase * 2 * Math.PI
element.style.top = (-r * Math.cos(theta)).toFixed(1) + 'px'
element.style.left = (r * Math.sin(theta)).toFixed(1) + 'px'
}
var rotate = function(element, second) {
element.style.transform = element.style.webkitTransform = 'rotate(' + (second * 6) + 'deg)'
}
var animate = function() {
var now = new Date()
var time = now.getHours() * 3600 +
now.getMinutes() * 60 +
now.getSeconds() * 1 +
now.getMilliseconds() / 1000
rotate(secondElement, time)
rotate(minuteElement, time / 60)
rotate(hourElement, time / 60 / 12)
requestAnimationFrame(animate)
}
for (var i = 1; i <= 60; i ++) minute(i)
for (var i = 1; i <= 12; i ++) hour(i)
animate()
}

function autoResize(element, nativeSize) {
var update = function() {
var parent = element.offsetParent
var scale = Math.min(parent.offsetWidth, parent.offsetHeight) / nativeSize
element.style.transform = element.style.webkitTransform = 'scale(' + scale.toFixed(3) + ')'
}
update()
window.addEventListener('resize', update)
}

</script>


本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

分页条是web开发中常用的前端组件之一,它能将数据分批次展示给用户。我们知道诸如Bootstrap这类框架都提供了非常漂亮的分页条组件,但是你的项目可能不用到Bootstrap,你想自己定制一个属于你自己项目的漂亮的分页条,那么请看本文。 本文给大家介绍用纯CSS
本文章向码农介绍css制作透明三角形,css绘制三角形很简单,如何绘制透明的三角形,本文为码农解决这个问题,感兴趣的码农可以参考一下。
本实例通过对radio按钮的状态判断来实现折叠效果。主要应用的是兄弟元素选择器,注意这个兄弟元素选择器只选择后面的兄弟元素,对于同级的前面的兄弟元素css3还没有响应的选择器。然后综合应用:before和transition来实现前面图标的切换及高度的伸展动画。这个
不用图片,只用css制作圆角的另一方法。
纯CSS打造的漫画,让枯燥变的活泼,让局限变的无限有的人说没有意义,我觉得意义重大。 让枯燥变的活泼,让局限变的无限,请点击以下运行看演示。 [html] div style=width: 100%; height: 466px; div style=float: left; border: 2px so
一个简单的效果,鼠标移到图片上就可以显示查看该图片的大图,完全用css写的,居中用到负边距居中方式。