knowladge_网站开发_jQuery插件_Clock Demo

news/2024/5/29 13:13:06/文章来源:https://blog.csdn.net/weixin_30598225/article/details/95981823

今天要用jQuery做一个Clock Demo. 查了查相关的资料 

找到了一个很好的插件设计 http://css-tricks.com/css3-clock

转发一下这个文章

Old School Clock with CSS3 and jQuery

PUBLISHED NOVEMBER 17, 2008 BY GUEST AUTHOR

Hi everyone, my name is Toby Pitman and Chris has asked me to write a post about a recent experiment that I posted up on the forum a little while ago. It all started when I was looking at the WebKit blog and saw an article on the new CSS3 animations and the one that caught my eye was 'rotate'. I started thinking what the hell can you rotate on a webpage (it's animated GIF's times ten!). Then it hit me - a clocks hands rotate! Bingo!

 

CSS3 transform: rotate

transform: rotate; is a new feature of CSS 3 which lets you... well, rotate stuff. Transform will also allow you to scale, skew and translate (move) objects in your web page. All these can be animated by the Transition property (complete with easing and duration).

Sound familiar? Well it should, it's the same thing we are currently using frameworks like jQuery to animate elements on our web pages. As with jQuery you can animate pretty much any CSS property worth animating with Transition. Think of a :hover effect and you should get some ideas. It's nowhere near as powerful as jQuery or Mootools etc but can still do some cool stuff. I doubt John Resig will lose any sleep!

IMPORTANT NOTE: This demo will only work in Firefox 3.1+ and Safari 3+ (and other modern WebKit based browsers like Chrome).

And no, this won't work in IE6... ever! OK let's go!

View Demo Download Files

The Graphics

First we'll need some graphics for our clock interface. We have a face and three hands. All the moving parts are sliced in Photoshop at 600px high and 30px wide and are positioned vertically dead center as by default the rotate property 'rotates' from the center of the element. You can use 'transform-origin' to set a rotate point if you want.

I've gone with a generic clock thing here but there's nothing to stop you going for a Micky Mouse clock if you like. The moving parts are all PNG images making this effect possible.

The HTML Markup

The mark-up for the clock is a simple un-ordered list. Each list item will hold a moving part and is given a relevant id. Here it is.

<ul id="clock">	<li id="sec"></li><li id="hour"></li><li id="min"></li>
</ul>

NOTE: The forum post had a bit more of a clunky mark-up as it has realistic drop shadows on the hands. I've refined some of the code a bit for this demo and the forum one has been left as is.

The CSS

#clock {position: relative;width: 600px;height: 600px;margin: 20px auto 0 auto;background: url(clockface.jpg);list-style: none;
}#sec, #min, #hour {position: absolute;width: 30px;height: 600px;top: 0px;left: 285px;
}#sec {background: url(sechand.png);z-index: 3;
}#min {background: url(minhand.png);z-index: 2;
}#hour {background: url(hourhand.png);z-index: 1;
}

The CSS is really simple too. As the moving parts share the same dimensions and start positions we can declare these together to avoid repeating ourselves making the CSS a bit leaner. The <ul> is given a position of relative allowing us to absolutely position the clock hands inside it.

NOTE: Rotate doesn't effect layout and the object behaves like an absolutely positioned element outside the normal flow when rotated.

So where is is the CSS3 stuff? Well we’re going to apply that using some simple jQuery.

The jQuery JavaScript

  1. Get the timing information for the clock.
  2. Calculate and inject the CSS style (rotation angle) for each element.
  3. Update the CSS style info at regular intervals.

It should be noted that jQuery has no problem at all using these new CSS3 properties. Also as the styles are allocated dynamically and not from the stylesheet this clock still validates as CSS2.1!

Getting the time

Some of you might (well I did!) equate date and time info with PHP, and when I started this PHP was my first thought, but then I discovered javascript has it's own built in functionality for getting date and time data too. Note with JavaScript the time is local to the machine not the server.

We're going to get this info using the Date() syntax and assigning that to a variable. We can get each hands specific data by attaching GetSeconds()GetMinutes() orGetHours() to the Date() syntax like this example.

var seconds = new Date().getSeconds();

The above code will return a number between 0 and 59 and store it inside the variable 'seconds'.

