使用自定义类名和animate.css动画库给一张图片的显示添加入场动画,动画效果自定义
时间: 2024-02-16 11:03:31 浏览: 25
好的,这里给你提供一种使用自定义类名和animate.css动画库给一张图片添加入场动画的示例。使用animate.css可以方便地实现CSS动画效果,具体可以根据需求自定义。
首先,需要在HTML文件中引入animate.css的CDN链接:
```html
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" />
```
然后,HTML代码如下:
```html
<div class="image-wrapper animate__animated">
<img src="image.jpg" alt="图片">
</div>
```
CSS代码如下:
```css
.image-wrapper {
position: relative;
display: inline-block;
}
.image-wrapper.animate__animated {
animation-duration: 1s; /* 设置动画持续时间 */
animation-delay: 0.5s; /* 设置动画延迟时间 */
animation-fill-mode: both; /* 设置动画结束后保持最后状态 */
animation-name: slideInLeft; /* 设置动画名称 */
}
@keyframes slideInLeft {
from {
transform: translateX(-100%);
}
to {
transform: translateX(0);
}
}
.image-wrapper img {
display: block;
max-width: 100%;
height: auto;
}
```
这段代码使用了animate.css库中的slideInLeft动画效果,通过给`.image-wrapper`添加类名`.animate__animated`来触发动画效果。其中,`.image-wrapper.animate__animated`使用了CSS3的动画属性animation,通过设置animation-duration、animation-delay、animation-fill-mode、animation-name属性来定义动画效果。同时,利用@keyframes定义了从左侧平移进入的动画效果。最后,`.image-wrapper img`设置了图片的基本样式,其中max-width: 100%可以确保图片的宽度不会超出其容器的宽度。