CSS\JS实现页面背景气泡logo上浮效果

效果图:

单容器显示气泡:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bubble Logo Animation</title>

</head>
<body>
    <div class="bubble-container">
        <!-- 气泡将会动态添加到这里 -->
    </div>


</body>



<style>
body, html {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background-color: #ffffff; /* 背景颜色 */
    display: flex;
    justify-content: center;
    align-items: center;
}

.bubble-container {
    position: relative;
    width: 100%;
    height: 100%;
}

.bubble {
    position: absolute;
    bottom: -50px; /* 从页面底部开始 */
    width: 100px;
    height: 100px;
    opacity: 0.8;
    animation: floatUp 10s infinite;
}

.bubble img {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

/* 气泡上浮动画 */
@keyframes floatUp {
    0% {
        transform: translateY(0) scale(1);
        opacity: 1;
    }
    100% {
        transform: translateY(-100vh) scale(1.2);
        opacity: 0;
    }
}


</style>

<script>
const bubbleContainer = document.querySelector('.bubble-container');

function createBubble() {
    const bubble = document.createElement('div');
    bubble.className = 'bubble';
    bubble.style.left = `${Math.random() * 100}vw`; // 随机水平位置
    bubble.style.animationDuration = `${5 + Math.random() * 5}s`; // 随机动画时长
    bubble.innerHTML = `<img src="https://img-home.csdnimg.cn/images/20201124032511.png" alt="Logo">`;

    bubbleContainer.appendChild(bubble);

    // 动画结束后移除气泡
    bubble.addEventListener('animationend', () => {
        bubble.remove();
    });
}

// 定期生成新的气泡
setInterval(createBubble, 1000);


</script>
</html>

全屏显示气泡:

html 复制代码
  <div class="one" style="width: 100%;height: 300px;background-color: pink;">

  </div>

    <div class="bubble-container">
        <!-- 气泡将会动态添加到这里 -->
    </div>




    <style>
        .one {
            width: 100%;
            height: 300px;
            background-color: pink;
            z-index: 1;
            position: relative;
        }

        /* 覆盖整个页面的气泡容器 */
        .bubble-container {
            position: fixed; /* 使容器固定在页面上 */
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            pointer-events: none; /* 确保气泡不影响页面其他内容的交互 */
            z-index: 9999; /* 将气泡放在最前面 */
        }

        .bubble {
            position: absolute;
            bottom: -50px;
            width: 75px;
            height: 75px;
            opacity: 0.5;
            animation: floatUp 10s infinite;
        }

        .bubble img {
            width: 100%;
            height: 100%;
            object-fit: contain;
        }

        /* 气泡上浮动画 */
        @keyframes floatUp {
            0% {
                transform: translateY(0) scale(1);
                opacity: 0.8;
            }
            100% {
                transform: translateY(-100vh) scale(1.2);
                opacity: 0;
            }
        }
    </style>

<script>
const bubbleContainer = document.querySelector('.bubble-container');

function createBubble() {
    const bubble = document.createElement('div');
    bubble.className = 'bubble';
    bubble.style.left = `${Math.random() * 100}vw`; // 随机水平位置
    bubble.style.animationDuration = `${5 + Math.random() * 5}s`; // 随机动画时长
    bubble.innerHTML = `<img src="https://img-home.csdnimg.cn/images/20201124032511.png" alt="Logo">`;

    bubbleContainer.appendChild(bubble);

    // 动画结束后移除气泡
    bubble.addEventListener('animationend', () => {
        bubble.remove();
    });
}

// 定期生成新的气泡
setInterval(createBubble, 5000);


</script>
相关推荐
繁依Fanyi几秒前
ColorAid —— 一个面向设计师的色盲模拟工具开发记
开发语言·前端·vue.js·编辑器·codebuddy首席试玩官
codelxy4 分钟前
vue引用cesium,解决“Not allowed to load local resource”报错
javascript·vue.js
程序猿阿伟1 小时前
《社交应用动态表情:RN与Flutter实战解码》
javascript·flutter·react native
明似水1 小时前
Flutter 开发入门:从一个简单的计数器应用开始
前端·javascript·flutter
沐土Arvin1 小时前
前端图片上传组件实战:从动态销毁Input到全屏预览的全功能实现
开发语言·前端·javascript
Zww08912 小时前
el-dialog鼠标在遮罩层松开会意外关闭,教程图文并茂
javascript·vue.js·计算机外设
爱编程的鱼2 小时前
C#接口(Interface)全方位讲解:定义、特性、应用与实践
java·前端·c#
sunbyte2 小时前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | 页面布局 与 Vue Router 路由配置
前端·javascript·vue.js·tailwindcss
saadiya~2 小时前
Vue 3 实现后端 Excel 文件流导出功能(Blob 下载详解)
前端·vue.js·excel