Getting the angle

Next we have calculate the angle for each hand. In the case of the seconds and minutes which have 60 increments per rotation we just divide 360 by 60 which gives us 6. This means that for each second or minute we have to rotate the hand by 6 degrees. We're going to to store this equation inside another variable. For the seconds it will look like this.

var sdegree = seconds * 6;

The hour hand throws up a different scenario. Because there are 12 increments per rotation the angle for each hour is 30 degrees. That's 360/12=30. Now that would be simple if a clocks hour hand moved in hour increments but it does't. It moves in smaller amounts based on the minute value. Say at 4:30 the hour hand will be half way between 3 and 4. So how do we do this. Here's the code.

var hdegree = hours * 30 + (mins / 2);

Basically we're going to add the current number of minutes divided by two which should give us a number between 0.5 and 29.5 ('rotate' can handle all floating point numbers) this puts the hand somewhere between any given 30 degree (hour) increment.

Example:

2.40 would be: 2 * 30=60 degrees
 +40 / 2=20 degrees
  ----------------------
  hdegree=80 degrees

I did think that the clock might explode when it got past 12 as it would be a number higher then 360 but it worked fine.

So now we've figured the math lets inject the new CSS.

Set the style

Here is the CSS3 rotate as if it were in a style sheet.

#sec {-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);-ms-transform: rotate(45deg);-o-transform: rotate(45deg);transform: rotate(45deg);
}

Here it is injected by jQuery (NOTE: As of jQuery 1.8, the proper vendor prefixes will be applied when using the .css() function. Older versions of jQuery will need to apply them manually).

$("#sec").css({"transform": "rotate(45deg)"
});

The problem we have here is getting the variable 'sdegree' (which holds our angle) into this syntax to replace the (45deg). At first I just tried putting the variable between the brackets but it wouldn't have it. To make this work we need to build a 'string' inside another variable called 'srotate' and completely replace the second argument. Here's how we make it.

var srotate = "rotate(" + sdegree + "deg)";

The jQuery will now be written as this.

$("#sec").css({ "transform": srotate });

Putting it all together (in time!)

The whole jQuery code looks like this.

$(function() {setInterval( function() {var seconds = new Date().getSeconds();var sdegree = seconds * 6;var srotate = "rotate(" + sdegree + "deg)";$("#sec").css({ "transform": srotate });}, 1000 );setInterval( function() {var hours = new Date().getHours();var mins = new Date().getMinutes();var hdegree = hours * 30 + (mins / 2);var hrotate = "rotate(" + hdegree + "deg)";$("#hour").css({ "transform": hrotate});}, 1000 );setInterval( function() {var mins = new Date().getMinutes();var mdegree = mins * 6;var mrotate = "rotate(" + mdegree + "deg)";$("#min").css({ "transform" : mrotate });}, 1000 );});

Notice how we've used a JavaScript setInterval so the function is run every second. The variables that get the time data have to be inside the function so they update too. If not they would only gather that data on page load once, making a pretty rubbish clock.

Our clock should now work (in Safari and other modern browsers).

Alternatives

Flash is the obvious one but unless you've got ActionScript chops I'd give it a swerve. This may even be possible with PHP using the GD image library to rotate the hands but that's way above my level.

Incidentally, Soh Tanaka posted this a few days after by pure coincidence: CSS clock. His clock uses JavaScript to add classes and sprite images for the hands.

UPDATE Nov 2012: Daniel Bakan has a very similar article "How To Write a Full Screen Web App Optimized For iOS Devices" in which he builds an old timey clock as well. If you're super interested in this, that's worth checking out as well.

Conclusion

So there you have it folks, a real and (probably the only) practical use for the new CSS3 property transform:rotate. I for one can't wait for upside down and sideways websites (Yes, you can even rotate the HTML tag! Although look what that does to Safari) and my personal favorite the spinning/zooming cartoon newspaper blog posts.

So go on, give some of those new CSS3 features a try!

 

小结一下: 做了一下 非常好用 关于计算那的代码还没有细看 如果要将钟的size变小 只需要改一下背景图片 然后用PS将背景中的白色空白p掉即可。 

 

 

 

 

转载于:https://www.cnblogs.com/kidshaoran/archive/2013/05/23/3095664.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_705963.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

