DIV CSS 佈局教程網

用 JavaScript 實作倒數計時並彈窗提示的特效
編輯:關於JavaScript     

在前端開發中,難免會用到倒數計時。例如做雙十一活動,在距離活動開始的半個月前需要做些宣傳,告知使用者優惠活動何時開始,這時就會用到倒數計時——例如在整站某個頁面提醒使用者活動何時開始。而在活動後期,特別是距離結束僅剩一天左右時,就要用到彈窗倒數計時:把這個倒數計時設在整站首頁頂部(當然也能設在其他地方,如首頁中部),並讓彈窗在彈出 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();//獲取現在到倒計時結束時間的毫秒數 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 代碼是一個很簡單的函式,重點是把算出的剩餘時間轉為字串,方便用 substr 擷取並分別顯示到「天、時、分、秒」的每個位數上。另外用 setTimeout 每 1 秒執行一次以動態更新剩餘時間,當然也可以用 setInterval,兩者效果基本相同。

至此,一個簡單的倒數計時效果就完成了。若要在首頁做彈窗倒數計時,可以把背景做得更炫一點以吸引點擊,並設定 10 秒後自動消失(或加一個關閉按鈕)。倒數計時的實作方式有很多種,這裡先介紹這一種,以後有時間會再繼續補充。以上所述就是本文的全部內容,希望能對大家了解 JavaScript 有所幫助。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved