CSS小球碰撞是指使用CSS动画来实现两个或多个小球在网页上相互碰撞的效果。这种效果通常用于演示、游戏或交互式网页设计中。
以下是一个简单的CSS小球碰撞示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Ball Collision</title>
<style>
.container {
position: relative;
width: 500px;
height: 500px;
border: 1px solid black;
}
.ball {
position: absolute;
width: 50px;
height: 50px;
border-radius: 50%;
background-color: red;
animation-timing-function: linear;
animation-iteration-count: infinite;
}
.ball:nth-child(1) {
left: 50px;
top: 50px;
animation-name: moveBall1;
animation-duration: 4s;
}
.ball:nth-child(2) {
left: 200px;
top: 200px;
animation-name: moveBall2;
animation-duration: 5s;
}
@keyframes moveBall1 {
0% { left: 50px; top: 50px; }
50% { left: 400px; top: 400px; }
100% { left: 50px; top: 50px; }
}
@keyframes moveBall2 {
0% { left: 200px; top: 200px; }
50% { left: 100px; top: 100px; }
100% { left: 200px; top: 200px; }
}
</style>
</head>
<body>
<div class="container">
<div class="ball"></div>
<div class="ball"></div>
</div>
</body>
</html>
will-change
属性提示浏览器提前优化。通过以上方法,可以实现一个简单的CSS小球碰撞效果,并解决常见的实现问题。
领取专属 10元无门槛券
手把手带您无忧上云