javascript特效實現——目前時間和倒計時效果的簡單一實例_javascript技巧

來源:互聯網
上載者:User

這個效果的實現關鍵是對Date對象和setTimeout的使用。

一共有三個例子,HTML結構如下,就不添加CSS樣式了。

<body>  目前時間:<p id="p1"></p>  高考倒計時:<p id="p2"></p>  限時搶購:<p id="p3"></p></body>

主要體會javascript的實現

window.onload=function () {   var p1=document.getElementById("p1"),    p2=document.getElementById("p2");    p3=document.getElementById("p3");  showtime1();  showtime2();  showtime3();}

1.簡單的實現目前時間的顯示

function showtime1() {   var nowdate=new Date();//建立Date對象nowdate,以擷取目前時間   var year=nowdate.getFullYear(),//擷取年份     month=nowdate.getMonth()+1,//擷取月份,getMonth()得到的是0-11,需要加1     date=nowdate.getDate(),//擷取日份     day=nowdate.getDay(),//擷取一周中的某一天,getDay()得到的是0-6     week=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"],     h=nowdate.getHours(),     m=nowdate.getMinutes(),     s=nowdate.getSeconds(),     h=checkTime(h),//函數checkTime用于格式化時,分,秒     m=checkTime(m),     s=checkTime(s);   p1.innerHTML=year+"年"+month+"月"+date+"日"+week[day]+h+":"+m+":"+s;   setTimeout(showtime1, 1000);//反覆執行函數本身 }

其中的checkTime函數如下:

function checkTime(i) {  if (i<10) {   i="0"+i;  }  return i;}

因為平時看到的時間格式一般是00:00:01,而getHours,getMinutes,getSeconds得到格式是0到9,不是00到09這樣的格式。所以在從9變成10的過程中,有一位元,變成兩位元,同樣在充59秒變為0秒或者59分變為0分或者23時變為0時。

比如 23:59:59 再下一秒 應該變為00:00:00;若未使用checkTime函數進行處理,則會變為0:0:0,這樣格式上就有點不統一,而且視覺上也有字數增加或減少的突變。(後面兩個例子就不用checkTime函數對時分秒進行處理了!!!)

2.高考倒計時效果實現

function showTime2() {  var nowtime=new Date(),//擷取目前時間    endtime=new Date("2017/6/6");//定義結束時間  var lefttime=endtime.getTime()-nowtime.getTime(),//距離結束時間的毫秒數    leftd=Math.floor(lefttime/(1000*60*60*24)),//計算天數    lefth=Math.floor(lefttime/(1000*60*60)%24),//計算時數    leftm=Math.floor(lefttime/(1000*60)%60),//計算分鐘數    lefts=Math.floor(lefttime/1000%60);//計算秒數  p2.innerHTML=leftd+"天"+lefth+":"+leftm+":"+lefts;  setTimeout(showTime2, 1000);  }

其中比較重要的幾點:

① 定義結束的時間endtime=new Date("2017/6/6")是通過new一個帶有參數的Date對象,直接 new Date()則是直接擷取到當前的時間;

② getTime()方法得到的是距離1970 年 1 月 1 日的毫秒數。

③天數,小時數,分鐘數和秒數的計算,%(模數運算)。得到距離結束時間的毫秒數(剩餘毫秒數),除以1000得到剩餘秒數,再除以60得到剩餘分鐘數,再除以60得到剩餘小時數。除以24得到剩餘天數。剩餘秒數 lefttime/1000 模60得到秒數,剩餘分鐘數 lefttime/(1000*60) 模60得到分鐘數,剩餘小時數模 lefttime/(1000*60*60) 模24得到小時數。

3.限時搶購倒計時效果

function showtime3() {  var nowtime=new Date(),    endtime=new Date("2020/1/1,00:00:00"),//設定結束時間    lefttime=parseInt((endtime.getTime()-nowtime.getTime())/1000),    d=Math.floor(lefttime/(60*60*24)),    h=Math.floor(lefttime/(60*60)%24),    m=Math.floor(lefttime/60%60),    s=Math.floor(lefttime%60);  p3.innerHTML=d+"天"+h+"小時"+m+"分"+s+"秒";  setTimeout(showtime3, 1000);  }

其實和第二個例子大同小異,區別是結束時間的設定 new Date("2020/1/1,00:00:00")

下面是完整的代碼

<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>倒計時效果</title>  <script type="text/javascript">    function checkTime(i) {      if (i<10) {        i="0"+i;      }      return i;    }    window.onload=function () {            var p1=document.getElementById("p1"),        p2=document.getElementById("p2");      showtime1();      showtime2();      showtime3();     }    function showtime1() {      var nowdate=new Date();      var year=nowdate.getFullYear(),//年份      month=nowdate.getMonth()+1,//月份      date=nowdate.getDate(),//日      week=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"],      day=nowdate.getDay(),//getDay擷取0-6      h=nowdate.getHours(),      h=checkTime(h),      m=nowdate.getMinutes(),      m=checkTime(m),      s=nowdate.getSeconds(),      s=checkTime(s);     p1.innerHTML=year+"年"+month+"月"+date+"日"+week[day]+h+":"+m+":"+s;     setTimeout(showtime1, 1000);     }     function showtime2() {      var nowtime=new Date(),        endtime=new Date("2017/6/6");      var lefttime=endtime.getTime()-nowtime.getTime(),        leftd=Math.floor(lefttime/(1000*60*60*24)),        lefth=Math.floor(lefttime/(1000*60*60)%24),        leftm=Math.floor(lefttime/(1000*60)%60),        lefts=Math.floor(lefttime/1000%60);      p2.innerHTML=leftd+"天"+lefth+":"+leftm+":"+lefts;      setTimeout(showtime2, 1000);       }     function showtime3() {      var nowtime=new Date(),        endtime=new Date("2020/1/1,00:00:00"),        lefttime=parseInt((endtime.getTime()-nowtime.getTime())/1000),        d=Math.floor(lefttime/(60*60*24)),        h=Math.floor(lefttime/(60*60)%24),        m=Math.floor(lefttime/60%60),        s=Math.floor(lefttime%60);      p3.innerHTML=d+"天"+h+"小時"+m+"分"+s+"秒";      setTimeout(showtime3, 1000);       }  </script></head><body>  目前時間:<p id="p1"></p>  高考倒計時:<p id="p2"></p>  限時搶購:<p id="p3"></p></body></html>

以上這篇javascript特效實現——目前時間和倒計時效果的簡單一實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.