【前端demo】圣诞节灯泡 CSS动画实现轮流闪灯

news2024/9/21 0:28:39

文章目录

    • 效果
    • 过程
      • 灯泡闪亮实现(animation和box-shadow)
      • 控制灯泡闪亮时间和顺序(animation-delay)
      • 按钮开关
    • 代码
      • html
      • css
      • js
    • 参考
      • 代码1
      • 代码2

前端demo目录

效果

在这里插入图片描述
效果预览:https://codepen.io/karshey/pen/zYyBRWZ

参考:
Pure CSS Christmas Lights (codepen.io)

Christmas Lights (codepen.io)

过程

灯泡闪亮实现(animation和box-shadow)

效果:

在这里插入图片描述

以单个灯泡闪亮为例。灯泡闪亮的实现原理是CSS动画animation和阴影box-shadow

亮的时候其实就是显示与背景色相同的阴影:

box-shadow: h-shadow v-shadow blur spread color inset;
水平阴影的位置|垂直阴影的位置|模糊距离|阴影的尺寸|阴影的颜色|将外部阴影 (outset) 改为内部阴影

这里只需要用到模糊距离|阴影的尺寸|阴影的颜色

只需要在动画开头和结尾有阴影,中间的时候阴影消失,就会有灯泡闪亮与熄灭的效果

@keyframes glow-red {

    /* 开头和结尾都亮 */
    0%,
    100% {
        /* 放大阴影相当于亮了 */
        box-shadow: 0 0 20px 5px #fbc2eb;
    }

    50% {
        box-shadow: none;
    }
}

这里我们命名粉色灯泡的动画效果为glow-red,我们就要在animation中使用到它。

animation的子属性:

  • animation-name: 指定一个 @keyframes 的名称,动画将要使用这个@keyframes定义。
  • animation-duration: 整个动画需要的时长。
  • animation-timing-function: 动画进行中的时速控制,比如 easelinear.
  • animation-delay: 动画延迟时间。
  • animation-direction: 动画重复执行时运动的方向。
  • animation-iteration-count: 动画循环执行的次数。
  • animation-fill-mode: 设置动画执行完成后/开始执行前的状态,比如,你可以让动画执行完成后停留在最后一幕,或恢复到初始状态。
  • animation-play-state: 暂停/启动动画。

在这里,我们只需要令动画时间为1s(animation-duration),执行无限次(animation-iteration-count)即可。

animation: glow-red 1s infinite;

综上,单个粉色灯泡相关代码:

