javascript實現(xiàn)倒計時并彈窗提示特效
在前端開發(fā)中,難免會用到倒計時。如做的雙十一活動,在距活動開始的半個月前需要做些宣傳工作,需要告知用戶優(yōu)惠活動什么時候開始。這個時候就要用到倒計時,如在整站的某個頁面提醒用戶活動什么時候開始等。而在活動的后期,特別是在距活動結束僅有1天左右時,就要用到彈窗倒計時。這個倒計時設置在整站的首頁頂部(當然也可以設置在其它地方,如首頁中部等),并設置彈窗彈出10秒后自動消失,由用戶決定是否點擊到相應的活動頁面,購買產品。
需要的技術支持: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">時</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");//設置倒計時結束時間
var nowdate = new Date();//獲取當前日期
var remaintime = date.getTime() - nowdate.getTime();//獲取現(xiàn)在到倒計時結束時間的毫秒數
var remainday = Math.floor(remaintime / (1000 * 60 * 60 * 24));//計算求得剩余天數
var remainhour = Math.floor((remaintime - remainday * 1000 * 60* 60 * 24)/ (1000 * 60 * 60));//計算求得剩余小時數
var remainminute = Math.floor((remaintime - remainday * 1000 * 60* 60 * 24 - remainhour * 1000 * 60 * 60)/ (1000 * 60));//計算求得剩余分鐘數
var remainsecond = Math.floor((remaintime - remainday * 1000 * 60 * 60 * 24- remainhour * 1000 * 60 * 60 - remainminute *
1000 * 60) / (1000));//計算求得剩余秒數
//當剩余天數小于10時,就在其前加一個0,以下剩余小時數、分鐘數與秒數與此相同
if (remainday < 10) {
remainday = "0" + remainday;
}else{remainday+="";
//當剩余天數大于10時,剩余天數為數值,這是需要將該值轉換為字符串,以下的剩余小時數、分鐘數與秒數與此相同
}
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);//設置1秒后調用remaintime函數
}
remaintime();
setTimeout(function(){$(".countdown").hide();},10000);//在首頁設置的彈窗效果,在分頁這段代碼可以不設置
</script>
這是我自己寫的倒計時效果,當然每個人都可以根據自己的愛好,設置倒計時的效果。如你可以只顯示“幾天幾時幾分”,但個人覺得沒有設置到“幾天幾時幾分幾秒”夠氣氛。這里的樣式也都可以根據自己的喜好改動,但最終的效果都是制造活動開始前的火熱氛圍。
至于這里的html代碼、css代碼及JavaScript代碼需要注意的也說下:
1.html代碼就不多說,主要就是怎么設置dom,以易于JavaScript操作;
2.css代碼,這里主要用了:before與:after偽類,設置倒計時數值的立體效果;
3.JavaScript代碼也是很簡單的一個函數,這里你需要將得到的剩余時間轉換為字符串,以便于字符串的截取顯示等。另外,用setTimeout函數設置隔1秒執(zhí)行一次函數,以動態(tài)顯示剩余時間,當然也可以用setInterval函數,這兩個函數設置的效果基本相同。
至此,一個簡單的倒計時效果就做出來了。如果要在首頁設置彈窗倒計時,你可以把背景設置的更炫酷一點,這樣可以吸引用戶點擊,并設置10秒后彈窗自動消失(或者設置一個關閉按鈕等)。
倒計時的實現(xiàn)可以有很多種方式,在這里也就先介紹這一種,以后有時間將會繼續(xù)總結。
以上所述就是本文的全部內容了,希望能夠對大家了解javascript有所幫助。
相關文章
JS實現(xiàn)HTML頁面中動態(tài)顯示當前時間完整示例
這篇文章主要介紹了JS實現(xiàn)HTML頁面中動態(tài)顯示當前時間,結合完整實例形式分析了JavaScript使用時間函數setTimeout及clearTimeout動態(tài)顯示當前時間相關操作技巧,非常簡單實用,需要的朋友可以參考下2018-07-07
JavaScript中全局變量、函數內變量以及常量表達式的效率測試
直接用字符串常量要比利用全局變量快,但創(chuàng)建正則表達式就比起用全局變量要慢上很多了。2009-11-11
通過Tabs方法基于easyUI+bootstrap制作工作站
本教程給大家介紹如何制作easyUI+bootstrap工作站,主要學習tabs方法,本文介紹非常詳細,具有參考借鑒價值,需要的朋友參考下吧2016-03-03
js中Math之random,round,ceil,floor的用法總結
本篇文章是對js中Math之random,round,ceil,floor的用法進行了總結介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-12-12
最全的JavaScript開發(fā)工具列表 總有一款適合你
最全的JavaScript開發(fā)工具列表分享給你,總有一款適合你!2017-06-06