opengl教程 linux,绘制基本的几何图形 - OpenGL编程学习实战教程_Linux编程_Linux公社-Linux系统门户网站...

第2章.编写一个的钟表程序第一节.绘制基本的几何图形矩形、三角形、圆形等这些都是经典的几何图形&#xff0c;他们都由线构成的(圆形看成是有很多根短线收尾相连围成的)&#xff0c;而线都是点构成的。想起了某一年狗血的高考题。....而在OpenGL中画线很简单&#xff0c;你指定…

让那些为Webkit优化的网站也能适配IE10(转载)

转载地址&#xff1a;http://www.w3cplus.com/css3/adapting-your-webkit-optimized-site-for-internet-explorer-10.html 特别声明&#xff1a;此篇文章由David根据Charles Morris的英文文章原名《Adapting your WebKit-optimized site for Internet Explorer 10》进行翻译&am…

网站服务器windows登陆密码忘记,网站服务器windows登陆密码忘记

网站服务器windows登陆密码忘记 内容精选换一换本节操作以使用“Microsoft Remote Desktop for Mac”工具远程连接“Windows Server 2012 R2 数据中心版 64位”操作系统云服务器为例&#xff0c;介绍Mac OS系统登录Windows云服务器的操作步骤。云服务器状态为“运行中”。已获取…

学霸网站-Beta版本发布说明

项目名称学霸网站项目版本Beta项目团队ourteam发布日期2015-1-5一、Alpha版本实现功能简介&#xff1a; 1.匿名提问 2.匿名回答 3.采纳功能 4.登录、注册失败后&#xff0c;用户名等信息保留在页面 5.加入悬赏功能 二、Beta版本实现功能 1.外部问题的显示 将爬虫组爬到的问题在…

网站服务器挂了导致排名下降,常见关键词排名消失的原因及解决对策

常见关键词排名消失的原因及解决对策(2013-06-07 14:34:29)标签&#xff1a;关键词排名消失的原因我们做seo&#xff0c;一定会遇到关键词排名消失的现象。其实关键词排名消失不可怕&#xff0c;怕的是我们找不到关键词排名消失的原因。我个人认为关键词排名消失了&#xff0c;…

.NET开发人员必知的八个网站

当前全球有数百万的开发人员在使用微软的.NET技术。如果你是其中之一&#xff0c;或者想要成为其中之一的话&#xff0c;我下面将要列出的每一个站点都应该是你的最爱&#xff0c;都应该收藏到书签中去。 对于不熟悉.NET技术的朋友&#xff0c;需要说明一下&#xff0c;.NET提供…

原SUN网站:java.sun.com,developers.sun.com,bigadmin将合并到OTN

就OTN上发布的合并声明来看到8月1日&#xff0c;Oracle将完成java.sun.com,developers.sun.com,bigadmin这几个网站合并到OTN的工作&#xff0c;这次迁移将是完整的并且在内容上是无损的。整合后的网站将提供给java开发者&#xff0c;数据库开发者及管理员&#xff0c;系统开发…

网站接入QQ登录最新2020 java版本

1.首先先注册账号,然后填资料审核,等个三四天吧大概 https://connect.qq.com/ 2.审核通过后点击创建应用,把备案号什么的都填(这个审核两天之内一般) !!! 这里有个重点,网站地址只能填一下,回调地址随便填一个,通过后可以随便改的 3. 整理java代码 我的是springBoot项目 先导入…

先睹为快:Visual Studio 11测试版已于2.29在微软官方网站正式发布

在2011的下半年&#xff0c;Visual Studio 11就已经呼之欲出&#xff0c;在2012年的2月29日&#xff0c;微软官方网站正式发布了VS11的测试版。VS11中使用了Framework4.5&#xff0c;与Framework4.0相比&#xff0c;没有发生太大的改变&#xff0c;只是对功能进行了优化。 下载…

UU看书于今日成功上线,各大小说网站发来贺电!!

经过连续一个多月艰苦卓越而紧张开发工作&#xff0c;UU看书于近日成功上线。UU看书是一个无弹窗小说网&#xff0c;UU看书页面简洁&#xff0c;无弹出广告&#xff0c;且小说更新速度快。UU看书提供玄幻小说,言情小说,都市小说,网游小说,军事小说等各类最新热门小说免费无弹窗…

