css钟效果代码(css时钟)
admin 发布:2022-12-19 23:50 122
本篇文章给大家谈谈css钟效果代码,以及css时钟对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、css怎么样制作钟表
- 2、怎样用html和css做时钟的转动效果
- 3、如何用jQuery和CSS3制作数字时钟
- 4、求css动态时钟以及日历的代码(运行能用的,没用的不要写上来了,谢谢)(与电脑时间同步)
css怎么样制作钟表
代码如下:
!DOCTYPE html
html lang="en" xmlns=""
head
meta charset="utf-8" /
titlecanvas实例--制作时钟/title
/head
body
canvas id="canvas" width="500" height="500"
您的浏览器版本太低啦!可以换了!
/canvas
script
//获取canvas
var canvas = document.getElementById("canvas");
//设置环境
var cxt = canvas.getContext("2d");
//制作时钟的函数
function DrawClock() {
//清除画布
cxt.clearRect(0,0,500,500);
//获取当前时间的时,分,秒
var now = new Date();
var hour = now.getHours();
var min = now.getMinutes();
var sec = now.getSeconds();
//小时必须获取浮点型(小时+分数---转化为的小时)
hour = hour + min / 60;
//将24小时转换为12小时
hour = hour 12 ? hour - 12 : hour;
//制作表盘
cxt.beginPath();
cxt.lineWidth = 10;
cxt.strokeStyle = "#ABCDEF";
cxt.arc(250, 250, 200, 0, 360, false);
cxt.stroke();
cxt.closePath();
//刻度
//时针
for (var i = 0; i 12; i++) {
cxt.save();
cxt.lineWidth = 7;
cxt.strokeStyle = "red";
//设置0,0点
cxt.translate(250, 250);
//再设置旋转角度
cxt.rotate(i * 30 * Math.PI / 180);
cxt.beginPath();
cxt.moveTo(0, -170);
cxt.lineTo(0, -190);
cxt.closePath();
cxt.stroke();
cxt.restore();
}
//分针
for (var i = 0; i 60; i++) {
//为避免不同颜色的重叠,
//在时针刻度与分针刻度重叠的位置,不画分针
if (i % 5 == 0) continue;
cxt.save();
//设置刻度粗细
cxt.lineWidth = 5;
cxt.strokeStyle = "purple";
//设置画布的0,0点
cxt.translate(250, 250);
//设置旋转角度
cxt.rotate(i * 6 * Math.PI / 180);
//画分针刻度
cxt.beginPath();
cxt.moveTo(0, -180);
cxt.lineTo(0, -190);
cxt.closePath();
cxt.stroke();
cxt.restore();
}
//时针
cxt.save();
//设置时针风格
cxt.lineWidth = 7;
cxt.strokeStyle = "pink";
//设置异次元空间的0,0点
cxt.translate(250, 250);
//设置旋转角度
cxt.rotate(hour * 30 * Math.PI / 180);
cxt.beginPath();
cxt.moveTo(0, -140);
cxt.lineTo(0, 10);
cxt.closePath();
cxt.stroke();
cxt.restore();
//分针
cxt.save();
//设置分针风格
cxt.lineWidth = 5;
cxt.strokeStyle = "orange";
//设置异次元空间的0,0点
cxt.translate(250, 250);
//设置旋转角度
cxt.rotate(min * 6 * Math.PI / 180);
cxt.beginPath();
cxt.moveTo(0, -160);
cxt.lineTo(0, 15);
cxt.closePath();
cxt.stroke();
cxt.restore();
//秒针
cxt.save();
//风格
cxt.strokeStyle = "yellow";
cxt.lineWidth = 3;
//重置0,0点
cxt.translate(250, 250);
//设置旋转角度
cxt.rotate(sec*6*Math.PI/180);
//画图
cxt.beginPath();
cxt.moveTo(0, -170);
cxt.lineTo(0, 20);
cxt.stroke();
//画出时针,分针,秒针的交叉点
cxt.beginPath();
cxt.arc(0, 0, 5, 0, 360, false);
cxt.closePath();
//设置填充样式
cxt.fillStyle = "blue";
cxt.fill();
//设置笔触样式---秒针已设置
cxt.stroke();
//设置秒针前端的小圆点
cxt.beginPath();
cxt.arc(0, -150, 5, 0, 360, false);
cxt.closePath();
//设置填充样式
cxt.fillStyle = "blue";
cxt.fill();
//设置笔触样式
cxt.stroke();
cxt.closePath();
cxt.restore();
}
//调用函数
DrawClock();
//设置时钟转动起来
setInterval(DrawClock, 1000);
/script
/body
/html
怎样用html和css做时钟的转动效果
css 有个 animation 可以实现动画,仅仅是动起来,没法实现实时与系统对时(需要js)
60秒跳动60次旋转360度。(可以使用linear 线性运动)
# animation:anim_mm 60s linear infinite;
animation:mm 60s steps(60) infinite;
@keyframes mm{
to{ transform:rotate(360deg) ;}
}
如何用jQuery和CSS3制作数字时钟
这个时钟不需要很多HTML,这是因为它很大的一部分,像工作日的名称和数字都是动态生成的。 下面是你需要在你页面上使用时钟时要有的标签:
index.html
div id="clock" class="light"
div class="display"
div class="weekdays"/div
div class="ampm"/div
div class="alarm"/div
div class="digits"/div
/div
/div
主元素为#clock的div,包含.display的div,用于容纳平日列表、AM/PM标记、闹铃和时间。 下面代码为每个数字生成一个标签:
div class="zero"
span class="d1"/span
span class="d2"/span
span class="d3"/span
span class="d4"/span
span class="d5"/span
span class="d6"/span
span class="d7"/span
/div
.digits元素包含6个像这样带span的div,每个div为时钟的一个数字。就像你在上面片段中所见到的一样,这些div拥有一个从0到9的样式名称,并且包含7个带独立样式的span元素,这些span是数字的一部分,像老的数字时钟一样:
数字说明
它们完全用CSS样式渲染且默认设置为 opacity:0 。定义在它们父div上的样式将决定它们的可见性。下面是数字“0”的CSS:
assets/css/styles.css
/* 0 */
#clock .digits div.zero .d1,
#clock .digits div.zero .d3,
#clock .digits div.zero .d4,
#clock .digits div.zero .d5,
#clock .digits div.zero .d6,
#clock .digits div.zero .d7{
opacity:1;
}
除了中间一个,所有的片断都是可见的,我已经向所有的这些span添加了CSS3转换属性,当在数字之间切换时出现渐变效果。
样式表里有很多其他CSS,我不再这列举。我相信最好的方式去学习CSS如何工作就是在Firebug、Chrome的审查器或你浏览器里的开发者工具里即时审查demo的代码。
黑色主题
jQuery 代码
要想要时钟工作,我们将使用jQuery生成每个数字的标签,并且设置一个定时器每秒钟更新一次样式,为了更简单,我们使用moment.js 库(快速开始) 来补偿JavaScript原生日期和时间方法的缺陷。
assets/js/script.js
$(function(){
// Cache some selectors
var clock = $('#clock'),
alarm = clock.find('.alarm'),
ampm = clock.find('.ampm');
// Map digits to their names (this will be an array)
var digit_to_name = 'zero one two three four five six seven eight nine'.split(' ');
// This object will hold the digit elements
var digits = {};
// Positions for the hours, minutes, and seconds
var positions = [
'h1', 'h2', ':', 'm1', 'm2', ':', 's1', 's2'
];
// Generate the digits with the needed markup,
// and add them to the clock
var digit_holder = clock.find('.digits');
$.each(positions, function(){
if(this == ':'){
digit_holder.append('div class="dots"');
}
else{
var pos = $('div');
for(var i=1; i8; i++){
pos.append('span class="d' + i + '"');
}
// Set the digits as key:value pairs in the digits object
digits[this] = pos;
// Add the digit elements to the page
digit_holder.append(pos);
}
});
// Add the weekday names
var weekday_names = 'MON TUE WED THU FRI SAT SUN'.split(' '),
weekday_holder = clock.find('.weekdays');
$.each(weekday_names, function(){
weekday_holder.append('span' + this + '/span');
});
var weekdays = clock.find('.weekdays span');
// Run a timer every second and update the clock
(function update_time(){
// Use moment.js to output the current time as a string
// hh is for the hours in 12-hour format,
// mm - minutes, ss-seconds (all with leading zeroes),
// d is for day of week and A is for AM/PM
var now = moment().format("hhmmssdA");
digits.h1.attr('class', digit_to_name[now[0]]);
digits.h2.attr('class', digit_to_name[now[1]]);
digits.m1.attr('class', digit_to_name[now[2]]);
digits.m2.attr('class', digit_to_name[now[3]]);
digits.s1.attr('class', digit_to_name[now[4]]);
digits.s2.attr('class', digit_to_name[now[5]]);
// The library returns Sunday as the first day of the week.
// Stupid, I know. Lets shift all the days one position down,
// and make Sunday last
var dow = now[6];
dow--;
// Sunday!
if(dow 0){
// Make it last
dow = 6;
}
// Mark the active day of the week
weekdays.removeClass('active').eq(dow).addClass('active');
// Set the am/pm text:
ampm.text(now[7]+now[8]);
// Schedule this function to be run again in 1 sec
setTimeout(update_time, 1000);
})();
// Switch the theme
$('a.button').click(function(){
clock.toggleClass('light dark');
});
});
求css动态时钟以及日历的代码(运行能用的,没用的不要写上来了,谢谢)(与电脑时间同步)
我这里有一段js动态时钟的代码,日历的比较复杂,没有研究:
scripttype="text/javascript"
setInterval(function() {
var week;
var date;
var today = new Date();
var year = today.getFullYear();
var month = today.getMonth() + 1;
var day = today.getDate();
var ss = today.getDay();
var hours = today.getHours();
var minutes = today.getMinutes();
var seconds = today.getSeconds();
date = year + "年" + month +"月" + day + "日 ";
if (ss == 0) week = "星期日";
if (ss == 1) week = "星期一";
if (ss == 2) week = "星期二";
if (ss == 3) week = "星期三";
if (ss == 4) week = "星期四";
if (ss == 5) week = "星期五";
if (ss == 6) week = "星期六";
if (minutes = 9) minutes ="0" + minutes;
if (seconds = 9) seconds ="0" + seconds;
document.getElementById('p_time').innerHTML = "今天是:" + date
+ week + " " + hours + ":" + minutes + ":" +
seconds;
},
1000);
/script
这里一定要注意,html里要有一个id为p_time的div。
css钟效果代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于css时钟、css钟效果代码的信息别忘了在本站进行查找喔。
版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;
- 上一篇:计费asp源代码(asp源码软件)
- 下一篇:鼠标滚动滚一屏代码(滑动滚屏怎么弄)
相关推荐
- 05-19百度seo软件,百度seo效果怎么样
- 05-19引流推广效果好的app,引流推广效果好的文案
- 05-18痘痘如何去除效果好,痘痘应该怎么去除
- 05-17北京百度快照推广公司,百度快照推广有效果吗
- 05-15网络营销推广微信hyhyk1效果好,微信网络营销怎么做
- 05-15在百度上打广告找谁,百度上打广告有效果吗
- 05-13现在什么app引流效果好,现在什么app引流效果好呢
- 05-09网页代码,网页代码快捷键
- 05-09百度seo查询工具,百度seo效果怎么样
- 05-06单页网站的代码(完整的网页代码)[20240506更新]
取消回复欢迎 你 发表评论:
- 标签列表
- 最近发表
- 友情链接