Html+Css+js實現(xiàn)春節(jié)倒計時效果(移動端和PC端)

PK創(chuàng)意鬧新春,我正在參加「春節(jié)創(chuàng)意投稿大賽」鸥昏,詳情請看:春節(jié)創(chuàng)意投稿大賽

前言

我是歌謠 我有個兄弟 巔峰的時候排名c站總榜19 叫前端小歌謠 曾經(jīng)我花了三年的時間創(chuàng)作了他 現(xiàn)在我要用五年的時間超越他 今天又是接近兄弟的一天人生難免坎坷 大不了從頭再來 歌謠的意志是永恒的 放棄很容易 但是堅持一定很酷

導語

歌謠 歌謠 新的一年就要到了 可否寫一個春節(jié)的倒計時呢 我想看看哪天可以過春節(jié)

效果預覽

[圖片上傳失敗...(image-6e0f73-1682041429064)][圖片上傳失敗...(image-473fc7-1682041429064)]

html部分

<!DOCTYPE html>
<!--geyao-->
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="css/mobile.css">
    <title>春節(jié)倒計時</title>
  </head>
  <body>
    <div class="container">
      <h2><span id="title">春節(jié)倒計時</span>2022</h2>
      <div class="countdown">
        <div id="day">--</div>
        <div id="hour">--</div>
        <div id="minute">--</div>
        <div id="second">--</div>
      </div>
      <!-- 手動切換不好看 直接加定時器切換 微信公眾號關注前端小歌謠
       -->
      <!-- <div id="btn">切換背景</div> -->
    </div>
      <script  src="js/script.js"></script>
 
  </body>
</html>

[圖片上傳失敗...(image-dc34d3-1682041429064)]

移動端樣式(mobile.css)

@media screen and (max-width: 1025px) {
    * {
        margin: 0;
        padding: 0;
    }
    body {
        background: rgb(129, 155, 190) url(../image/geyao1.jpg);
        background-size: cover;
        background-position: center center;
        height: 100%;
    }
    .container {
        margin: 0;
        color: #fff;
        line-height: normal;
        position: absolute;
        align-items: center;
        left: 5%;
        right: 5%;
    }
    .container h2 {
        font-size: 6em;
        text-align: center;
        margin: 10% 0;
        color: #fff;
    }
    .container h2 span {
        color: #fff;
        display: block;
        text-align: center;
        font-size: 0.3em;
        font-weight: 300;
        letter-spacing: 2px;
    }
    .countdown {
        display: flex;
        justify-content: space-around;
        margin: 0;
    }
    .countdown div {
        width: 20%;
        height: 13vw;
        margin: 0 10px;
        line-height: 13vw;
        font-size: 2em;
        position: relative;
        text-align: center;
        background: #333333;
        color: #ffffff;
        font-weight: 500;
        border-radius: 10px 10px 0 0;
    }
    .countdown div:before {
        content: '';
        position: absolute;
        bottom: -30px;
        left: 0;
        width: 100%;
        height: 30px;
        background: #b00000;
        color: #ffffff;
        font-size: 0.4em;
        line-height: 35px;
        font-weight: 300;
        border-radius: 0 0 10px 10px;
    }
    .countdown #day:before {
        content: '天';
    }
    .countdown #hour:before {
        content: '時';
    }
    .countdown #minute:before {
        content: '分';
    }
    .countdown #second:before {
        content: '秒';
    }
}

[圖片上傳失敗...(image-47f069-1682041429064)]

pc端樣式(style.css)


