-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.countdown.js
136 lines (90 loc) · 3.53 KB
/
jquery.countdown.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
countdown 1.0 - jQuery plugin
written by O! Interactive, Acuna; Thanks to Martin Angelov
http://ointeractive.ru
Copyright (c) 2015 O! Interactive, Acuna (http://ointeractive.ru)
Dual licensed under the MIT (MIT-LICENSE.txt)
and GPL (GPL-LICENSE.txt) licenses.
Built for jQuery library
http://jquery.com
1.0 09.08.2015
Первый приватный релиз
*/
(function ($) {
$.fn.countdown = function (options) {
options = $.extend ({
'timestamp': 0,
'callback': function () {},
}, options);
var
days = 24 * 60 * 60,
hours = 60 * 60,
minutes = 60;
$(this).each (function () {
var self = $(this);
(function tick () {
var left, d, h, m, s;
// Осталось времени
left = Math.floor ((options.timestamp - (new Date ())) / 1000);
if (left < 0) left = 0;
// Осталось дней
d = Math.floor (left / days);
updateDuo (0, 1, d);
left -= d * days;
// Осталось часов
h = Math.floor (left / hours);
updateDuo (2, 3, h);
left -= h * hours;
// Осталось минут
m = Math.floor (left / minutes);
updateDuo (4, 5, m);
left -= m * minutes;
// Осталось секунд
s = left;
updateDuo (6, 7, s);
options.callback (d, h, m, s);
setTimeout (tick, 1000); // Повторяем вызов каждую секунду
})();
function switchDigit (position, number) {
var digit = position.find ('.digit');
if (digit.is (':animated')) return false;
if (position.data ('digit') == number) return false;
position.data ('digit', number);
var replacement = $('<span>', {
'class': 'digit',
'css': { 'top':'-2.1em', 'opacity': 0 },
'html': number
});
digit
.before (replacement)
.removeClass ('static')
.animate ({ 'top':'2.5em', 'opacity':0 }, 'fast', function () {
digit.remove ();
});
replacement
.delay (100)
.animate ({ 'top':0, 'opacity':1 }, 'fast', function () {
replacement.addClass ('static');
});
};
function updateDuo (minor, major, value) {
var positions = self.find ('.position');
switchDigit (positions.eq (minor), Math.floor (value / 10) % 10);
switchDigit (positions.eq (major), value % 10);
};
self.addClass ('countdownHolder');
$.each (['Days', 'Hours', 'Minutes', 'Seconds'], function (i) {
$('<span class="count' + this + '">').html (
'<span class="position">\
<span class="digit static">0</span>\
</span>\
<span class="position">\
<span class="digit static">0</span>\
</span>'
).appendTo (self);
if (this != 'Seconds')
self.append ('<span class="countDiv countDiv' + i + '"></span>');
});
});
};
})($);