.red {
    background-image: linear-gradient(to top, #fbc2eb 0%, #a6c1ee 100%);
    animation: glow-red 1s infinite;
}

@keyframes glow-red {

    /* 开头和结尾都亮 */
    0%,
    100% {
        /* 放大阴影相当于亮了 */
        box-shadow: 0 0 20px 5px #fbc2eb;
    }

    50% {
        box-shadow: none;
    }
}

控制灯泡闪亮时间和顺序(animation-delay)

此时灯泡是同时闪亮的。

在这里插入图片描述
我们希望灯泡闪亮是按顺序的:粉色先,蓝色其次,最后绿色。

实现方法:为灯泡设置动画开始的时间,即延迟时间animation-delay

如,令粉色没有延迟时间,蓝色延迟0.3s,绿色延迟0.6s。

.red {
    background-image: linear-gradient(to top, #fbc2eb 0%, #a6c1ee 100%);
    animation: glow-red 1s infinite;
}

.blue {
    background-image: linear-gradient(120deg, #89f7fe 0%, #66a6ff 100%);
    animation: glow-blue 1s infinite;
    animation-delay: 0.3s;
}

.green {
    background-image: linear-gradient(120deg, #d4fc79 0%, #96e6a1 100%);
    animation: glow-green 1s infinite;
    animation-delay: 0.6s;
}

效果:

在这里插入图片描述

按钮开关

想要有按钮ON和OFF,点击ON就开灯,点击OFF就关灯。

添加按钮和对应点击事件:

OFF按钮的事件:获取节点,并设置关灯。

const item = document.getElementsByClassName('item')
const len = item.length

function offLight() {
    for (let i = 0; i < len; i++) {
        item[i].style.animation = 'none'
        item[i].style.background = '#9e9e9e'
    }
}

ON按钮的事件:移除原先的syle并令animationPlayState 开始。

这里不写item[i].style.animationPlayState = 'running’也可以。

function onLight() {
    for (let i = 0; i < len; i++) {
        item[i].removeAttribute('style')
        //可以不写
        item[i].style.animationPlayState = 'running'
    }
}

点击OFF时:相当于添加了停止动画和灰色背景色的样式,覆盖了原先的样式。

在这里插入图片描述

因此,点击ON时:需要把OFF添加的style去除掉。这里不添加item[i].style.animationPlayState = 'running'也可以亮灯,因为没有style后就显示原先CSS的亮灯效果了。

在这里插入图片描述

代码

html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>圣诞节灯泡</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <!-- 九个灯泡 -->
    <div class="main">
        <div class="items">
            <div class="red item" id="red"></div>
            <div class="blue item" id="blue"></div>
            <div class="green item" id="green"></div>
            <div class="red item" id="red"></div>
            <div class="blue item" id="blue"></div>
            <div class="green item" id="green"></div>
            <div class="red item" id="red"></div>
            <div class="blue item" id="blue"></div>
            <div class="green item" id="green"></div>
        </div>
        <div class="title">
            Christmas Lights
        </div>
        <div class="btn">
            <div onclick="onLight()">ON</div>
            <div onclick="offLight()">OFF</div>
        </div>
    </div>


</body>

</html>

<script src="index.js"></script>

css

body {
    margin: 0;
    padding: 0;
    background-image: linear-gradient(to right, #6a11cb 0%, #2575fc 100%);
}

.red {
    background-image: linear-gradient(to top, #fbc2eb 0%, #a6c1ee 100%);
    animation: glow-red 1s infinite;
}

.blue {
    background-image: linear-gradient(120deg, #89f7fe 0%, #66a6ff 100%);
    animation: glow-blue 1s infinite;
    animation-delay: 0.3s;
}

.green {
    background-image: linear-gradient(120deg, #d4fc79 0%, #96e6a1 100%);
    animation: glow-green 1s infinite;
    animation-delay: 0.6s;
}

.main {
    display: flex;
    flex-direction: column;
    margin-top: 100px;
    justify-content: center;
    align-items: center;
}

.items {
    display: flex;
}

.title {
    font-size: 38px;
    font-weight: 700;
    color: #fff;
    text-shadow: 0 0 30px #fff;
}

.btn {
    margin-top: 20px;
    display: flex;
}

.btn div {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    line-height: 50px;
    text-align: center;
    background-color: #9e9e9e;
    color: #fff;
    margin: 10px;
    cursor: pointer;
}

.item {
    height: 50px;
    width: 50px;
    border-radius: 50%;
    display: inline-block;
    margin: 20px;
}

@keyframes glow-red {

    /* 开头和结尾都亮 */
    0%,
    100% {
        /* 放大阴影相当于亮了 */
        box-shadow: 0 0 20px 5px #fbc2eb;
    }

    50% {
        box-shadow: none;
    }
}

@keyframes glow-blue {

    /* 开头和结尾都亮 */
    0%,
    100% {
        /* 放大阴影相当于亮了 */
        box-shadow: 0 0 20px 5px #89f7fe;
    }

    50% {
        box-shadow: none;
    }
}

@keyframes glow-green {

    /* 开头和结尾都亮 */
    0%,
    100% {
        /* 放大阴影相当于亮了 */
        box-shadow: 0 0 20px 5px #96e6a1;
    }

    50% {
        box-shadow: none;
    }
}

js

const item = document.getElementsByClassName('item')
const len = item.length


function onLight() {
    for (let i = 0; i < len; i++) {
        item[i].removeAttribute('style')
        // item[i].style.animationPlayState = 'running'
    }
}

function offLight() {
    for (let i = 0; i < len; i++) {
        item[i].style.animation = 'none'
        item[i].style.background = '#9e9e9e'
    }
}

参考

Pure CSS Christmas Lights (codepen.io)

Christmas Lights (codepen.io)

CSS动画技术中animation的使用介绍 – WEB骇客 (webhek.com)

在线SCSS转CSS工具 - UU在线工具 (uutool.cn)

免费的渐变背景CSS3样式 | oulu.me

代码1

Pure CSS Christmas Lights (codepen.io)

html:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<ul class="lightrope">
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
</ul>

</html>

css:将链接中的scss转为了css。

body {
    background: #000;
}

.lightrope {
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    position: absolute;
    z-index: 1;
    margin: -15px 0 0 0;
    padding: 0;
    pointer-events: none;
    width: 100%;
}

.lightrope li {
    position: relative;
    animation-fill-mode: both;
    animation-iteration-count: infinite;
    list-style: none;
    margin: 0;
    padding: 0;
    display: block;
    width: 12px;
    height: 28px;
    border-radius: 50%;
    margin: 20px;
    display: inline-block;
    background: #00f7a5;
    box-shadow: 0px 4.6666666667px 24px 3px #00f7a5;
    animation-name: flash-1;
    animation-duration: 2s;
}

.lightrope li:nth-child(2n+1) {
    background: aqua;
    box-shadow: 0px 4.6666666667px 24px 3px rgba(0, 255, 255, 0.5);
    animation-name: flash-2;
    animation-duration: 0.4s;
}

.lightrope li:nth-child(4n+2) {
    background: #f70094;
    box-shadow: 0px 4.6666666667px 24px 3px #f70094;
    animation-name: flash-3;
    animation-duration: 1.1s;
}

.lightrope li:nth-child(odd) {
    animation-duration: 1.8s;
}

.lightrope li:nth-child(3n+1) {
    animation-duration: 1.4s;
}

.lightrope li:before {
    content: "";
    position: absolute;
    background: #222;
    width: 10px;
    height: 9.3333333333px;
    border-radius: 3px;
    top: -4.6666666667px;
    left: 1px;
}

.lightrope li:after {
    content: "";
    top: -14px;
    left: 9px;
    position: absolute;
    width: 52px;
    height: 18.6666666667px;
    border-bottom: solid #222 2px;
    border-radius: 50%;
}

.lightrope li:last-child:after {
    content: none;
}

.lightrope li:first-child {
    margin-left: -40px;
}

@keyframes flash-1 {

    0%,
    100% {
        background: #00f7a5;
        box-shadow: 0px 4.6666666667px 24px 3px #00f7a5;
    }

    50% {
        background: rgba(0, 247, 165, 0.4);
        box-shadow: 0px 4.6666666667px 24px 3px rgba(0, 247, 165, 0.2);
    }
}

@keyframes flash-2 {

    0%,
    100% {
        background: aqua;
        box-shadow: 0px 4.6666666667px 24px 3px aqua;
    }

    50% {
        background: rgba(0, 255, 255, 0.4);
        box-shadow: 0px 4.6666666667px 24px 3px rgba(0, 255, 255, 0.2);
    }
}

@keyframes flash-3 {

    0%,
    100% {
        background: #f70094;
        box-shadow: 0px 4.6666666667px 24px 3px #f70094;
    }

    50% {
        background: rgba(247, 0, 148, 0.4);
        box-shadow: 0px 4.6666666667px 24px 3px rgba(247, 0, 148, 0.2);
    }
}

代码2

Christmas Lights (codepen.io)

链接里有代码。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/959901.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

初出茅庐的小李博客之STM32F103C8T6音乐控制器实战教程【1】

STM32F103C8T6音乐控制器实战教程[1] USB简单介绍&#xff1a; "USB"代表通用串行总线&#xff08;Universal Serial Bus&#xff09;&#xff0c;是一种用于连接计算机及其外部设备的标准接口。USB接口允许各种设备&#xff08;如打印机、存储设备、键盘、鼠标、摄…

为何电商行业都在争相使用WhatsApp引流小挂件?

WhatsApp小挂件是嵌入在网站上的聊天小部件&#xff0c;允许访问者同WhatsApp与您联系。点击后&#xff0c;它会将客户带到移动或桌面 WhatsApp应用程序&#xff0c;或者直接打开一个对话框&#xff0c;客户可以在这些地方与您发起对话。让我们看看在您的网站上拥有WhatsApp聊天…

Java实现MQTT订阅发布

一. MQTT 与 EMQX MQTT 是轻量级基于代理的发布/订阅的消息传输协议。使用发布/订阅消息模式&#xff0c;提供一对多的消息发布&#xff0c;解除应用程序耦合。底层使用 TCP/IP 提供网络连接。 EMQ X (Erlang/Enterprise/Elastic MQTT Broker) 是基于 Erlang/OTP 平台开发的开…

Vue+Element-ui实现表格本地导入

表格文件存储在前端 如图&#xff0c;表格文件template.xlsx存储在public下的static文件夹下 注意这里的路径容易报错 a链接下载文件失败的问题(未发现文件&#xff09; a.href ‘./static/template.xlsx’ 写的时候不能带public&#xff0c;直接这么写就可以 DownloadTemp…

星域的庞大规模已经让我们眩晕

有一句道格拉斯亚当斯的名言银河系漫游指南我最近想了很多。“空间很大&#xff0c;”他写道。“你不会相信它有多么巨大&#xff0c;令人难以置信。我的意思是&#xff0c;你可能认为去药店的路很长&#xff0c;但那只是去太空的小钱。” 星域不妨把这句引言放在其设计文档的封…

从天镜大模型,透视马上消费的“三重价值”

AI正在打开新世界。 红杉资本曾发表名为《生成式AI&#xff1a;一个创造性的新世界》的文章&#xff0c;提到生成式AI将涉及数十亿的人工劳动力&#xff0c;并促使这些人工劳动力的效率和创造力至少提高10%&#xff0c;有潜力产生数万亿美元的经济价值。 大模型&#xff0c;被…

2 | Window 搭建单机 Hadoop 和Spark

搭建单机 Hadoop 和 Spark 环境可以学习和测试大数据处理的基础知识。在 Windows 操作系统上搭建这两个工具需要一些配置和设置,下面是一个详细的教程: 注意: 在开始之前,请确保你已经安装了 Java 开发工具包(JDK),并且已经下载了 Hadoop 和 Spark 的最新版本。你可以从…

程序员:你如何写可重复执行的SQL语句?

上图的意思&#xff1a; 百战百胜&#xff0c;屡试不爽。 故事 程序员小张&#xff1a; 刚毕业&#xff0c;参加工作1年左右&#xff0c;日常工作是CRUD 架构师老李&#xff1a; 多个大型项目经验&#xff0c;精通各种开发架构屠龙宝术&#xff1b; 小张注意到&#xff0c;在…

【Datawhale】AI夏令营第三期——基于论文摘要的文本分类笔记(下)

笔记上部分请看【Datawhale】AI夏令营第三期——基于论文摘要的文本分类笔记(上) 文章目录 一、深度学习Topline1.1 数据预处理1.2 模型训练1.3 评估模型1.4 测试集推理1.5 后续改进 二、大模型Topline2.1 大模型介绍2.2 大模型是什么&#xff1f;2.3 大模型的原理2.4 大模型可…

嵌入式部署机器学习模型---TinyML

我们目前生活在一个被机器学习模型包围的世界。在一天中&#xff0c;您使用这些模型的次数比您意识到的要多。诸如浏览社交媒体、拍照、查看天气等日常任务都依赖于机器学习模型。您甚至可能会看到此博客&#xff0c;因为机器学习模型向您推荐了此博客。 我们都知道训练这些模型…

【位运算】leetcode371:两整数之和

一.题目描述 两整数之和 二.思路分析 题目要求我们实现两整数相加&#xff0c;但是不能使用加号&#xff0c;应该立马想到是用位运算来解决问题。之前说过&#xff0c;异或就是“无进位相加”&#xff0c;故本题可以先将两数异或&#xff0c;然后想办法让得到的结果进位即可。…

yolov5自定义模型训练三

经过11个小时cpu训练完如下 在runs/train/expx里存放训练的结果&#xff0c; 测试是否可以检测ok 网上找的这张识别效果不是很好&#xff0c;通过加大训练次数和数据集的话精度可以提升。 训练后的权重也可以用视频源来识别&#xff0c; python detect.py --source 0 # webca…

WOFOST模型与PCSE模型技术应用

实现作物产量的准确估算对于农田生态系统响应全球变化、可持续发展、科学粮食政策制定、粮食安全维护都至关重要。传统的经验模型、光能利用率模型等估产模型原理简单&#xff0c;数据容易获取&#xff0c;但是作物生长发育非常复杂&#xff0c;中间涉及众多生理生化过程&#…

Ansible-playbook条件语句when的使用

目录 when关键字1.基本使用2.比较运算符3.逻辑运算符4.判断变量 when关键字 1.基本使用 当ansible_os_family是redhat的时候&#xff0c;执行安装vim&#xff0c;不是的话跳过 --- - hosts: webtasks:- name: Install VIM via yumyum:name: vim-enhancedstate: installedwhe…

mac安装brew

mac安装brew 安装brew 安装brew 第一步&#xff1a;执行. /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"第二步&#xff1a;输入开机密码 第三步&#xff1a;回车继续。等待安装完成 第四步&#xff1a;根…

最新盘点!上海值得加入的互联网公司有哪些?(文末附招聘岗位)

暑假结束了&#xff0c;除了迎来了青春热烈的开学季以外&#xff0c;也带来了打工人备受期待的金九银十秋招季。 我们在找工作时&#xff0c;每个人都期待能遇到一个“神仙公司”&#xff0c;譬如丰厚的薪水、优越的晋升通道、融洽的同事关系、良好的work-life balance以及自由…

KubeSphere 社区双周报 | KubeKey 新增网络插件 Hybridnet | 2023.08.18-08.31

KubeSphere 社区双周报主要整理展示新增的贡献者名单和证书、新增的讲师证书以及两周内提交过 commit 的贡献者&#xff0c;并对近期重要的 PR 进行解析&#xff0c;同时还包含了线上/线下活动和布道推广等一系列社区动态。 本次双周报涵盖时间为&#xff1a;2023.08.18-2023.…

Stable Diffusion中的ControlNet插件

文章目录 ControlNet的介绍及安装ControlNet的介绍ControlNet的安装 ControlNet的功能介绍ControlNet的应用与演示 ControlNet的介绍及安装 ControlNet的介绍 ControlNet 的中文就是控制网&#xff0c;本质上是Stable Diffusion的一个扩展插件&#xff0c;在2023年2月份由斯坦…

红米手机使用google play

开启&#xff1a; 1.在 Google Play 支持的设备列表内的小米/红米手机已预装谷歌服务&#xff0c;我们只需要安装Play 商店。 1.开启谷歌服务: 设置 -> 帐号与同步 > 谷歌基础服务 2.安装 Play 商店: 在应用商店搜索 [google play] &#xff0c;安装[Google Play 商店] …

NPM 常用命令(一)

目录 1、npm 1.1 简介 1.2 依赖性 1.3 安装方式 2、npm access 2.1 命令描述 2.2 详情 3、npm adduser 3.1 描述 4、npm audit 4.1 简介 4.2 审计签名 4.3 操作示例 4.4 配置 audit-level dry-run force json package-lock-only omit foreground-scripts …