js+css3简易实现2023新年快乐全屏满天星动画特效

目录

⭐ 前言

一、效果图

二、代码实现 

2.1 html

2.2 CSS

2.3. JS

⭐ 预览


⭐ 前言

js+css3全屏星星闪烁背景2023新年快乐动画特效,文字还有3D立体效果。其中,利用Math.random()方法,实现满天星的效果,着实让人眼前一亮。对于某些站点来说,这个方法非常实用,因为可以利用它来随机显示一些名人名言和新闻事件。

好了,话不多说,先看效果图,如下:

一、效果图

二、代码实现 

2.1 html

* index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>js+css3全屏2023新年快乐动画特效</title>
    <link rel="stylesheet" href="css/style.css" />
  </head>
  <body>
    <section class="section">
      <h2 class="section__title">Happy New Year<br /><span>2023</span></h2>
    </section>
    <script src="js/script.js"></script>
  </body>
</html>

2.2 CSS

* style.css

* {
  /* 采用怪异模式下的盒模型:元素的总高度和宽度包含内边距和边框(padding与border)  */
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  /* 没有滚动条 */
  overflow: hidden;
}

.section {
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  min-height: 100vh;
  background: linear-gradient(135deg, #111, #222, #111);
}
.section::before {
  content: "";
  position: absolute;
  width: 30vw;
  height: 30vw;
  /* 红色边框 */
  border: 5vw solid #ff1062;
  /* 圆形边框 */
  border-radius: 50%;
  /* 为边框添加2个下拉阴影 */
  box-shadow: 0 0 0 1vw #222, 0 0 0 1.3vw #fff;
}
.section .section__title {
  position: absolute;
  transform: skewY(-7deg);
  z-index: 10;
  color: #fff;
  text-align: center;
  font-size: 9vw;
  line-height: 2em;
  text-shadow: 1px 1px 0 #ccc, 2px 2px 0 #ccc, 3px 3px 0 #ccc, 4px 4px 0 #ccc,
    5px 5px 0 #ccc, 10px 10px 0 rgba(0, 0, 0, 0.2);
  animation: floating 5s ease-in-out infinite;
}
.section .section__title span {
  text-shadow: 1px 1px 0 #ccc, 2px 2px 0 #ccc, 3px 3px 0 #ccc, 4px 4px 0 #ccc,
    5px 5px 0 #ccc, 6px 6px 0 #ccc, 7px 7px 0 #ccc, 8px 8px 0 #ccc,
    9px 9px 0 #ccc, 20px 20px 0 rgba(0, 0, 0, 0.2);
  font-weight: 700;
  font-size: 3em;
}
.section i {
  position: absolute;
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 0 10px #fff, 0 0 20px #fff, 0 0 40px #fff, 0 0 80px #fff;
  animation: animate linear infinite;
}

@keyframes floating {
  0%,
  100% {
    transform: skewY(-7deg) translate(0, -20px);
  }
  50% {
    transform: skewY(-7deg) translate(0, 20px);
  }
}
/* 利用透明度设置星星明暗变化的动画效果 */
@keyframes animate {
  0% {
    opacity: 0;
  }
  10% {
    opacity: 1;
  }
  90% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}

2.3. JS

* script.js

const stars = () => {
  const count = 200;
  const section = document.querySelector('.section');
  let i = 0;
  while (i < count) {
    // 在内存中创建一个新的空元素对象,如i或是div
    const star = document.createElement('i');
    // 定义变量x和y :通过Math.random()方法随机的使星星出现在不同位置,当然星星的定位要在文档显示区内
    const x = Math.floor(Math.random() * window.innerWidth);
    const y = Math.floor(Math.random() * window.innerHeight);
    const size = Math.random() * 4;
    // 让星星始终会在网页最左最顶端出现,通过想x和y的定位,我们要让它出现在页面各个不同的位置
    star.style.left = x + 'px';
    star.style.top = y + 'px';
    // 利用Math.random()这个方法来随机取星星的大小:为每颗星星设置随机的宽高范围为[0,5)
    star.style.width = 1 + size + 'px';
    star.style.height = 1 + size + 'px';
    
    const duration = Math.random() * 2;
    
    // 设置持续时间
    // js中除了减法计算之外,不允许随便写-。因为会混淆。所以,DOM标准规定,所有带-的css属性名,一律去横线变驼峰
    // css属性animation-duration,在js中改写为驼峰形式:animationDuration
    star.style.animationDuration = 2 + duration + 's';
    // 设置延迟 
    star.style.animationDelay = 2 + duration + 's';
    // 将新元素添加到DOM树:把新创建的节点追加到父元素下所有直接子元素的结尾
    section.appendChild(star);
    i++;
  }
}
// 调用函数
stars();

🔥 关于Math.radom()方法

这里主要利用JavaScript的内置对象Mach的一个radom()方法制作满天星。Math.random()方法返回大于等于 0 小于 1 的一个随机数。也就是说,Math.radom()可以获得一个0.0到1.0的随机double值,不包括1.0,即[0.0,1.0)。

下面我们可以简单了解一下js的内置对象Math。Math 和其他的对象不同,它不是一个构造函数,不需要创建对象。所以,我们不需要通过 new 来调用,而是直接使用里面的属性和方法即可。

Math属于一个工具类,里面封装了数学运算相关的属性和方法。如下:

⭐ 预览

Github仓库 | Demo预览 🌐 Gitee仓库 | Demo预览 

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

儒雅的烤地瓜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值