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

JavaScript用20行代碼實(shí)現(xiàn)虎年春節(jié)倒計(jì)時(shí)

 更新時(shí)間:2022年01月25日 16:32:07   作者:_超 夢(mèng)  
春節(jié)將至,相信大家跟小編一樣很激動(dòng)呀。為了迎接虎年春節(jié)到來(lái),小編為大家準(zhǔn)備了一個(gè)虎年春節(jié)倒計(jì)時(shí),僅20行代碼用js就實(shí)現(xiàn)啦。感興趣的可以動(dòng)手試一試

春節(jié)將至,小夢(mèng)相信大家跟小朦夢(mèng)一樣很激動(dòng)呀。為了迎接虎年春節(jié)到來(lái),小夢(mèng)擼了一個(gè)虎年春節(jié)倒計(jì)時(shí),僅20行代碼用js就實(shí)現(xiàn)啦,是不是很簡(jiǎn)單呢?我們用這20行代碼不僅能做個(gè)虎年春節(jié)倒計(jì)時(shí),還能從中學(xué)到JS相關(guān)知識(shí),一舉兩得!話不多說(shuō),接下來(lái)小伙伴們跟著小夢(mèng)一起也動(dòng)手試試吧!

虎年春節(jié)倒計(jì)時(shí)局部效果圖

虎年春節(jié)倒計(jì)時(shí)全局效果圖,灰常可愛(ài)的小老虎呀~

JS關(guān)鍵代碼實(shí)現(xiàn)

window.onload=function starttime(){
        // 2022年春節(jié)時(shí)間
        time(h1,'2022/2/1');
        // 計(jì)時(shí)器
        timer = setTimeout(starttime,1000); 
}
 
    function time(obj,springtime){
        // 獲取當(dāng)前時(shí)間
        var nowtime = new Date().getTime(); 
        // 獲取未來(lái)時(shí)間
        var futruetime =  new Date(springtime).getTime(); 
        // 獲取當(dāng)前時(shí)間到未來(lái)時(shí)間之差
        var mtime = futruetime-nowtime; 
        // 毫秒/1000
        var time = (mtime/1000);
        // 天   
        var day = parseInt(time/(24*60*60)); 
        // 小時(shí) 
        var hour = parseInt(time/(60*60))-24*day; 
        // 分   
        var minute = parseInt(time%3600/60);  
        var second = parseInt(time%60); 
        obj.innerHTML="<br>      虎年春節(jié)倒計(jì)時(shí):<br>     "+day+"天"+hour+"小時(shí)"+minute+"分"+second+"秒"+"<br><span><br>龍騰虎躍,虎虎虎虎!</span>";
        return true;
    }

上述代碼中實(shí)現(xiàn)倒計(jì)時(shí)的關(guān)鍵點(diǎn)在于setTimeout()函數(shù),setTimeout()函數(shù)是一個(gè)起到定時(shí)器的作用,接下來(lái)為小伙伴們簡(jiǎn)單講解一下該知識(shí)點(diǎn),在后面大家制作虎年春節(jié)倒計(jì)時(shí)的同時(shí),也能學(xué)習(xí)與復(fù)習(xí)相關(guān)知識(shí)點(diǎn)。

setTimeout()函數(shù)

setTimeout函數(shù)用來(lái)指定某個(gè)函數(shù)或某段代碼,在多少毫秒之后執(zhí)行。它返回一個(gè)整數(shù),表示定時(shí)器的編號(hào),以后可以用來(lái)取消這個(gè)定時(shí)器。

var timerId = setTimeout(func, delay)

在上面代碼中,setTimeout函數(shù)中有兩個(gè)參數(shù),第一個(gè)參數(shù)func是將要延遲執(zhí)行的函數(shù)或一段延遲執(zhí)行的代碼,第二個(gè)參數(shù)delay是延遲執(zhí)行的毫秒數(shù)。

timer = setTimeout(starttime,1000); 

