js倒計時程式碼 適合於本月促銷-倒計時程式碼
阿新 • • 發佈:2019-01-06
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>無標題文件</title> </head> <script src="jquery.SuperSlide.2.1.1.source.js"></script> <body> <div class="clockContainer clockContainer1 fl relative" data-timer="2016/3/31 23:59:59"> <span class="day fl timer">00</span><em class="tag fl">天</em> <span class="hour fl timer">00</span><em class="tag fl">時</em> <span class="minute fl timer">00</span><em class="tag fl">分</em> <span class="second fl timer">00</span><em class="tag fl">秒</em> </div> <script src="jquery-1.9.1.min.js"></script> <script src="aa.js"></script> <script> $(function(){ //new countDownTimer('.clockContainer'); $(".clockContainer").countDownTimer(); }) </script> </body> </html>
js部分程式碼
程式原理。當前日期與當月最後一天的倒計時程式碼.// JavaScript Document (function($){ $.fn.countDownTimer=function(options){ var options = $.extend({},options); return this.each(function(){ var _this = $(this); var time =_this.attr("data-timer"); var day_elem = _this.find('.day'); var hour_elem = _this.find('.hour'); var minute_elem = _this.find('.minute'); var second_elem = _this.find('.second'); var end_time = new Date(time).getTime(),//月份是實際月份-1 sys_second = (end_time-new Date().getTime())/1000; var timer = setInterval(function(){ if (sys_second > 1) { sys_second -= 1; var day = Math.floor((sys_second / 3600) / 24); var hour = Math.floor((sys_second / 3600) % 24); var minute = Math.floor((sys_second / 60) % 60); var second = Math.floor(sys_second % 60); day_elem && $(day_elem).text(day<10?"0"+day:day);//計算天 $(hour_elem).text(hour<10?"0"+hour:hour);//計算小時 $(minute_elem).text(minute<10?"0"+minute:minute);//計算分鐘 $(second_elem).text(second<10?"0"+second:second);//計算秒殺 } else { clearInterval(timer); } }, 1000); }); } })(jQuery);
比如開始時間是3.1號。那活動截止日期是3月最後一天,也就是31號截止。