效果图:
源码:
<!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>