在我們的虎年春節(jié)倒計(jì)時(shí)代碼中,starttime是要延遲執(zhí)行的函數(shù),1000是延遲1s,也就是每隔1秒執(zhí)行一次starttime函數(shù)來(lái)實(shí)現(xiàn)倒計(jì)時(shí)邏輯。

CSS樣式相關(guān)代碼

<style>
h1{
  position: fixed;
  top: 25%;
  left: 20;
  width: 100%;
  transform:translateY(-50%);
  font-family:'Courier New', Courier, monospace;
  font-size: 60px;
  color: #2c0712;
  padding: 0 20px;
}
h1 span{
  position: fixed;
  left: 0;
  width: 100%;
  text-align: center;
margin-top:250px;
    font-size:40px;
}
body{
    background-image: url("C:\\Users\\Administrator\\Desktop\\博客圖片\\虎虎.jpg"); 
    background-size: cover;
    background-repeat: no-repeat;
    background-position: top center;
    display: flex;
    flex-direction: column;
    align-items: center;
    min-height: 100vm;
    margin: 0;
</style>

CSS樣式用于控制我們的頁(yè)面規(guī)整美觀,與JS強(qiáng)強(qiáng)結(jié)合,在這里小夢(mèng)就不細(xì)說(shuō)CSS啦,后期肝一篇CSS相關(guān)內(nèi)容再分享給大家。

完整代碼貼附,復(fù)制到THML文件中就可以直接運(yùn)行啦!小伙伴們可以嘗試一下哦~,圖片資料有需要的話大家可以留言,小夢(mèng)看到會(huì)發(fā)給你滴~

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>虎年倒計(jì)時(shí)</title>
<style>
h1{
  position: fixed;
  top: 25%;
  left: 20;
  width: 100%;
  transform:translateY(-50%);
  font-family:'Courier New', Courier, monospace;
  font-size: 60px;
  color: #2c0712;
  padding: 0 20px;
}
h1 span{
  position: fixed;
  left: 0;
  width: 100%;
  text-align: center;
margin-top:250px;
    font-size:40px;
}
body{
    background-image: url("C:\\Users\\Administrator\\Desktop\\博客圖片\\虎虎.jpg"); 
    background-size: cover;
    background-repeat: no-repeat;
    background-position: top center;
    display: flex;
    flex-direction: column;
    align-items: center;
    min-height: 100vm;
    margin: 0;
</style>
 
</head>
<body bgcolor="red">
<h1 id="h1"></h1>
 
<script>
window.onload=function starttime(){
        // 2022年春節(jié)時(shí)間
        time(h1,'2022/2/1');
        // 計(jì)時(shí)器
        timer = setTimeout(starttime,1000); 
}
 
    function time(obj,springtime){
        // 獲取當(dāng)前時(shí)間
        var nowtime = new Date().getTime(); 
        // 獲取未來(lái)時(shí)間
        var futruetime =  new Date(springtime).getTime(); 
        // 獲取當(dāng)前時(shí)間到未來(lái)時(shí)間之差
        var mtime = futruetime-nowtime; 
        // 毫秒/1000
        var time = (mtime/1000);
        // 天   
        var day = parseInt(time/(24*60*60)); 
        // 小時(shí) 
        var hour = parseInt(time/(60*60))-24*day; 
        // 分   
        var minute = parseInt(time%3600/60);  
        var second = parseInt(time%60); 
        obj.innerHTML="<br>      虎年春節(jié)倒計(jì)時(shí):<br>     "+day+"天"+hour+"小時(shí)"+minute+"分"+second+"秒"+"<br><span><br>龍騰虎躍,虎虎虎虎!</span>";
        return true;
    }
</script>
</body>
</html>

到此這篇關(guān)于JavaScript用20行代碼實(shí)現(xiàn)虎年春節(jié)倒計(jì)時(shí)的文章就介紹到這了,更多相關(guān)JavaScript倒計(jì)時(shí)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論