iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >利用JavaScript实现春节倒计时效果(移动端和PC端)
  • 568
分享到

利用JavaScript实现春节倒计时效果(移动端和PC端)

2024-04-02 19:04:59 568人浏览 薄情痞子
摘要

目录效果预览html部分移动端样式(mobile.CSS)pc端样式(style.css)js部分效果演示移动端pc端效果预览 html部分 <!DOCTYPE html&g

效果预览

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" rel="external nofollow" >
    <link rel="stylesheet" href="css/mobile.css" rel="external nofollow" >
    <title>春节倒计时</title>
  </head>
  <body>
    <div class="container">
      <h2><span id="title">春节倒计时</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>

移动端样式(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: '秒';
	}
}

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;
}

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 = '节';
  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;

效果演示

移动端

pc端

以上就是利用javascript实现春节倒计时效果(移动端和PC端)的详细内容,更多关于JavaScript倒计时效果的资料请关注编程网其它相关文章!

--结束END--

本文标题: 利用JavaScript实现春节倒计时效果(移动端和PC端)

本文链接: https://www.lsjlt.com/news/162495.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 利用JavaScript实现春节倒计时效果(移动端和PC端)
    目录效果预览html部分移动端样式(mobile.css)pc端样式(style.css)js部分效果演示移动端pc端效果预览 html部分 <!DOCTYPE html&g...
    99+
    2024-04-02
  • 如何利用JavaScript实现春节倒计时效果
    这篇文章给大家分享的是有关如何利用JavaScript实现春节倒计时效果的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。效果预览html部分<!DOCTYPE html><!--geya...
    99+
    2023-06-26
  • typescript+react实现移动端和PC端简单拖拽效果
    本文实例为大家分享了typescript+react实现移动端和PC端简单拖拽效果的具体代码,供大家参考,具体内容如下 一、移动端 1.tsx代码 import { Compon...
    99+
    2024-04-02
  • Html5如何实现移动端、PC端的刮刮卡效果
    这篇文章将为大家详细讲解有关 Html5如何实现移动端、PC端的刮刮卡效果,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。先给大家展示下效果图:刮刮卡需求:每一位用户有三次...
    99+
    2024-04-02
  • 怎么用JavaScript代码实现虎年春节倒计时
    这篇文章主要介绍“怎么用JavaScript代码实现虎年春节倒计时”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“怎么用JavaScript代码实现虎年春节倒计时”文章能帮助大家解决问题。虎年春节倒计...
    99+
    2023-06-29
  • JavaScript用20行代码实现虎年春节倒计时
    春节将至,小梦相信大家跟小朦梦一样很激动呀。为了迎接虎年春节到来,小梦撸了一个虎年春节倒计时,仅20行代码用js就实现啦,是不是很简单呢?我们用这20行代码不仅能做个虎年春节倒计时,...
    99+
    2024-04-02
  • 如何实现css移动端与pc端一样的acitve效果
    本篇内容主要讲解“如何实现css移动端与pc端一样的acitve效果”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何实现css移动端与pc端一样的acitve...
    99+
    2024-04-02
  • 原生javascript如何实现移动端滑动banner效果
    这篇文章给大家分享的是有关原生javascript如何实现移动端滑动banner效果的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。具体内容如下<!DOCTYPE ...
    99+
    2024-04-02
  • JavaScript利用Canvas实现粒子动画倒计时
    目录canvas 粒子动画介绍何为canvas粒子动画是啥canvas定义初始变量初始化canvas和数字文本创建一定数量的点倒计时倒计时文本绘画循环绘制点动画效果图canvas 粒...
    99+
    2022-12-09
    JavaScript Canvas粒子动画倒计时 JavaScript  粒子动画倒计时 JavaScript Canvas 倒计时 JavaScript 倒计时
  • Android中怎么利用CountDownTimer实现验证码倒计时效果
    今天就跟大家聊聊有关Android中怎么利用CountDownTimer实现验证码倒计时效果,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。1、需求分析点击按钮之后,按钮文字变为“ns...
    99+
    2023-05-30
    android countdowntimer
  • 纯CSS3如何实现移动端展开和收起效果
    这篇文章给大家分享的是有关纯CSS3如何实现移动端展开和收起效果的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。展示效果:HTML代码<section class="block"...
    99+
    2023-06-08
  • JavaScript怎么实现移动端手势滑动的幻灯片切换效果
    这篇文章主要介绍了JavaScript怎么实现移动端手势滑动的幻灯片切换效果的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇JavaScript怎么实现移动端手势滑动的幻灯片切换...
    99+
    2024-04-02
  • 使用iframe怎么实现一个移动端缩放效果
    今天就跟大家聊聊有关使用iframe怎么实现一个移动端缩放效果,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。这是我习惯用的meta头部,使页面宽度根据设备宽度自适应变化<met...
    99+
    2023-06-09
  • 使用Html5怎么实现一个移动端弹幕动画效果
    这期内容当中小编将会给大家带来有关使用Html5怎么实现一个移动端弹幕动画效果,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。思路把单个内容编辑好,计算自身宽度,确定初始位置移动的距离是屏幕宽度js动态的添...
    99+
    2023-06-09
  • 怎么用jQuery插件Turn.js实现移动端电子书翻页效果
    本篇内容主要讲解“怎么用jQuery插件Turn.js实现移动端电子书翻页效果”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用jQuery插件Turn.js实现移动端电子书翻页效果”吧!先来...
    99+
    2023-07-04
  • JS如何实现PC手机端和嵌入式滑动拼图验证码三种效果
    这篇文章将为大家详细讲解有关JS如何实现PC手机端和嵌入式滑动拼图验证码三种效果,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。PC和手机端网站滑动拼图验证码效果源码,同时...
    99+
    2024-04-02
  • 使用JavaScript 实现时间轴与动画效果的示例代码(前端组件化)
    目录代码整理JavaScript 中的 “帧”实现“帧”的方法1. setInterval2. setTimeout3. requestAnimationFrame实现 Timeli...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作