用css做一个小球不间断融入右侧盒子动画效果
时间: 2024-01-29 21:03:46 浏览: 23
好的,以下是一个实现方法:
HTML部分:
```html
<div class="container">
<div class="ball"></div>
<div class="box"></div>
</div>
```
CSS部分:
```css
.container {
width: 500px;
height: 500px;
background-color: #f2f2f2;
position: relative;
}
.ball {
width: 50px;
height: 50px;
background-color: #ff9900;
border-radius: 50%;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) scale(0.2);
animation: ball-in 2s ease-in-out infinite;
}
.box {
width: 150px;
height: 150px;
background-color: #ccc;
position: absolute;
top: 50%;
right: -75px;
transform: translateY(-50%);
}
@keyframes ball-in {
0% {
transform: translate(-50%, -50%) scale(0.2);
}
50% {
transform: translate(50%, -50%) scale(1.2);
}
100% {
transform: translate(150%, -50%) scale(0.2);
}
}
```
在这个例子中,我们使用了 CSS3 动画来实现小球不间断地融入右侧盒子的效果。关键是使用了 `transform` 属性,将小球的初始大小设置为 0.2,然后在动画中逐渐放大到 1.2,最后移动到右侧盒子内部并缩小到最小,使得小球看起来像是不断地在融入右侧盒子中。同时,我们使用了 `translate` 属性来控制小球的移动,在动画中将小球从中心移动到右侧盒子内部。