* {
    margin: 0;
    padding: 0;
    font-family: 'Poppins', sans-serif;
}
@media screen and (min-width: 1025px) {
    body {
        background: rgb(129, 155, 190) url(../image/geyao1.jpg);
        background-attachment: fixed;
        background-size: cover;
        -webkit-background-size: cover;
        -o-background-size: cover;
    }
    .container {
        position: absolute;
        top: 80px;
        left: 100px;
        right: 100px;
        bottom: 80px;
        background-size: cover;
        -webkit-background-size: cover;
        -o-background-size: cover;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        box-shadow: 0 50px 50px rgba(0, 0, 0, 0.8),
            0 0 0 100px rgba(0, 0, 0, 0.3);
    }
    .container h2 {
        text-align: center;
        font-size: 10em;
        line-height: 0.7em;
        color: #ffffff;
        margin-top: -80px;
    }
    .container h2 span {
        display: block;
        font-weight: 300;
        letter-spacing: 6px;
        font-size: 0.2em;
    }
    .countdown {
        display: flex;
        margin-top: 50px;
    }
    .countdown div {
        position: relative;
        width: 100px;
        height: 100px;
        line-height: 100px;
        text-align: center;
        background: #333;
        color: #fff;
        margin: 0 15px;
        font-size: 3em;
        font-weight: 500;
        border-radius: 10px 10px 0 0;
    }
    .countdown div:before {
        content: '';
        position: absolute;
        bottom: -30px;
        left: 0;
        width: 100%;
        height: 35px;
        background: #b00000;
        color: #ffffff;
        font-size: 0.35em;
        line-height: 35px;
        font-weight: 300;
        border-radius: 0 0 10px 10px;
    }
    .countdown #day:before {
        content: '天';
    }
    .countdown #hour:before {
        content: '時';
    }
    .countdown #minute:before {
        content: '分';
    }
    .countdown #second:before {
        content: '秒';
    }
}
canvas {
    width: 100%;
    height: 100%;
}
::-webkit-scrollbar {
    display: none;
}
#btn{
  margin: 40px;
  width: 100px;
  height: 30px;
  background: pink;
  text-align: center;
  color: darkred;
  line-height: 30px;
}

[圖片上傳失敗...(image-2a3511-1682041429064)]

js部分

class Snowflake {
  constructor() {
    this.x = 0;
    this.y = 0;
    this.vx = 0;
    this.vy = 0;
    this.radius = 0;
    this.alpha = 0;
    this.reset();
  }
  reset() {
    this.x = this.randBetween(0, window.innerWidth);
    this.y = this.randBetween(0, -window.innerHeight);
    this.vx = this.randBetween(-3, 3);
    this.vy = this.randBetween(2, 5);
    this.radius = this.randBetween(1, 4);
    this.alpha = this.randBetween(0.1, 0.9);
  }
  randBetween(min, max) {
    return min + Math.random() * (max - min);
  }
  update() {
    this.x += this.vx;
    this.y += this.vy;
    if (this.y + this.radius > window.innerHeight) {
      this.reset();
    }
  }
}
class Snow {
  constructor() {
    this.canvas = document.createElement('canvas');
    this.ctx = this.canvas.getContext('2d');
    document.body.appendChild(this.canvas);
    window.addEventListener('resize', () => this.onResize());
    this.onResize();
    this.updateBound = this.update.bind(this);
    requestAnimationFrame(this.updateBound);
    this.createSnowflakes();
  }
  onResize() {
    this.width = window.innerWidth;
    this.height = window.innerHeight;
    this.canvas.width = this.width;
    this.canvas.height = this.height;
  }
  createSnowflakes() {
    const flakes = window.innerWidth / 4;
    this.snowflakes = [];
    for (let s = 0; s < flakes; s++) {
      this.snowflakes.push(new Snowflake());
    }
  }
  update() {
    this.ctx.clearRect(0, 0, this.width, this.height);
    for (let flake of this.snowflakes) {
      flake.update();
      this.ctx.save();
      this.ctx.fillStyle = '#FFF';
      this.ctx.beginPath();
      this.ctx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
      this.ctx.closePath();
      this.ctx.globalAlpha = flake.alpha;
      this.ctx.fill();
      this.ctx.restore();
    }
    requestAnimationFrame(this.updateBound);
  }
}
new Snow();
var stop = false;
function show_runtime() {
  var newDay = '2022/2/1 00:00:00';
  var countDate = new Date(newDay);
  var now = new Date().getTime();
  gap = countDate - now;
  var second = 1000;
  var minute = second * 60;
  var hour = minute * 60;
  var day = hour * 24;
  var d = Math.floor(gap / day);
  var h = Math.floor((gap % day) / hour);
  var m = Math.floor((gap % hour) / minute);
  var s = Math.floor((gap % minute) / second);
  if ((d, h, m, s < 0)) {
    stop = true;
  } else {
    document.getElementById('day').innerText = d;
    document.getElementById('hour').innerText = h;
    document.getElementById('minute').innerText = m;
    document.getElementById('second').innerText = s;
  }
}
function newyear() {
  document.getElementById('title').innerText = 'Happy Spring Festival';
  document.getElementById('day').innerText = '春';
  document.getElementById('hour').innerText = '節(jié)';
  document.getElementById('minute').innerText = '快';
  document.getElementById('second').innerText = '樂';
}
var time = setInterval(() => {
  show_runtime();
  if (stop === true) {
    newyear();
    clearInterval(time);
  }
}, 1000);
// 定時器 控制圖片自動切換
function downTime() {
  let item = 1;
  setInterval(() => {
    item++;
    if (item === 4) {
      item = 1;
    }
    console.log(item, 'item');
    document.body.style.backgroundImage = `url(./image/geyao${item}.jpg)`;
    return item;
    e.stopPropagation(); //取消事件冒泡
  }, 2000);
}
window.onload = downTime;

