广州总部电话:020-85564311
20年
互联网应用服务商
广州总部电话:020-85564311
20年
互联网应用服务商
请输入搜索关键词
知识库 知识库

优网知识库

探索行业前沿,共享知识宝库

动态气泡效果网页动态背景源码

发布日期:2026-05-12 11:34:40 浏览次数: 809 来源:黄献新
推荐语
想为网站添加梦幻的动态气泡背景吗?这份源码简单三步即可实现,效果惊艳。

核心内容:
1. 动态气泡背景的实现原理与视觉效果
2. 源码的获取与三步快速使用方法
3. 代码的核心结构与可自定义效果
小优 网站建设顾问
专业来源于二十年的积累,用心让我们做到更好!
源码如何使用
1.新建一个记事本,把代码粘贴进去
2.保存,关闭记事本
3.修改记事本后缀TXT编程html,然后双击就可以查看了
泡泡是随机飘动的效果很好,适合做网站背景
<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <meta name="viewport" content="width=device-width, initial-scale=1.0">  <title>Animated Bubbles Background</title>  <style>    /* 全局样式 */    body {      font-family: Arial, sans-serif;      margin0;      padding0;      backgroundlinear-gradient(135deg#0f3443 0%#34e89e 100%);      display: flex;      justify-content: center;      align-items: center;      min-height100vh;      overflow: hidden;    }
    /* 气泡容器样式 */    .bubbles-container {      position: fixed;      top0;      left0;      width100%;      height100%;      z-index: -1;    }
    /* 单个气泡样式 */    .bubble {      position: absolute;      border-radius50%;      backgroundrgba(2552552550.3);      border1px solid rgba(2552552550.5);      animation: float 8s infinite ease-in-out;      backdrop-filterblur(3px);      transition: transform 0.3s ease;    }
    /* 气泡悬停效果 */    .bubble:hover {      transformscale(1.2);      box-shadow0 0 20px rgba(2552552550.8);    }
    /* 标题样式 */    h1 {      color: white;      text-align: center;      text-shadow0 2px 10px rgba(0000.3);      font-sizeclamp(2rem5vw3.5rem);      font-family'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;      z-index1;    }  </style></head><body>  <div class="bubbles-container" id="bubblesContainer"></div>  <h1>Animated Bubbles</h1>
  <script>    // 气泡配置    const BUBBLE_COUNT = 7// 气泡数量    const COLORS = ['rgba(255, 255, 255, 0.3)''rgba(255, 255, 255, 0.4)''rgba(255, 255, 255, 0.5)'];
    // 创建气泡    function createBubbles() {      const container = document.getElementById('bubblesContainer');
      for (let i = 0; i < BUBBLE_COUNT; i++) {        const bubble = document.createElement('div');        bubble.className = 'bubble';
        // 随机大小        const size = Math.random() * 80 + 20// 20px 到 100px        bubble.style.width = `${size}px`;        bubble.style.height = `${size}px`;
        // 随机位置        const left = Math.random() * 100// 0% 到 100%        const top = Math.random() * 100;  // 0% 到 100%        bubble.style.left = `${left}%`;        bubble.style.top = `${top}%`;
        // 随机颜色        const color = COLORS[Math.floor(Math.random() * COLORS.length)];        bubble.style.background = color;
        // 随机动画延迟和持续时间        const delay = Math.random() * 5// 0s 到 5s        const duration = Math.random() * 10 + 10// 10s 到 20s        bubble.style.animationDelay = `${delay}s`;        bubble.style.animationDuration = `${duration}s`;
        // 添加自定义数据属性        bubble.dataset.directionX = Math.random() > 0.5 ? 1 : -1// 水平方向        bubble.dataset.directionY = Math.random() > 0.5 ? 1 : -1// 垂直方向        bubble.dataset.speed = Math.random() * 0.5 + 0.1// 移动速度
        container.appendChild(bubble);      }
      // 启动气泡动画      animateBubbles();    }
    // 气泡随机游动动画    function animateBubbles() {      const bubbles = document.querySelectorAll('.bubble');      const container = document.getElementById('bubblesContainer');      const containerRect = container.getBoundingClientRect();
      function updateBubblePosition(bubble) {        const rect = bubble.getBoundingClientRect();        let left = parseFloat(bubble.style.left);        let top = parseFloat(bubble.style.top);        const directionX = parseFloat(bubble.dataset.directionX);        const directionY = parseFloat(bubble.dataset.directionY);        const speed = parseFloat(bubble.dataset.speed);
        // 更新位置        left += directionX * speed;        top += directionY * speed;
        // 边界检测 - 水平方向        if (left < 0 || left + rect.width > containerRect.width) {          bubble.dataset.directionX = -directionX; // 反转水平方向        }
        // 边界检测 - 垂直方向        if (top < 0 || top + rect.height > containerRect.height) {          bubble.dataset.directionY = -directionY; // 反转垂直方向        }
        // 确保气泡不会超出容器        left = Math.max(0Math.min(left, containerRect.width - rect.width));        top = Math.max(0Math.min(top, containerRect.height - rect.height));
        // 更新样式        bubble.style.left = `${left}px`;        bubble.style.top = `${top}px`;      }
      // 动画循环      function animate() {        bubbles.forEach(bubble => updateBubblePosition(bubble));        requestAnimationFrame(animate);      }
      animate();    }
    // 页面加载完成后创建气泡    window.addEventListener('DOMContentLoaded', createBubbles);
    // 窗口大小变化时重新计算气泡位置    window.addEventListener('resize'() => {      const bubbles = document.querySelectorAll('.bubble');      bubbles.forEach(bubble => {        bubble.style.left = `${parseFloat(bubble.style.left) * window.innerWidth / 100}px`;        bubble.style.top = `${parseFloat(bubble.style.top) * window.innerHeight / 100}px`;      });    });  </script></body></html>

优网科技,优秀企业首选的互联网供应服务商

优网科技秉承"专业团队、品质服务" 的经营理念,诚信务实的服务了近万家客户,成为众多世界500强、集团和上市公司的长期合作伙伴!

优网科技成立于2001年,擅长网站建设、网站与各类业务系统深度整合,致力于提供完善的企业互联网解决方案。优网科技提供PC端网站建设(品牌展示型、官方门户型、营销商务型、电子商务型、信息门户型、微信小程序定制开发、移动端应用(手机站APP开发)、微信定制开发(微信官网、微信商城、企业微信)等一系列互联网应用服务。


我要投稿

姓名

文章链接

提交即表示你已阅读并同意《个人信息保护声明》

专属顾问 专属顾问
扫码咨询您的优网专属顾问!
专属顾问
马上咨询