css3时钟
参考资料:
奇舞团: http://www.75team.com/archives/851
DEMO:demo
截图:

代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>css3钟表</title>
<style id="style">
body,html{width:100%;height:100%;}
*,ul{margin:0;padding:0;}
li{list-style: none;padding:0;margin:0;}
#box{ position:absolute; left:100px;top:100px; width:300px; height:300px; border-radius: 50%; border:1px solid #999; border:2px solid #000; }
.handles,.digits{ position:absolute;left:0;top:0; width:300px; height:300px; }
.handles{}
.handles li{ position: absolute; top:0;left:50%;width:2px;height:8px;background-color: #000;transform-origin: 0 150px;}
.handles li:nth-child(5n+1){ height:14px;}
.digits li{ position: absolute; top:50%;left:50%;width:20px;height:20px; margin:-10px 0 0 -10px;line-height: 20px; text-align: center; font-size: 18px; font-weight: bold;font-family: 'microsoft yahei', Arial, Helvetica, sans-serif; }
.timer{ position: absolute;left:50%;top:50%;width:20px;height:20px; background-color: #000;margin:-10px 0 0 -10px;border-radius: 50%; }
.timer:after{ content:'';position: absolute;left:0;top:0;z-index: 10;background-color: #000; width:20px;height:20px; border-radius: 50%;}
.hours-hand{ position: absolute;bottom:50%;left:50%;width:8px;height:60px;margin-left:-4px; background-color:#000; transform-origin:center bottom; border-top-left-radius:30px;border-top-right-radius:30px; }
.minutes-hand{ position: absolute;bottom:50%;left:50%;width:6px;height:90px;margin-left:-3px; background-color:#000; transform-origin:center bottom;border-top-left-radius:30px;border-top-right-radius:30px;}
.seconds-hand{ position: absolute;bottom:50%;left:50%;width:2px;height:120px;margin-left:-1px; background-color:red; transform-origin:center bottom; transform:rotate(30deg);border-top-left-radius:30px;border-top-right-radius:30px;}
</style>
<script>
window.onload = function(){ var oBox = document.getElementById('box'),
oHandles = document.getElementById('handles'),
oDigits = document.getElementById('digits'),
oHours = document.getElementById('hours'),
oMinutes = document.getElementById('minutes'),
oSeconds = document.getElementById('seconds'),
timer = null; //创建表针;
var aLiHtml = '';
for( var i=0,len=60;i<len;i++ ){
aLiHtml += '<li style="transform:rotate('+ ( i*360/60 ) +'deg);" ></li>';
}
oHandles.innerHTML = aLiHtml; //创建数字;
var aDigitsHtml = '';
var r = 122;
for( var i=1,len=13;i<len;i++ ){
var t = -Math.cos( i*360/12*Math.PI/180 )*r;
var l = Math.sin( i*360/12*Math.PI/180 )*r;
aDigitsHtml += '<li style="transform:translate('+ l +'px,'+ t +'px);" >'+ i +'</li>';
}
oDigits.innerHTML = aDigitsHtml; //初始化时间;
setTime();
timer = setInterval(setTime,1000); //设置时间方法;
function setTime(){ var myDate = new Date(),
seconds = myDate.getSeconds(),
minutes = myDate.getMinutes() + seconds/60,
hours = myDate.getHours() + minutes/60; oHours.style.transform = 'rotate('+ hours*(360/12) +'deg)';
oMinutes.style.transform = 'rotate('+ minutes*(360/60) +'deg)';
oSeconds.style.transform = 'rotate('+ seconds*(360/60) +'deg)'; } //拖拽;
drag( oBox ); //拖拽方法;
function drag( obj ){ obj.onmouseover = function(){
obj.style.cursor = 'move';
} obj.onmousedown = function( event ){ var ev = window.event || event;
var yuanT = obj.offsetTop;
var yuanL = obj.offsetLeft;
var disX = ev.clientX - yuanL;
var disY = ev.clientY - yuanT; document.onmousemove = function( event ){ var ev = window.event || event; var evX = ev.clientX;
var evY = ev.clientY;
var t = evY - disY;
var l = evX - disX ; var maxWidth = document.body.clientWidth - obj.offsetWidth;
var maxHeight = document.body.clientHeight - obj.offsetHeight; if( t > maxHeight ){
t = maxHeight;
}else if( t < 0 ){
t = 0;
} if( l > maxWidth ){
l = maxWidth;
}else if( l < 0 ){
l = 0;
} obj.style.top = t + 'px';
obj.style.left = l + 'px'; ev.stopPropagation();
return false; } document.onmouseup = function( event ){
document.onmousemove = null;
} } } }
</script>
</head>
<body>
<div id="box">
<ul id="handles" class="handles">
</ul>
<ul id="digits" class="digits">
</ul>
<ul class="timer">
<li id="hours" class="hours-hand"></li>
<li id="minutes" class="minutes-hand"></li>
<li id="seconds" class="seconds-hand"></li>
</ul>
</div>
</body>
</html>
后记:
网上有很多这种css3画的钟表,原理其实就是transform里的rotate和transform-origin的旋转元素基点位置,至于数字位置的看奇舞团的视频就可以了原理一样,只不过觉得太简单了加了个拖拽。。。。
css3时钟的更多相关文章
- CSS3时钟式进度条
CSS3时钟式进度条,加载完成时生成一个圆,数字慢慢变成100,适时的显示加载进度.友情提醒,如果预览时网页左下角提示错误,刷新一下就可以看到效果了:实际使用中不会出现这样的问题. <!DOCT ...
- CSS3 时钟
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- CSS3简易表盘时钟
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- CSS3 圆形时钟式网页进度条
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- css3 简易时钟
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- html5和css3学习笔记
HTML5针对移动端,移动端的浏览器主要是chrome,是webkit内核; app(applicatin):应用; native app:原生的app sadsadsadad 单标签可以省略结尾标记 ...
- css3、html5学习笔记
2016/12/14 ----认真看完绝对对你有帮助 HTML5针对移动端,移动端的浏览器主要是chrome,是webkit内核; app(applicatin):应用; native app:原生的 ...
- 9种jQuery和css3图片动画特效代码演示
1.自由旋转的jQuery图片 演示和下载地址 2.css3阴影动画效果 演示和下载地址 3.拉窗帘特效图片 演示和下载地址 4.css3文字特效动画 演示和下载地址 5.css3时钟代码 演示和下载 ...
- 简单的CSS3 Loading动画
最终效果如图一,gif图片稍微有点卡顿,事实上代码在浏览器里执行得很流畅.这里面用到的css3技术非常简单,分别是border-radius.伪元素.css3关键帧以及animation动画. 首先整 ...
随机推荐
- bzoj千题计划313:bzoj3879: SvT(后缀数组+st表+单调栈)
https://www.lydsy.com/JudgeOnline/problem.php?id=3879 把所有的后缀取出,按rank排序 求出相邻两个后缀的lcp 每个后缀对答案的贡献就是 与在它 ...
- extern "C" 含义
extern "C" 被 extern 限定的函数或变量是 extern 类型的 被 extern "C" 修饰的变量和函数是按照 C 语言方式编译和链接的 e ...
- HanLP用户自定义词典源码分析
HanLP用户自定义词典源码分析 1. 官方文档及参考链接 关于词典问题Issue,首先参考:FAQ 自定义词典其实是基于规则的分词,它的用法参考这个issue 如果有些数量词.字母词需要分词,可参考 ...
- 开源框架.netCore DncZeus学习(二)配置连接
配置连接字符串,update-database,初始数据后,访问报错,提示offset错误.因为本机上使用的sql2008. .net Core 2.X中的EF访问sqlserver2008默认使用的 ...
- 分享12款 JavaScript 表格控件(DataGrid)
JavaScript 表格控件可以操作大数据集的 HTML 表格,提供各种功能,如分页.排序.过滤以及行编辑.在本文中,我们整理了13个最好的 JavaScript 表格插件分享给开发人员,开发者可以 ...
- 账号管理文件/etc/passwd和/etc/shadow
/etc/passwd和/etc/shadow是Linux中两个账号管理的重要文件 一./etc/passwd 这个文件中每一行代表一个账号,有几行就代表系统中存在几个账号.有些账号是系 ...
- 求f(n)=[n/1]+[n/2]+---+[n/n]的值 简单杂题
这种小题首先根据 n/1+n/2+n/3+--+n/n=nlogn+欧拉常数r 可以知道 1e12的范围也不会爆longlong,不需要写高精度(到现在都不会写) 再根据数据范围可知O(n)级别的暴力 ...
- mongodb 添加字段并设置默认值
db.doc名称.update({}, {$set: {新字段名称: 默认值}}, false, true) 如:db.fly_bill.update({}, {$set: {usableStatus ...
- 利用PHP访问数据库——实现分页功能与多条件查询功能
1.实现分页功能 <body><table width="100%" border="1"> <thead> < ...
- 第19月第20天 UITableView:改变 TableHeaderView 的高度 获取目录大小
1.UITableView:改变 TableHeaderView 的高度 CGRect newFrame = headerView.frame; newFrame.size.height = newF ...