欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

javascript實(shí)現(xiàn)倒計(jì)時(shí)并彈窗提示特效

 更新時(shí)間:2015年06月05日 11:34:40   投稿:hebedich  
倒計(jì)時(shí)的功能在我們做項(xiàng)目的時(shí)候會(huì)經(jīng)常遇到,這里給大家分享的是個(gè)人編寫的一個(gè)簡易的效果代碼,有需要的小伙伴可以參考下

在前端開發(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有所幫助。

相關(guān)文章

最新評(píng)論