网页动画气泡
HTML部分
<section>
<h2>Bubbles</h2>
</section>
CSS部分
* {
margin: 0;
padding: 0;
}
section {
position: relative;
width: 100%;
height: 100vh;
overflow: hidden;
background: #111;
display: flex;
justify-content: center;
align-items: center;
}
section h2 {
font-size: 10em;
color: #333;
}
section span {
position: absolute;
bottom: 50px;
background: transparent;
border-radius: 50%;
pointer-events: none;
box-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
animation: animate 4s linear infinite;
}
section span:before {
content: '';
position: absolute;
width: 100%;
height: 100%;
/* 缩放,大于1 放大,小于1 缩小 倍数 */
transform: scale(0.25) translate(-70%, -70%);
/* 渐变 */
background: radial-gradient(#fff, transparent);
border-radius: 50%;
}
@keyframes animate {
0% {
transform: translateY(0%);
opacity: 1;
}
99% {
opacity: 1;
}
100% {
transform: translateY(-1200%);
opacity: 1;
}
}
JS部分
function createBubble() {
const section = document.querySelector('section');
const createElement = document.createElement('span')
var size = Math.random() * 60;
createElement.style.width = 20 + size + 'px';
createElement.style.height = 20 + size + 'px';
createElement.style.left = Math.random() * innerWidth + 'px';
section.appendChild(createElement);
setTimeout(() => {
createElement.remove()
}, 4000)
}
setInterval(createBubble, 50)
动态气泡