html购物倒计时,基于javascript实现的购物商城商品倒计时实例

话不多说,下面跟着小编一起来看下实例代码吧

Js:

/**

* Author: Black_Jay郗

* downCount: 时间转换 倒计时

*/

(function ($) {

$.fn.downCount = function (options, callback) {

var settings = $.extend({

date: null,

offset: null

}, options);

if (!settings.date) {

$.error('Date is not defined.');

}

if (!Date.parse(settings.date)) {

$.error('Incorrect date format, it should look like this, 12/24/2012 12:00:00.');

}

var container = this;

var currentDate = function () {

var date = new Date();

/*var utc = date.getTime() + (date.getTimezoneOffset() * 60000);

var new_date = new Date(utc + (3600000*settings.offset));*/

return date;

};

function countdown () {

var target_date = new Date(settings.date),

current_date = currentDate();

var difference = target_date - current_date;

if (difference < 0) {

clearInterval(interval);//取消 setInterval() 函数设定的定时执行操作

if (callback && typeof callback === 'function') callback();

return;

}

var _second = 1000,

_minute = _second * 60,

_hour = _minute * 60,

_day = _hour * 24;

var days = Math.floor(difference / _day),

hours = Math.floor(((difference % _day) / _hour) + (days*24)),

minutes = Math.floor((difference % _hour) / _minute),

seconds = Math.floor((difference % _minute) / _second);

days = (String(days).length >= 2) ? days : '0' + days;

hours = (String(hours).length >= 2) ? hours : '0' + hours;

minutes = (String(minutes).length >= 2) ? minutes : '0' + minutes;

seconds = (String(seconds).length >= 2) ? seconds : '0' + seconds;

container.find('.hours').text(hours);

container.find('.minutes').text(minutes);

container.find('.seconds').text(seconds);

};

var interval = setInterval(countdown, 1000);

};

})(jQuery);

html:

00:

00:

00

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

JS:

$('.countdown').downCount({

date: '11/09/2016 13:45:00',

offset: +10

}, function () {

alert('秒杀已结束');

});

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值