程序员实用工具网站

目录 1、搜索引擎 2、PPT 3、图片操作 4、文件共享 5、应届生招聘 6、程序员面试题库 7、办公、开发软件 8、高清图片、视频素材网站 9、项目开源 10、算法 11、在线工具宝典大全 程序员开发需要具备良好的信息检索能力&#xff0c;为了备忘&#xff08;收藏夹真是满…

优化网站设计(七):避免在CSS中使用表达式

前言 网站设计的优化是一个很大的话题,有一些通用的原则,也有针对不同开发平台的一些建议。这方面的研究一直没有停止过&#xff0c;我在不同的场合也分享过这样的话题。 作为通用的原则&#xff0c;雅虎的工程师团队曾经给出过35个最佳实践。这个列表请参考 Best Practices fo…

网站如何从http升级成https

基本概念&#xff1a; HTTP: 是互联网上应用最为广泛的一种网络协议&#xff0c;是一个客户端和服务器端请求和应答的标准&#xff0c;用于从WWW服务器传输超文本到本地浏览器的传输协议&#xff0c;它可以使浏览器更加高效&#xff0c;使网络传输减少。 HTTPS: 是以安全为目标…

程序员之提升开发效率非常实用的十个工具网站分享

一、regex101 强大的正则表达式工具&#xff0c;可以实时查看匹配信息&#xff0c;并且会用不同的颜色将 Group 标记出来&#xff0c;而且有 Quick Reference 来帮助记忆正则表达式的规则和 Explanation 对正则表达式进行解释。更重要的是&#xff0c;还支持直接生成多种语言的…

大型网站系统架构实践(五)深入探讨web应用高可用方案

从上篇文章到这篇文章&#xff0c;中间用了一段时间准备&#xff0c;主要是想把东西讲透&#xff0c;同时希望大家给与一些批评和建议&#xff0c;这样我才能有所进步&#xff0c;也希望喜欢我文章的朋友&#xff0c;给个赞&#xff0c;这样我才能更有激情&#xff0c;呵呵。 由…

PHP网站使用JavaScript和Iframe简单实现部分刷新效果

本文主要是记录自己寒假作业PHP网站实现加载界面的文章&#xff0c;运行效果如下图所示。主要记录phphtmlApache开发网站的3个功能&#xff1a;(方便以后阅读和其他人学习) 1.如何实现简单页面布局 2.使用jsp如何实现隐藏与显示效果 3.通过iframe实现局…

[python学习] 简单爬取图片网站图库中图片

最近老师让学习Python与维基百科相关的知识&#xff0c;无聊之中用Python简单做了个爬取“游讯网图库”中的图片&#xff0c;因为每次点击下一张感觉非常浪费时间又繁琐。主要分享的是如何爬取HTML的知识和Python如何下载图片&#xff1b;希望对大家有所帮助&#xff0c;同时发…

Java+MyEclipse+Tomcat (一)配置过程及jsp网站开发入门

这些年免费帮同学或同学的朋友做过的毕设还是有一些了&#xff0c;我的博客基本都是基于我做过的项目相关的&#xff0c;而里面又有三分之一都是自己做的毕设或别人的毕设的做后感。毕设其中就包括MFC图像处理、.Net安全软件、C#聊天软件字典软件、Android百度地图随手拍、php网…

JSP网站开发之HTML入门知识及常用标记符 (一)

最近发生的事情很多&#xff0c;其中一件很重要的事情就是&#xff1a;学生生涯的结束&#xff0c;教学生涯的开始。我准备下个月写一篇总结研究生生涯的文章&#xff0c;包括自己放弃互联网选择回家教书、找工作经历、项目和毕业设计的各种感想。很荣幸XB七月初就给了我第一次…

[网站搭建] 阿里云虚拟主机搭建及FTP文件上传

写这篇文章主要有两个原因&#xff1a;一方面是得到了阿里云的一个“开通码”&#xff0c;另一方面是最近给学生们上网站制作的课程&#xff0c;想把学生们提交的作业上传到服务器上去&#xff0c;让他们体会一下自己做的东西&#xff0c;提升他们的编程兴趣。虽然只有短暂的4节…