javascript實(shí)現(xiàn)倒計(jì)時(shí)并彈窗提示特效
在前端開發(fā)中,難免會(huì)用到倒計(jì)時(shí)。如做的雙十一活動(dòng),在距活動(dòng)開始的半個(gè)月前需要做些宣傳工作,需要告知用戶優(yōu)惠活動(dòng)什么時(shí)候開始。這個(gè)時(shí)候就要用到倒計(jì)時(shí),如在整站的某個(gè)頁面提醒用戶活動(dòng)什么時(shí)候開始等。而在活動(dòng)的后期,特別是在距活動(dòng)結(jié)束僅有1天左右時(shí),就要用到彈窗倒計(jì)時(shí)。這個(gè)倒計(jì)時(shí)設(shè)置在整站的首頁頂部(當(dāng)然也可以設(shè)置在其它地方,如首頁中部等),并設(shè)置彈窗彈出10秒后自動(dòng)消失,由用戶決定是否點(diǎn)擊到相應(yīng)的活動(dòng)頁面,購買產(chǎn)品。
需要的技術(shù)支持:CSS3,jQuery庫;
HTML代碼如下:
<section class="the_body"> <div class="countdown"> <h3>距中國雄于地球之日還有</h3> <div class="countdown_time"> <span class="the_days"><i>0</i><i>3</i></span> <i class="date_text">天</i> <span class="the_hours"><i>0</i><i>7</i></span> <i class="date_text">時(shí)</i> <span class="the_minutes"><i>4</i><i>7</i></span> <i class="date_text">分</i> <span class="the_seconds"><i>1</i><i>1</i></span> <i class="date_text">秒</i> </div> </div> </section>
css代碼如下:
.the_body{width: 100%;max-width: 640px;min-width: 320px;margin: 0 auto;} .countdown{background:#ffec20;padding: 10px 0;} .countdown h3{margin:0;padding:5px 0;color:#f53544;text-align:center;font-size:14px;} .countdown .countdown_time{display:block;width:100%;text-align:center;} .countdown .countdown_time i{display:inline-block;position:relative;padding:0 3px;font-style:normal;background:#fff; margin:0 2px;border-radius:3px;box-shadow:0px 1px 1px #ccc;border-bottom:1px solid #cfcfcf;font-weight :bold;} .countdown .countdown_time i:after{content:"";width:100%;border:1px solid #cfcfcf;border-width:1px 0 0;position:absolute; bottom:1px;left:0;} .countdown .countdown_time i:before{content:"";width:100%;border:1px solid #cfcfcf;border-width:1px 0 0;position:absolute; bottom:3px;left:0;} .countdown .countdown_time .date_text{background:transparent;font-weight:bold;box-shadow:none; border-bottom:none;text-decoration:none;padding: 0;} .countdown .countdown_time .date_text:after{content:"";border:none;} .countdown .countdown_time .date_text:before{content:"";border:none;}
JavaScript代碼如下:
<script> function remaintime() { var date = new Date("Jan 1,2015 00:00:00");//設(shè)置倒計(jì)時(shí)結(jié)束時(shí)間 var nowdate = new Date();//獲取當(dāng)前日期 var remaintime = date.getTime() - nowdate.getTime();//獲取現(xiàn)在到倒計(jì)時(shí)結(jié)束時(shí)間的毫秒數(shù) var remainday = Math.floor(remaintime / (1000 * 60 * 60 * 24));//計(jì)算求得剩余天數(shù) var remainhour = Math.floor((remaintime - remainday * 1000 * 60* 60 * 24)/ (1000 * 60 * 60));//計(jì)算求得剩余小時(shí)數(shù) var remainminute = Math.floor((remaintime - remainday * 1000 * 60* 60 * 24 - remainhour * 1000 * 60 * 60)/ (1000 * 60));//計(jì)算求得剩余分鐘數(shù) var remainsecond = Math.floor((remaintime - remainday * 1000 * 60 * 60 * 24- remainhour * 1000 * 60 * 60 - remainminute * 1000 * 60) / (1000));//計(jì)算求得剩余秒數(shù) //當(dāng)剩余天數(shù)小于10時(shí),就在其前加一個(gè)0,以下剩余小時(shí)數(shù)、分鐘數(shù)與秒數(shù)與此相同 if (remainday < 10) { remainday = "0" + remainday; }else{remainday+=""; //當(dāng)剩余天數(shù)大于10時(shí),剩余天數(shù)為數(shù)值,這是需要將該值轉(zhuǎn)換為字符串,以下的剩余小時(shí)數(shù)、分鐘數(shù)與秒數(shù)與此相同 } if (remainhour < 10) { remainhour = "0" + remainhour; }else{remainhour+="";} if (remainminute < 10) { remainminute = "0" + remainminute; }else{remainminute+="";} if (remainsecond < 10) { remainsecond = "0" + remainsecond; }else{remainsecond+="";} $(".the_days i:first-child").html(remainday.substr(0, 1)); $(".the_days i:last-child").html(remainday.substr(1, 2)); $(".the_hours i:first-child").html(remainhour.substr(0, 1)); $(".the_hours i:last-child").html(remainhour.substr(1, 2)); $(".the_minutes i:first-child").html(remainminute.substr(0, 1)); $(".the_minutes i:last-child").html(remainminute.substr(1, 2)); $(".the_seconds i:first-child").html(remainsecond.substr(0, 1)); $(".the_seconds i:last-child").html(remainsecond.substr(1, 2)); setTimeout("remaintime()",1000);//設(shè)置1秒后調(diào)用remaintime函數(shù) } remaintime(); setTimeout(function(){$(".countdown").hide();},10000);//在首頁設(shè)置的彈窗效果,在分頁這段代碼可以不設(shè)置 </script>
這是我自己寫的倒計(jì)時(shí)效果,當(dāng)然每個(gè)人都可以根據(jù)自己的愛好,設(shè)置倒計(jì)時(shí)的效果。如你可以只顯示“幾天幾時(shí)幾分”,但個(gè)人覺得沒有設(shè)置到“幾天幾時(shí)幾分幾秒”夠氣氛。這里的樣式也都可以根據(jù)自己的喜好改動(dòng),但最終的效果都是制造活動(dòng)開始前的火熱氛圍。
至于這里的html代碼、css代碼及JavaScript代碼需要注意的也說下:
1.html代碼就不多說,主要就是怎么設(shè)置dom,以易于JavaScript操作;
2.css代碼,這里主要用了:before與:after偽類,設(shè)置倒計(jì)時(shí)數(shù)值的立體效果;
3.JavaScript代碼也是很簡單的一個(gè)函數(shù),這里你需要將得到的剩余時(shí)間轉(zhuǎn)換為字符串,以便于字符串的截取顯示等。另外,用setTimeout函數(shù)設(shè)置隔1秒執(zhí)行一次函數(shù),以動(dòng)態(tài)顯示剩余時(shí)間,當(dāng)然也可以用setInterval函數(shù),這兩個(gè)函數(shù)設(shè)置的效果基本相同。
至此,一個(gè)簡單的倒計(jì)時(shí)效果就做出來了。如果要在首頁設(shè)置彈窗倒計(jì)時(shí),你可以把背景設(shè)置的更炫酷一點(diǎn),這樣可以吸引用戶點(diǎn)擊,并設(shè)置10秒后彈窗自動(dòng)消失(或者設(shè)置一個(gè)關(guān)閉按鈕等)。
倒計(jì)時(shí)的實(shí)現(xiàn)可以有很多種方式,在這里也就先介紹這一種,以后有時(shí)間將會(huì)繼續(xù)總結(jié)。
以上所述就是本文的全部內(nèi)容了,希望能夠?qū)Υ蠹伊私鈐avascript有所幫助。
- JS 倒計(jì)時(shí)實(shí)現(xiàn)代碼(時(shí)、分,秒)
- JS實(shí)現(xiàn)倒計(jì)時(shí)(天數(shù)、時(shí)、分、秒)
- 簡單易用的倒計(jì)時(shí)js代碼
- js代碼實(shí)現(xiàn)點(diǎn)擊按鈕出現(xiàn)60秒倒計(jì)時(shí)
- 2種簡單的js倒計(jì)時(shí)方式
- 原生JS實(shí)現(xiàn)簡單的倒計(jì)時(shí)功能示例
- js幾秒以后倒計(jì)時(shí)跳轉(zhuǎn)示例
- 一個(gè)不錯(cuò)的js html頁面倒計(jì)時(shí)可精確到秒
- js實(shí)現(xiàn)點(diǎn)擊獲取驗(yàn)證碼倒計(jì)時(shí)效果
- javascript實(shí)現(xiàn)倒計(jì)時(shí)提示框
相關(guān)文章
JS實(shí)現(xiàn)PC手機(jī)端和嵌入式滑動(dòng)拼圖驗(yàn)證碼三種效果
這篇文章主要介紹了JS實(shí)現(xiàn)PC手機(jī)端和嵌入式滑動(dòng)拼圖驗(yàn)證碼三種效果,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02以JSON形式將JS中Array對(duì)象數(shù)組傳至后臺(tái)的方法
業(yè)務(wù)是需要將前臺(tái)jQuery easyUI DataGrid列表中所選的若干行的數(shù)據(jù)傳到后臺(tái)進(jìn)行update操作,具體的實(shí)現(xiàn)如下,感興趣的朋友可以參考下2014-01-01Javascript 檢測(cè)鍵盤按鍵信息及鍵碼值對(duì)應(yīng)介紹
Javascript中有3個(gè)事件句柄在對(duì)應(yīng)鍵盤的輸入狀態(tài):按鍵被按下(按下按鍵但還沒有抬起)、點(diǎn)擊按鍵(按下并抬起按鍵)、按鍵抬起(按鍵抬起之后),接下來詳細(xì)介紹,感興趣的朋友可以了解下2013-01-01JS實(shí)現(xiàn)旋轉(zhuǎn)木馬式圖片輪播效果
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)旋轉(zhuǎn)木馬式圖片輪播效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01JavaScript操作XML 使用百度RSS作為新聞源示例
JavaScript操作XML 使用百度RSS作為新聞源示例,需要的朋友可以參考下2012-02-02