0


Html+Css+js实现春节倒计时效果(移动端和PC端)

前言

我是歌谣 我有个兄弟 巅峰的时候排名c站总榜19 叫前端小歌谣 曾经我花了三年的时间创作了他 现在我要用五年的时间超越他 今天又是接近兄弟的一天人生难免坎坷 大不了从头再来 歌谣的意志是永恒的 放弃很容易 但是坚持一定很酷

导语

歌谣 歌谣 新的一年就要到了 可否写一个春节的倒计时呢 我想看看哪天可以过春节

效果预览

html部分

  1. <!DOCTYPE html>
  2. <!--geyao-->
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <link rel="stylesheet" href="css/style.css">
  8. <link rel="stylesheet" href="css/mobile.css">
  9. <title>春节倒计时</title>
  10. </head>
  11. <body>
  12. <div class="container">
  13. <h2><span id="title">春节倒计时</span>2022</h2>
  14. <div class="countdown">
  15. <div id="day">--</div>
  16. <div id="hour">--</div>
  17. <div id="minute">--</div>
  18. <div id="second">--</div>
  19. </div>
  20. <!-- 手动切换不好看 直接加定时器切换 微信公众号关注前端小歌谣
  21. -->
  22. <!-- <div id="btn">切换背景</div> -->
  23. </div>
  24. <script src="js/script.js"></script>
  25. </body>
  26. </html>

移动端样式(mobile.css)

  1. @media screen and (max-width: 1025px) {
  2. * {
  3. margin: 0;
  4. padding: 0;
  5. }
  6. body {
  7. background: rgb(129, 155, 190) url(../image/geyao1.jpg);
  8. background-size: cover;
  9. background-position: center center;
  10. height: 100%;
  11. }
  12. .container {
  13. margin: 0;
  14. color: #fff;
  15. line-height: normal;
  16. position: absolute;
  17. align-items: center;
  18. left: 5%;
  19. right: 5%;
  20. }
  21. .container h2 {
  22. font-size: 6em;
  23. text-align: center;
  24. margin: 10% 0;
  25. color: #fff;
  26. }
  27. .container h2 span {
  28. color: #fff;
  29. display: block;
  30. text-align: center;
  31. font-size: 0.3em;
  32. font-weight: 300;
  33. letter-spacing: 2px;
  34. }
  35. .countdown {
  36. display: flex;
  37. justify-content: space-around;
  38. margin: 0;
  39. }
  40. .countdown div {
  41. width: 20%;
  42. height: 13vw;
  43. margin: 0 10px;
  44. line-height: 13vw;
  45. font-size: 2em;
  46. position: relative;
  47. text-align: center;
  48. background: #333333;
  49. color: #ffffff;
  50. font-weight: 500;
  51. border-radius: 10px 10px 0 0;
  52. }
  53. .countdown div:before {
  54. content: '';
  55. position: absolute;
  56. bottom: -30px;
  57. left: 0;
  58. width: 100%;
  59. height: 30px;
  60. background: #b00000;
  61. color: #ffffff;
  62. font-size: 0.4em;
  63. line-height: 35px;
  64. font-weight: 300;
  65. border-radius: 0 0 10px 10px;
  66. }
  67. .countdown #day:before {
  68. content: '天';
  69. }
  70. .countdown #hour:before {
  71. content: '时';
  72. }
  73. .countdown #minute:before {
  74. content: '分';
  75. }
  76. .countdown #second:before {
  77. content: '秒';
  78. }
  79. }

pc端样式(style.css)

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. font-family: 'Poppins', sans-serif;
  5. }
  6. @media screen and (min-width: 1025px) {
  7. body {
  8. background: rgb(129, 155, 190) url(../image/geyao1.jpg);
  9. background-attachment: fixed;
  10. background-size: cover;
  11. -webkit-background-size: cover;
  12. -o-background-size: cover;
  13. }
  14. .container {
  15. position: absolute;
  16. top: 80px;
  17. left: 100px;
  18. right: 100px;
  19. bottom: 80px;
  20. background-size: cover;
  21. -webkit-background-size: cover;
  22. -o-background-size: cover;
  23. display: flex;
  24. justify-content: center;
  25. align-items: center;
  26. flex-direction: column;
  27. box-shadow: 0 50px 50px rgba(0, 0, 0, 0.8),
  28. 0 0 0 100px rgba(0, 0, 0, 0.3);
  29. }
  30. .container h2 {
  31. text-align: center;
  32. font-size: 10em;
  33. line-height: 0.7em;
  34. color: #ffffff;
  35. margin-top: -80px;
  36. }
  37. .container h2 span {
  38. display: block;
  39. font-weight: 300;
  40. letter-spacing: 6px;
  41. font-size: 0.2em;
  42. }
  43. .countdown {
  44. display: flex;
  45. margin-top: 50px;
  46. }
  47. .countdown div {
  48. position: relative;
  49. width: 100px;
  50. height: 100px;
  51. line-height: 100px;
  52. text-align: center;
  53. background: #333;
  54. color: #fff;
  55. margin: 0 15px;
  56. font-size: 3em;
  57. font-weight: 500;
  58. border-radius: 10px 10px 0 0;
  59. }
  60. .countdown div:before {
  61. content: '';
  62. position: absolute;
  63. bottom: -30px;
  64. left: 0;
  65. width: 100%;
  66. height: 35px;
  67. background: #b00000;
  68. color: #ffffff;
  69. font-size: 0.35em;
  70. line-height: 35px;
  71. font-weight: 300;
  72. border-radius: 0 0 10px 10px;
  73. }
  74. .countdown #day:before {
  75. content: '天';
  76. }
  77. .countdown #hour:before {
  78. content: '时';
  79. }
  80. .countdown #minute:before {
  81. content: '分';
  82. }
  83. .countdown #second:before {
  84. content: '秒';
  85. }
  86. }
  87. canvas {
  88. width: 100%;
  89. height: 100%;
  90. }
  91. ::-webkit-scrollbar {
  92. display: none;
  93. }
  94. #btn{
  95. margin: 40px;
  96. width: 100px;
  97. height: 30px;
  98. background: pink;
  99. text-align: center;
  100. color: darkred;
  101. line-height: 30px;
  102. }