[圖片上傳失敗...(image-66f0f6-1682041429064)]

效果演示

移動端

[圖片上傳失敗...(image-3ae82-1682041429064)]

[圖片上傳失敗...(image-6b8a24-1682041429064)]

[圖片上傳失敗...(image-c3ca95-1682041429064)]

pc端

[圖片上傳失敗...(image-fdb504-1682041429064)]

[圖片上傳失敗...(image-8fbaf7-1682041429064)]

[圖片上傳失敗...(image-c85977-1682041429064)]

總結

在過往的歲月中,我遇到了形形色色的人和事情。有的人堅持烟逊,有的人放棄渣窜。 有的人逆襲,有的人失敗宪躯。最好的種樹是十年前乔宿,其次是現(xiàn)在。很高興遇到你, 愿你的人生多姿多彩访雪,幸福綿綿详瑞,好事連連 喜歡請一鍵三連 謝謝~

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市臣缀,隨后出現(xiàn)的幾起案子坝橡,更是在濱河造成了極大的恐慌,老刑警劉巖精置,帶你破解...
    沈念sama閱讀 222,729評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件计寇,死亡現(xiàn)場離奇詭異,居然都是意外死亡脂倦,警方通過查閱死者的電腦和手機番宁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,226評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來赖阻,“玉大人贝淤,你說我怎么就攤上這事≌” “怎么了播聪?”我有些...
    開封第一講書人閱讀 169,461評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長布隔。 經(jīng)常有香客問我离陶,道長,這世上最難降的妖魔是什么衅檀? 我笑而不...
    開封第一講書人閱讀 60,135評論 1 300
  • 正文 為了忘掉前任招刨,我火速辦了婚禮,結果婚禮上哀军,老公的妹妹穿的比我還像新娘沉眶。我一直安慰自己,他們只是感情好杉适,可當我...
    茶點故事閱讀 69,130評論 6 398
  • 文/花漫 我一把揭開白布谎倔。 她就那樣靜靜地躺著,像睡著了一般猿推。 火紅的嫁衣襯著肌膚如雪片习。 梳的紋絲不亂的頭發(fā)上捌肴,一...
    開封第一講書人閱讀 52,736評論 1 312
  • 那天,我揣著相機與錄音藕咏,去河邊找鬼状知。 笑死,一個胖子當著我的面吹牛孽查,可吹牛的內容都是我干的饥悴。 我是一名探鬼主播,決...
    沈念sama閱讀 41,179評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼盲再,長吁一口氣:“原來是場噩夢啊……” “哼铺坞!你這毒婦竟也來了?” 一聲冷哼從身側響起洲胖,我...
    開封第一講書人閱讀 40,124評論 0 277
  • 序言:老撾萬榮一對情侶失蹤济榨,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后绿映,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體擒滑,經(jīng)...
    沈念sama閱讀 46,657評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,723評論 3 342
  • 正文 我和宋清朗相戀三年叉弦,在試婚紗的時候發(fā)現(xiàn)自己被綠了丐一。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,872評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡淹冰,死狀恐怖库车,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情樱拴,我是刑警寧澤柠衍,帶...
    沈念sama閱讀 36,533評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站晶乔,受9級特大地震影響珍坊,放射性物質發(fā)生泄漏。R本人自食惡果不足惜正罢,卻給世界環(huán)境...
    茶點故事閱讀 42,213評論 3 336
  • 文/蒙蒙 一阵漏、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧翻具,春花似錦履怯、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,700評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至晾虑,卻和暖如春疹味,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背帜篇。 一陣腳步聲響...
    開封第一講書人閱讀 33,819評論 1 274
  • 我被黑心中介騙來泰國打工糙捺, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人笙隙。 一個月前我還...
    沈念sama閱讀 49,304評論 3 379
  • 正文 我出身青樓洪灯,卻偏偏與公主長得像,于是被迫代替她去往敵國和親竟痰。 傳聞我的和親對象是個殘疾皇子签钩,可洞房花燭夜當晚...
    茶點故事閱讀 45,876評論 2 361

推薦閱讀更多精彩內容