root / drupal7 / sites / all / modules / jquery_countdown_timer / js / jquery_countdown_timer.js @ a2bb1a14
1 |
/**
|
---|---|
2 |
* @name jQuery Countdown Plugin
|
3 |
* @author Martin Angelov
|
4 |
* @version 1.0
|
5 |
* @url http://tutorialzine.com/2011/12/countdown-jquery/
|
6 |
* @license MIT License
|
7 |
*/
|
8 |
|
9 |
(function($){ |
10 |
|
11 |
// Creating the plugin
|
12 |
$.fn.countdown = function(prop){ |
13 |
|
14 |
var options = $.extend({ |
15 |
callback : function(){}, |
16 |
timestamp : 0 |
17 |
},prop); |
18 |
|
19 |
var left, d, h, m, s, positions;
|
20 |
|
21 |
// Initialize the plugin
|
22 |
init(this, options);
|
23 |
|
24 |
positions = this.find('.position'); |
25 |
|
26 |
(function tick(){ |
27 |
|
28 |
// Time left
|
29 |
left = Math.floor((options.timestamp - (new Date())) / 1000); |
30 |
|
31 |
if(left < 0){ |
32 |
left = 0;
|
33 |
} |
34 |
|
35 |
var weeks = Math.floor(left/60/60/24/7), |
36 |
days = Math.floor(left/60/60/24) % 7, |
37 |
hours = Math.floor((left/(60*60)) % 24), |
38 |
minutes = Math.floor((left/60) % 60), |
39 |
seconds = left % 60;
|
40 |
|
41 |
updateDuo(0, 1, weeks); |
42 |
updateDuo(2, 3, days); |
43 |
updateDuo(4, 5, hours); |
44 |
updateDuo(6, 7, minutes); |
45 |
updateDuo(8, 9, seconds); |
46 |
|
47 |
// Calling an optional user supplied callback
|
48 |
options.callback(weeks, days, hours, minutes, seconds); |
49 |
|
50 |
// Scheduling another call of this function in 1s
|
51 |
setTimeout(tick, 1000);
|
52 |
})(); |
53 |
|
54 |
// This function updates two digit positions at once
|
55 |
function updateDuo(minor,major,value){ |
56 |
switchDigit(positions.eq(minor),Math.floor(value/10)%10); |
57 |
switchDigit(positions.eq(major),value%10);
|
58 |
} |
59 |
|
60 |
return this; |
61 |
}; |
62 |
|
63 |
|
64 |
function init(elem, options){ |
65 |
elem.addClass('countdownHolder');
|
66 |
|
67 |
// Creating the markup inside the container
|
68 |
$.each(['Weeks','Days','Hours','Minutes','Seconds'],function(i){ |
69 |
$('<span class="count'+this+'">'+ |
70 |
'<span class="position">'+
|
71 |
'<span class="digit static">0</span>'+
|
72 |
'</span>'+
|
73 |
'<span class="position">'+
|
74 |
'<span class="digit static">0</span>'+
|
75 |
'</span>'+
|
76 |
'</span>').appendTo(elem);
|
77 |
|
78 |
if(this!="Seconds"){ |
79 |
elem.append('<span class="countDiv countDiv'+i+'"></span>'); |
80 |
} |
81 |
}); |
82 |
|
83 |
} |
84 |
|
85 |
// Creates an animated transition between the two numbers
|
86 |
function switchDigit(position,number){ |
87 |
|
88 |
var digit = position.find('.digit') |
89 |
|
90 |
if(digit.is(':animated')){ |
91 |
return false; |
92 |
} |
93 |
|
94 |
if(position.data('digit') == number){ |
95 |
// We are already showing this number
|
96 |
return false; |
97 |
} |
98 |
|
99 |
position.data('digit', number);
|
100 |
|
101 |
var replacement = $('<span>',{ |
102 |
'class':'digit', |
103 |
css:{
|
104 |
top:'-2.1em', |
105 |
opacity:0 |
106 |
}, |
107 |
html:number
|
108 |
}); |
109 |
|
110 |
// The .static class is added when the animation
|
111 |
// completes. This makes it run smoother.
|
112 |
|
113 |
digit |
114 |
.before(replacement) |
115 |
.removeClass('static')
|
116 |
.animate({top:'2.5em',opacity:0},'fast',function(){ |
117 |
digit.remove(); |
118 |
}) |
119 |
|
120 |
replacement |
121 |
.delay(100)
|
122 |
.animate({top:0,opacity:1},'fast',function(){ |
123 |
replacement.addClass('static');
|
124 |
}); |
125 |
} |
126 |
})(jQuery); |