js部分

  1. class Snowflake {
  2. constructor() {
  3. this.x = 0;
  4. this.y = 0;
  5. this.vx = 0;
  6. this.vy = 0;
  7. this.radius = 0;
  8. this.alpha = 0;
  9. this.reset();
  10. }
  11. reset() {
  12. this.x = this.randBetween(0, window.innerWidth);
  13. this.y = this.randBetween(0, -window.innerHeight);
  14. this.vx = this.randBetween(-3, 3);
  15. this.vy = this.randBetween(2, 5);
  16. this.radius = this.randBetween(1, 4);
  17. this.alpha = this.randBetween(0.1, 0.9);
  18. }
  19. randBetween(min, max) {
  20. return min + Math.random() * (max - min);
  21. }
  22. update() {
  23. this.x += this.vx;
  24. this.y += this.vy;
  25. if (this.y + this.radius > window.innerHeight) {
  26. this.reset();
  27. }
  28. }
  29. }
  30. class Snow {
  31. constructor() {
  32. this.canvas = document.createElement('canvas');
  33. this.ctx = this.canvas.getContext('2d');
  34. document.body.appendChild(this.canvas);
  35. window.addEventListener('resize', () => this.onResize());
  36. this.onResize();
  37. this.updateBound = this.update.bind(this);
  38. requestAnimationFrame(this.updateBound);
  39. this.createSnowflakes();
  40. }
  41. onResize() {
  42. this.width = window.innerWidth;
  43. this.height = window.innerHeight;
  44. this.canvas.width = this.width;
  45. this.canvas.height = this.height;
  46. }
  47. createSnowflakes() {
  48. const flakes = window.innerWidth / 4;
  49. this.snowflakes = [];
  50. for (let s = 0; s < flakes; s++) {
  51. this.snowflakes.push(new Snowflake());
  52. }
  53. }
  54. update() {
  55. this.ctx.clearRect(0, 0, this.width, this.height);
  56. for (let flake of this.snowflakes) {
  57. flake.update();
  58. this.ctx.save();
  59. this.ctx.fillStyle = '#FFF';
  60. this.ctx.beginPath();
  61. this.ctx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
  62. this.ctx.closePath();
  63. this.ctx.globalAlpha = flake.alpha;
  64. this.ctx.fill();
  65. this.ctx.restore();
  66. }
  67. requestAnimationFrame(this.updateBound);
  68. }
  69. }
  70. new Snow();
  71. var stop = false;
  72. function show_runtime() {
  73. var newDay = '2022/2/1 00:00:00';
  74. var countDate = new Date(newDay);
  75. var now = new Date().getTime();
  76. gap = countDate - now;
  77. var second = 1000;
  78. var minute = second * 60;
  79. var hour = minute * 60;
  80. var day = hour * 24;
  81. var d = Math.floor(gap / day);
  82. var h = Math.floor((gap % day) / hour);
  83. var m = Math.floor((gap % hour) / minute);
  84. var s = Math.floor((gap % minute) / second);
  85. if ((d, h, m, s < 0)) {
  86. stop = true;
  87. } else {
  88. document.getElementById('day').innerText = d;
  89. document.getElementById('hour').innerText = h;
  90. document.getElementById('minute').innerText = m;
  91. document.getElementById('second').innerText = s;
  92. }
  93. }
  94. function newyear() {
  95. document.getElementById('title').innerText = 'Happy Spring Festival';
  96. document.getElementById('day').innerText = '春';
  97. document.getElementById('hour').innerText = '节';
  98. document.getElementById('minute').innerText = '快';
  99. document.getElementById('second').innerText = '乐';
  100. }
  101. var time = setInterval(() => {
  102. show_runtime();
  103. if (stop === true) {
  104. newyear();
  105. clearInterval(time);
  106. }
  107. }, 1000);
  108. // 定时器 控制图片自动切换
  109. function downTime() {
  110. let item = 1;
  111. setInterval(() => {
  112. item++;
  113. if (item === 4) {
  114. item = 1;
  115. }
  116. console.log(item, 'item');
  117. document.body.style.backgroundImage = `url(./image/geyao${item}.jpg)`;
  118. return item;
  119. e.stopPropagation(); //取消事件冒泡
  120. }, 2000);
  121. }
  122. window.onload = downTime;

效果演示

移动端

pc端

总结

在过往的岁月中,我遇到了形形色色的人和事情。有的人坚持,有的人放弃。 有的人逆袭,有的人失败。最好的种树是十年前,其次是现在。很高兴遇到你, 愿你的人生多姿多彩,幸福绵绵,好事连连 喜欢请一键三连 谢谢~

标签: html css javascript

本文转载自: https://blog.csdn.net/geyaoisnice/article/details/122460313
版权归原作者 前端大歌谣 所有, 如有侵权,请联系我们删除。

“Html+Css+js实现春节倒计时效果(移动端和PC端)”的评论:

还没有评论