购物商城商品倒计时

Js:

 1 /**
 2  * Author: Black_Jay郗
 3  * downCount: 时间转换  倒计时
 4  */
 5 (function ($) {
 6     $.fn.downCount = function (options, callback) {
 7         var settings = $.extend({
 8                 date: null,
 9                 offset: null
10             }, options);
11         if (!settings.date) {
12             $.error('Date is not defined.');
13         }
14         if (!Date.parse(settings.date)) {
15             $.error('Incorrect date format, it should look like this, 12/24/2012 12:00:00.');
16         }
17         var container = this;
18 
19         var currentDate = function () {
20             var date = new Date();
21             /*var utc = date.getTime() + (date.getTimezoneOffset() * 60000);
22             var new_date = new Date(utc + (3600000*settings.offset));*/
23             return date;
24         };
25         function countdown () {
26             var target_date = new Date(settings.date),
27                 current_date = currentDate();
28             var difference = target_date - current_date;
29             if (difference < 0) {
30                 clearInterval(interval);//取消 setInterval() 函数设定的定时执行操作
31                 if (callback && typeof callback === 'function') callback();
32                 return;
33             }
34             var _second = 1000,
35                 _minute = _second * 60,
36                 _hour = _minute * 60,
37                 _day = _hour * 24;
38 
39             var days = Math.floor(difference / _day),
40                 hours = Math.floor(((difference % _day) / _hour) + (days*24)),
41                 minutes = Math.floor((difference % _hour) / _minute),
42                 seconds = Math.floor((difference % _minute) / _second);
43                 days = (String(days).length >= 2) ? days : '0' + days;
44                 hours = (String(hours).length >= 2) ? hours : '0' + hours;
45                 minutes = (String(minutes).length >= 2) ? minutes : '0' + minutes;
46                 seconds = (String(seconds).length >= 2) ? seconds : '0' + seconds;
47             container.find('.hours').text(hours);
48             container.find('.minutes').text(minutes);
49             container.find('.seconds').text(seconds);
50         };
51         var interval = setInterval(countdown, 1000);
52     };
53 })(jQuery);

html:

1 <!-- 倒计时显示Star -->
2 <p class="countdown">
3     <span class="hours">00</span>:
4     <span class="minutes">00</span>:
5     <span class="seconds">00</span>
6 </p>
7 <!-- 倒计时End -->

最后输入你想要的结束时间

JS:

$('.countdown').downCount({
    date: '11/09/2016 13:45:00',
    offset: +10
}, function () {
    alert('秒杀已结束');
});

 

posted @ 2016-11-09 17:16  Black_Jay  阅读(412)  评论(0编辑  收藏  举报