jQuery實(shí)現(xiàn)每日秒殺商品倒計(jì)時(shí)功能
html & js:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>jQuery每日秒殺倒計(jì)時(shí)</title>
<link rel="stylesheet" href="css/zzsc.css" rel="external nofollow" >
</head>
<body>
<div class="se-kl">
<div class="se-cn">倒計(jì)時(shí)</div>
<div class="se-en">COUNT DOWN</div>
<i class="se-io"></i>
<div class="se-info">距離結(jié)束還剩</div>
<div class="se-count">
<div class="se-day"></div>
<div class="se-hour"><span class="se-txt">00</span></div>
<div class="se-min"><span class="se-txt">00</span></div>
<div class="se-sec"><span class="se-txt">00</span></div>
</div>
</div>
<script src="js/jquery.min.js"></script> <!-- 依賴條件1 -->
<script type="text/javascript">
$(document).ready(function () {
var oDate = new Date();
var nowTime = oDate.getTime(); //現(xiàn)在的毫秒數(shù)
oDate.setDate(oDate.getDate() + 1); // 設(shè)定截止時(shí)間為第二天
var targetDate = new Date(oDate.toLocaleDateString());
run(targetDate);
});
function run(enddate) {
getDate(enddate);
setInterval("getDate('" + enddate + "')", 500);
}
function getDate(enddate) {
var oDate = new Date(); //獲取日期對(duì)象
var nowTime = oDate.getTime(); //現(xiàn)在的毫秒數(shù)
var enddate = new Date(enddate);
var targetTime = enddate.getTime(); // 截止時(shí)間的毫秒數(shù)
var second = Math.floor((targetTime - nowTime) / 1000); //截止時(shí)間距離現(xiàn)在的秒數(shù)
var day = Math.floor(second / 24 * 60 * 60); //整數(shù)部分代表的是天;一天有24*60*60=86400秒 ;
second = second % 86400; //余數(shù)代表剩下的秒數(shù);
var hour = Math.floor(second / 3600); //整數(shù)部分代表小時(shí);
second %= 3600; //余數(shù)代表 剩下的秒數(shù);
var minute = Math.floor(second / 60);
second %= 60;
var spanH = $('.se-txt')[0];
var spanM = $('.se-txt')[1];
var spanS = $('.se-txt')[2];
spanH.innerHTML = tow(hour);
spanM.innerHTML = tow(minute);
spanS.innerHTML = tow(second);
}
function tow(n) {
return n >= 0 && n < 10 ? '0' + n : '' + n;
}
</script>
</body>
</html>
css:
(zzsc.cs)
* {
margin: 0;
padding: 0;
}
.se-kl {
width: 190px;
height: 275px;
background-color: #e83632;
margin: 100px auto;
position: relative;
}
.se-cn {
position: absolute;
top: 42px;
left: 0;
width: 100%;
text-align: center;
font-size: 34px;
color: #fff;
}
.se-en {
position: absolute;
top: 90px;
left: 0;
width: 100%;
text-align: center;
font-size: 20px;
color: rgba(255, 255, 255, 0.5);
}
.se-io {
width: 20px;
height: 33px;
position: absolute;
background: url(../imgs/seckill.png) no-repeat;
background-position: -32.5px 0;
background-size: 52.5px 40px;
left: 85px;
top: 126px;
display: block;
}
.se-info {
position: absolute;
top: 170px;
text-align: center;
width: 100%;
font-size: 16px;
color: #fff;
}
.se-count {
position: absolute;
top: 212px;
left: 30px;
height: 40px;
}
.se-day {
display: none;
}
.se-hour,
.se-min,
.se-sec {
position: relative;
background-color: #2f3430;
width: 40px;
height: 40px;
float: left;
text-align: center;
line-height: 40px;
margin-right: 5px;
}
.se-txt {
font-size: 20px;
font-weight: bold;
color: #fff;
}
.se-txt:before {
content: "";
display: block;
position: absolute;
top: 50%;
left: 0;
width: 100%;
height: 1px;
background-color: #e83632;
}
示例圖1:

注:本程序依賴于jquery庫(kù),請(qǐng)自行下載并選擇合適路徑或使用遠(yuǎn)程cdn地址:
https://cdn.bootcss.com/jquery/3.4.1/jquery.js
總結(jié)
以上所述是小編給大家介紹的jQuery實(shí)現(xiàn)每日秒殺商品倒計(jì)時(shí)功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
jquery實(shí)現(xiàn)煙花效果(面向?qū)ο?
這篇文章主要為大家詳細(xì)介紹了jquery面向?qū)ο髮?shí)現(xiàn)煙花效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
jquery mobile changepage的三種傳參方法介紹
本來(lái)覺得changePage 那么多option,傳幾個(gè)參數(shù)應(yīng)該沒問(wèn)題結(jié)果翻遍國(guó)內(nèi)外網(wǎng)站,基本方法只有三種,下面與大家分享下,感興趣的朋友可以參考下2013-09-09
如何使用CSS3和JQuery easing 插件制作絢麗菜單
這篇文章主要介紹了如何使用CSS3和JQuery easing 插件制作絢麗菜單,這樣做可以讓有菜單的盒子滑出,并且彈出縮略圖。在某些菜單項(xiàng)中我們還包含著有進(jìn)一步鏈接的子菜單。取決于我們鼠標(biāo)在菜單項(xiàng)上的停懸,子菜單將向左或向右滑動(dòng)。,需要的朋友可以參考下2019-06-06
基于Jquery實(shí)現(xiàn)表單驗(yàn)證
本文給大家分享的是一段基于Jquery實(shí)現(xiàn)表單驗(yàn)證的代碼,非常簡(jiǎn)單實(shí)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-08-08
動(dòng)態(tài)標(biāo)簽 懸停效果 延遲加載示例代碼
懸停效果、延遲加載想必大家都有見到過(guò)吧,在本文將為大家介紹下是如何實(shí)現(xiàn)的,感興趣的朋友不要錯(cuò)過(guò)2013-11-11
jQuery(js)獲取文字寬度(顯示長(zhǎng)度)示例代碼
今天遇到了獲取文字寬度的問(wèn)題,在網(wǎng)上找到了不錯(cuò)的方法并成功使用到了項(xiàng)目中,有類似情況的朋友可以參考下2013-12-12
jquery利用event.which方法獲取鍵盤輸入值的代碼
jquery利用event.which方法獲取鍵盤輸入值的代碼,需要的朋友可以參考下。2011-10-10
jquery移除button的inline onclick事件(已測(cè)試及兼容瀏覽器)
這種方法在Google Chrome下正常工作,但是在IE的兼容模式下會(huì)馬上調(diào)用reclick方法,這不是我們想要的效果;為了解決這個(gè)問(wèn)題,我們可以換個(gè)思路,就是延遲綁定click事件2013-01-01

