




已阅读5页,还剩11页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
简介今天我们将要制作一个丰富多彩的jQuery和CSS的时钟,这个时钟将使用tzineClock插件来吧,下载演示文件,并继续学习该教程吧。XHTML像往常一样,我们开始于XHTML标记。有所不同的是,该时钟的XHTML不是写在demo.html里,而是写成JavaScript,然后由jQuery动态载入到页面中(当然,我们也得至少有一个容器div在html页面内,用于插入该时钟)。这中做法可以节省我们手动键入类似的代码块(例如:时钟中有三个类似的代码块:小时,分钟,秒)。让我们看一看jQuery要插入XHTML的外观:jquery.tzineClock.jsdiv class=green clock此代码是包含在jquery.tzineClock / jquery.tzineClock.js。他被调用三次 小时,分,秒。这些都是后来的动画和每一秒的运动(您等会儿就能看到这一点)。在产生代码的过程中,有3个类被分配在的最顶层容器 绿色,蓝色和橙色。只要通过指定的这些类的参数,我们就能改变这些层的颜色。让我们继续下一个步骤。CSS首先,我们必须在在网页的头部分包含以下文件:demo.html以上的代码将styles.css的和jquery.tzineClock.css插入到页面中。styles.css风格化了demo页面,jquery.tzineClock.css用于丰富时钟的表盘(这也是插件的一部分)。现在,我们可以看一看在CSS的规则。styles.cssbody,h1,h2,h3,p,quote,small,form,input,ul,li,ol,label/* Simple page reset */margin:0;padding:0;body/* Setting default text color, background and a font stack */color:#dddddd;font-size:13px;background: #302b23;font-family:Arial, Helvetica, sans-serif;#fancyClockmargin:40px auto;height:200px;border:1px solid #111111;width:600px;这些几行都是必须的,他用于制作演示页面的风格。在CSS代码中,我们首先实现一个简单的CSS重置,这将确保该网页上的元素在不同的浏览器上外观一致。之后,我们定义的网页正文的风格,最后是fancyClock分区,我们将在稍后插入三个表盘。jquery.tzineClock.css.clock/* The .clock div. Created dynamically by jQuery */background-color:#252525;height:200px;width:200px;position:relative;overflow:hidden;float:left;.clock .rotate/* There are two .rotate divs - one for each half of the background */position:absolute;width:200px;height:200px;top:0;left:0;.rotate.rightdisplay:none;z-index:11;.clock .bg, .clock .frontwidth:100px;height:200px;background-color:#252525;position:absolute;top:0;.clock .display/* Holds the number of seconds, minutes or hours respectfully */position:absolute;width:200px;font-family:Lucida Sans Unicode, Lucida Grande, sans-serif;z-index:20;color:#F5F5F5;font-size:60px;text-align:center;top:65px;left:0;/* CSS3 text shadow: */text-shadow:4px 4px 5px #333333;/* The left part of the background: */.clock .bg.left left:0; /* Individual styles for each color: */.orange .bg.left background:url(img/bg_orange.png) no-repeat left top; .green .bg.left background:url(img/bg_green.png) no-repeat left top; .blue .bg.leftbackground:url(img/bg_blue.png) no-repeat left top; /* The right part of the background: */.clock .bg.right left:100px; .orange .bg.right background:url(img/bg_orange.png) no-repeat right top; .green .bg.right background:url(img/bg_green.png) no-repeat right top; .blue .bg.right background:url(img/bg_blue.png) no-repeat right top; .clock .front.leftleft:0;z-index:10;jquery.tzineClock.css是jquery.tzineClock.js插件和一起的,它的作用是定义表盘的风格。有趣的是每个表盘运行使用独立的颜色规则,这也正是我们在步骤一中提到的。你可以通过以下图片说明来进一步了解该动画插图:jQueryJavaScript的插件使得代码可以很容易被重复使用,并在同一时间,jQuery能使我们感受到它选择器和方法的强大。为了能够使用jQuery库,我们首先需要包含一些脚本:demo.html第一个文件是jQuery本身,来自谷歌的CDN,后面2个分别是时钟插件和用于运行演示的script.js文件。script.js$(document).ready(function()/* This code is executed after the DOM has been completely loaded */$(#fancyClock).tzineClock(););如果您曾经学习过我们以前的一些教程,你可能会比较期待第50行以上的代码,但这次我们的脚本文件只包含一行代码 用于调用我们的插件。这使得它非常容易被包括到现有的站点(这是jQuery的插件摆在首位的目的)。让我们更深入地了解一下这个小插件:jquery.tzineClock.js Part 1(function($)/ A global object used by the functions of the plug-in:var gVars = ;/ Extending the jQuery core:$.fn.tzineClock = function(opts)/ this contains the elements that were selected when calling the plugin: $(elements).tzineClock();/ If the selector returned more than one element, we use the first one:var container = this.eq(0);if(!container)tryconsole.log(Invalid selector!); catch(e)return false;if(!opts) opts = ;var defaults = /* Additional options will be added in future versions of the plugin. */;/* Merging the provided options with the default ones (will be used in future versions of the plugin): */$.each(defaults,function(k,v)optsk = optsk | defaultsk;);/ Calling the setUp function and passing the container,/ will be available to the setUp function as this:setUp.call(container);return this;function setUp()/ The colors of the dials:var colors = orange,blue,green;var tmp;for(var i=0;i3;i+)/ Creating a new element and setting the color as a class name:tmp = $().attr(class,colorsi+ clock).html(+);/ Appending to the fancyClock container:$(this).append(tmp);/ Assigning some of the elements as variables for speed:tmp.rotateLeft = tmp.find(.rotate.left);tmp.rotateRight = tmp.find(.rotate.right);tmp.display = tmp.find(.display);/ Adding the dial as a global variable. Will be available as gVars.colorNamegVarscolorsi = tmp;/ Setting up a interval, executed every 1000 milliseconds:setInterval(function()var currentTime = new Date();var h = currentTime.getHours();var m = currentTime.getMinutes();var s = currentTime.getSeconds();animation(gVars.green, s, 60);animation(gVars.blue, m, 60);animation(gVars.orange, h, 24);,1000);做好一个jQuery插件,可以归结为通过jQuery.fn方法确定一个自定义函数。这样,你的jQuery函数是可以被用在任何元素上。例如,在script.js,我们选择的div的id是fancyClock的宽度和使用tzineClock()方法:$(fancyClock).tzineClock();我们选择的元素是后来传递给tzineClock的功能,并通过了“this”属性被使用。jquery.tzineClock.js Part 2function animation(clock, current, total)/ Calculating the current angle:var angle = (360/total)*(current+1);var element;if(current=0)/ Hiding the right half of the background:clock.rotateRight.hide();/ Resetting the rotation of the left part:rotateElement(clock.rotateLeft,0);if(angle=180)/ The left part is rotated, and the right is currently hidden:element = clock.rotateLeft;else/ The first part of the rotation has completed, so we start rotating the right part:clock.rotateRight.show();clock.rotateLeft.show();rotateElement(clock.rotateLeft,180);element = clock.rotateRight;angle = angle-180;rotateElement(element,angle);/ Setting the text inside of the display element, inserting a leading zero if needed:clock.display.html(current10?0+current:current);function rotateElement(element,angle)/ Rotating the element, depending on the browser:var rotate = rotate(+angle+deg);if(element.css(MozTransform)!=undefined)element.css(MozTransform,rotate);else if(element.css(WebkitTransform)!=undefined)element.css(WebkitTransform,rotate);/ A version for internet explorer using filters, works but is a bit buggy (no surprise here):else if(element.css(filter)!=undefined)var cos =
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 肉羊养殖高质量发展实施方案
- 江苏财经职业技术学院《马克思主义者基本原理》2023-2024学年第一学期期末试卷
- 重庆工程职业技术学院《中国法制史》2023-2024学年第一学期期末试卷
- 船舶VR培训数据采集-洞察及研究
- 出差专员培训总结
- 永州职业技术学院《园林植物配景》2023-2024学年第一学期期末试卷
- 售楼部培训的基础知识
- 湘南幼儿师范高等专科学校《动画影视欣赏》2023-2024学年第一学期期末试卷
- 湖南工程职业技术学院《粤剧唱腔与身段表演》2023-2024学年第一学期期末试卷
- 河南财经政法大学《复变函数与积分变换Ⅰ》2023-2024学年第一学期期末试卷
- 村规民约范文大全三篇村规民约范文大全
- Q∕SY 01007-2016 油气田用压力容器监督检查技术规范
- 赤水市辖区内枫溪河(风溪河)、宝沅河(宝源河)、丙安河
- 水利水电 流体力学 外文文献 外文翻译 英文文献 混凝土重力坝基础流体力学行为分析
- 零星维修工程项目施工方案
- 物流公司超载超限整改报告
- 起重机安装施工记录表
- 江苏省高中学生学籍卡
- 碳排放问题的研究--数学建模论文
- 赢越酒会讲解示范
- 物业承接查验协议书
评论
0/150
提交评论