HTML&CSS:动态星空按钮

这段代码创建了一个具有动态背景和光晕效果的按钮,模拟了太空中星星闪烁的效果。按钮在鼠标悬停和按下时有不同的视觉效果,增加了页面的互动性。

演示效果

HTML&CSS

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>前端Hardy</title>
    <style>
        body {
            background-color: #212121;
            display: flex;
            align-items: center;
            justify-content: center;
            width: 100%;
            height: 100vh;
        }

        .btn {
            display: flex;
            justify-content: center;
            align-items: center;
            width: 13rem;
            overflow: hidden;
            height: 3rem;
            background-size: 300% 300%;
            cursor: pointer;
            backdrop-filter: blur(1rem);
            border-radius: 5rem;
            transition: 0.5s;
            animation: gradient_301 5s ease infinite;
            border: double 4px transparent;
            background-image: linear-gradient(#212121, #212121),
                linear-gradient(137.48deg,
                    #ffdb3b 10%,
                    #fe53bb 45%,
                    #8f51ea 67%,
                    #0044ff 87%);
            background-origin: border-box;
            background-clip: content-box, border-box;
        }

        #container-stars {
            position: absolute;
            z-index: -1;
            width: 100%;
            height: 100%;
            overflow: hidden;
            transition: 0.5s;
            backdrop-filter: blur(1rem);
            border-radius: 5rem;
        }

        strong {
            z-index: 2;
            font-family: "Avalors Personal Use";
            font-size: 12px;
            letter-spacing: 5px;
            color: #ffffff;
            text-shadow: 0 0 4px white;
        }

        #glow {
            position: absolute;
            display: flex;
            width: 12rem;
        }

        .circle {
            width: 100%;
            height: 30px;
            filter: blur(2rem);
            animation: pulse_3011 4s infinite;
            z-index: -1;
        }

        .circle:nth-of-type(1) {
            background: rgba(254, 83, 186, 0.636);
        }

        .circle:nth-of-type(2) {
            background: rgba(142, 81, 234, 0.704);
        }

        .btn:hover #container-stars {
            z-index: 1;
            background-color: #212121;
        }

        .btn:hover {
            transform: scale(1.1);
        }

        .btn:active {
            border: double 4px #fe53bb;
            background-origin: border-box;
            background-clip: content-box, border-box;
            animation: none;
        }

        .btn:active .circle {
            background: #fe53bb;
        }

        #stars {
            position: relative;
            background: transparent;
            width: 200rem;
            height: 200rem;
        }

        #stars::after {
            content: "";
            position: absolute;
            top: -10rem;
            left: -100rem;
            width: 100%;
            height: 100%;
            animation: animStarRotate 90s linear infinite;
        }

        #stars::after {
            background-image: radial-gradient(#ffffff 1px, transparent 1%);
            background-size: 50px 50px;
        }

        #stars::before {
            content: "";
            position: absolute;
            top: 0;
            left: -50%;
            width: 170%;
            height: 500%;
            animation: animStar 60s linear infinite;
        }

        #stars::before {
            background-image: radial-gradient(#ffffff 1px, transparent 1%);
            background-size: 50px 50px;
            opacity: 0.5;
        }

        @keyframes animStar {
            from {
                transform: translateY(0);
            }

            to {
                transform: translateY(-135rem);
            }
        }

        @keyframes animStarRotate {
            from {
                transform: rotate(360deg);
            }

            to {
                transform: rotate(0);
            }
        }

        @keyframes gradient_301 {
            0% {
                background-position: 0% 50%;
            }

            50% {
                background-position: 100% 50%;
            }

            100% {
                background-position: 0% 50%;
            }
        }

        @keyframes pulse_3011 {
            0% {
                transform: scale(0.75);
                box-shadow: 0 0 0 0 rgba(0, 0, 0, 0.7);
            }

            70% {
                transform: scale(1);
                box-shadow: 0 0 0 10px rgba(0, 0, 0, 0);
            }

            100% {
                transform: scale(0.75);
                box-shadow: 0 0 0 0 rgba(0, 0, 0, 0);
            }
        }
    </style>
</head>

<body>
    <button type="button" class="btn">
        <strong>前端Hardy</strong>
        <div id="container-stars">
            <div id="stars"></div>
        </div>

        <div id="glow">
            <div class="circle"></div>
            <div class="circle"></div>
        </div>
    </button>

</body>

</html>

HTML 结构

  • type="button" class="btn:定义了一个按钮元素,这个按钮被赋予了一个类名 btn,用于应用 CSS 样式。
  • container-stars:一个用于包含星星效果的容器,它有一个IDcontainer-stars。
  • stars:一个空的div元素,用于通过CSS创建星星效果。
  • glow:包含两个圆形光晕效果的容器,它有一个IDglow。
  • circle:两个类名为circle的div元素,用于创建光晕效果。

CSS样式

  • .btn:定义了按钮的基本样式,包括显示方式、宽度、高度、背景图像、边框、圆角和过渡效果。
  • #container-stars:设置了星星容器的位置、大小、溢出隐藏、背景模糊和圆角。
  • strong:设置了按钮内文本的字体、大小、字间距、颜色和文本阴影。
  • #glow:设置了光晕容器的位置和大小。
  • .circle:定义了光晕的宽度、高度、模糊效果和动画。
  • .btn:hover:定义了鼠标悬停在按钮上时的样式,包括z-index和缩放效果。
  • .btn:active:定义了按钮被按下时的样式,包括边框和背景效果。
  • #stars:设置了星星效果的容器大小和背景。
  • #stars::after和#stars::before:使用伪元素创建星星效果,并通过动画使星星移动。
  • @keyframes:定义了多个关键帧动画,包括星星移动、星星旋转和按钮背景渐变效果。
相关推荐
qq_274475151 小时前
第 28 篇:Promise 全解析:状态、then/catch/finally、静态方法 all/race/allSettled/any
开发语言·javascript
GuWenyue3 小时前
昵称限 20 字,8 个 emoji 却超长:一个「字」在系统里有四种长度
javascript·数据库·后端
CV-deeplearning3 小时前
Chrome DevTools实战
前端·agent·chrome devtools·mcp
xurileidian3 小时前
第 30 篇 · 边玩边学Python · 猜数字小游戏 把判断循环异常串起来
前端·python·算法
前端精髓3 小时前
三种方式实现 Agent 工具调用:OpenAI、LangChain 与 LangGraph
前端·数据库·langchain
国科安芯4 小时前
光链路捕获的那一刻:商业激光通信终端捕获跟踪伺服与探测前端的低噪声供电
前端·网络·卫星·电源芯片·抗辐照芯片·在轨
一铭111994 小时前
微信小程序可视化搭建实战:模板、组件与发布验收
前端·低代码·微信小程序·小程序·页面设计·可视化搭建·发布验收
LabVIEW开发4 小时前
LabVIEW 2025 新特性盘点:这几个改进,值得你认真看一遍
前端·labview
画中有画6 小时前
自动化脚本中使用RSA
前端·javascript·自动化
要吃这碗饭7 小时前
Codex failed to revert changes怎么办?2026年4类成因与恢复指南
前端·人工智能