-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery.MyDigitClock.js
95 lines (82 loc) · 1.98 KB
/
jquery.MyDigitClock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* @author Paul Chan / KF Software House
* Homepage: http://www.kfsoft.info
*
* Version 0.5
* Copyright (c) 2010 KF Software House
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
*/
(function($) {
var _options = {};
var _container = {};
jQuery.fn.MyDigitClock = function(options) {
var id = $(this).get(0).id;
_options[id] = $.extend({}, $.fn.MyDigitClock.defaults, options);
return this.each(function()
{
_container[id] = $(this);
showClock(id);
});
function showClock(id)
{
var d = new Date;
var h = d.getHours();
var m = d.getMinutes();
var s = d.getSeconds();
var ampm = "";
if (_options[id].bAmPm)
{
if (h>12)
{
h = h-12;
ampm = " PM";
}
else
{
ampm = " AM";
}
}
var templateStr = _options[id].timeFormat + ampm;
templateStr = templateStr.replace("{HH}", getDD(h));
templateStr = templateStr.replace("{MM}", getDD(m));
templateStr = templateStr.replace("{SS}", getDD(s));
var obj = $("#"+id);
obj.css("fontSize", _options[id].fontSize);
obj.css("fontFamily", _options[id].fontFamily);
obj.css("color", _options[id].fontColor);
obj.css("background", _options[id].background);
obj.css("fontWeight", _options[id].fontWeight);
//change reading
obj.html(templateStr)
//toggle hands
if (_options[id].bShowHeartBeat)
{
obj.find("#ch1").fadeTo(800, 0.1);
obj.find("#ch2").fadeTo(800, 0.1);
}
setTimeout(function(){showClock(id)}, 1000);
}
function getDD(num)
{
return (num>=10)?num:"0"+num;
}
function refreshClock()
{
setupClock();
}
}
//default values
jQuery.fn.MyDigitClock.defaults = {
fontSize: '50px',
fontFamily: 'Microsoft JhengHei, Century gothic, Arial',
fontColor: '#ff2200',
fontWeight: 'bold',
background: '#fff',
timeFormat: '{HH}<span id="ch1">:</span>{MM}<span id="ch2">:</span>{SS}',
bShowHeartBeat: false,
bAmPm:false
};
})(jQuery);