HTML向四周扩散背景

html 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>扩散背景效果</title>
    <style>
      body {
        margin: 0;
        height: 100vh;
        display: flex;
        justify-content: center;
        align-items: center;
        background-color: white;
        overflow: hidden;
      }

      button {
        padding: 10px 20px;
        font-size: 16px;
        cursor: pointer;
      }

      #overlay {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background-color: black;
        opacity: 0;
        pointer-events: none;
        z-index: 100;
      }

      #circle {
        position: fixed;
        top: 50%;
        left: 50%;
        width: 0;
        height: 0;
        background-color: black;
        border-radius: 50%;
        transform: translate(-50%, -50%);
        z-index: 101;
      }
    </style>
  </head>
  <body>
    <button id="toggleBtn">点击切换背景</button>
    <div id="overlay"></div>
    <div id="circle"></div>

    <script src="script.js"></script>
  </body>
  <script>
    document.addEventListener("DOMContentLoaded", () => {
      const toggleBtn = document.getElementById("toggleBtn");
      const overlay = document.getElementById("overlay");
      const circle = document.getElementById("circle");
      let isAnimating = false;

      toggleBtn.addEventListener("click", () => {
        if (!isAnimating) {
          isAnimating = true;

          // 开始扩散动画
          let radius = 0;
          const maxRadius = Math.sqrt(
            window.innerWidth * window.innerWidth +
              window.innerHeight * window.innerHeight
          );

          const animate = () => {
            if (radius < maxRadius) {
              radius += 5;
              circle.style.width = `${radius * 2}px`;
              circle.style.height = `${radius * 2}px`;
              animate();
            } else {
              // 扩散完成后,显示黑色背景
              overlay.style.opacity = "1";
              circle.style.opacity = "0";
              isAnimating = false;
            }
          };

          animate();
        }
      });
    });
  </script>
</html>
相关推荐
疏狂难除2 分钟前
【Tauri2】046—— tauri_plugin_clipboard_manager(一)
前端·clipboard·tauri2
污斑兔21 分钟前
VMWare清理后,残留服务删除方案详解
前端
gong1917231696731 分钟前
非受控组件在React中的使用场景有哪些?
前端·javascript·react.js
TE-茶叶蛋32 分钟前
React 常见的陷阱之(如异步访问事件对象)
前端·javascript·react.js
zhangpeng4555479401 小时前
C++编程起步项目
开发语言·前端·c++
秋田君2 小时前
构建安全的Vue前后端分离架构:利用长Token与短Token实现单点登录(SSO)策略
前端·javascript·vue.js·elementui·前端框架·mock·sso单点登录客户端
GISer_Jing2 小时前
Canvas &SVG &BpmnJS编辑器中Canvas与SVG职能详解
前端·javascript·编辑器
2402_881319302 小时前
web开发全过程总结
前端·javascript·vue.js
一峰说2 小时前
power BI 倒计时+插件HTML Content,实现更新倒计时看板!
前端·html
2301_816169612 小时前
vue路由小案例
前端·javascript·vue.js