通过CSS3实现图像动画并显示图像后面的文本,通过这个例子来练习扩展你的CSS知识,并开动脑筋创建你自己独特的动画。
先上HTML结构:
<div class="container">
<div class="image-container">
<img src="https://jesperekstrom.com/wp-content/uploads/2018/11/Wordpress-folder-purple.png" alt="wordpress-folder-icon">
</div>
<div class="text-container">
<h1>Animation</h1>
<div class="fading-effect"></div>
</div>
</div>
CSS结构:
// Malteser Wordpress Animation
@media screen and (min-width:1000px) {
@keyframes image-slide {
0% {
transform: translatex(-250px) scale(0);
}
60% {
transform: translatex(-250px) scale(1);
}
90% {
transform: translatex(150px) scale(1);
}
100% {
transform: translatex(150px) scale(1);
}
}
@keyframes text-slide {
0% {
width: 100%;
}
60% {
width: 100%;
}
77% {
width: 0;
}
100% {
width: 0;
}
}
}
@media screen and (max-width:1000px) {
@keyframes image-slide {
0% {
transform: translatex(-150px) scale(0);
}
60% {
transform: translatex(-150px) scale(1);
}
90% {
transform: translatex(120px) scale(1);
}
100% {
transform: translatex(120px) scale(1);
}
}
@keyframes text-slide {
0% {
width: 100%;
}
60% {
width: 100%;
}
77%{
width: 0;
}
100% {
width: 0;
}
}
}
@media screen and (max-width:450px) {
@keyframes image-slide {
0% {
transform: translatex(-80px) scale(0);
}
60% {
transform: translatex(-80px) scale(1);
}
90% {
transform: translatex(80px) scale(1);
}
100% {
transform: translatex(80px) scale(1);
}
}
@keyframes text-slide {
0% {
width: 100%;
}
60% {
width: 100%;
}
77% {
width: 0;
}
100% {
width: 0;
}
}
}
body {
background-color: white;
}
.container {
width: 100%;
height: 100vh;
display: block;
position: relative;
overflow: hidden;
}
.image-container {
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%,-50%);
transform: translate(-50%,-50%);
z-index: 2;
img {
-webkit-animation: image-slide 4s cubic-bezier(.5,.5,0,1);
animation: image-slide 4s cubic-bezier(.5,.5,0,1);
animation-fill-mode: forwards;
-webkit-animation-fill-mode: forwards;
-webkit-filter: drop-shadow(-4px 5px 5px rgba(0,0,0,0.6));
filter: drop-shadow(-4px 5px 5px rgba(0,0,0,0.6));
height: 200px;
}
}
.text-container {
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%,-50%);
transform: translate(-50%,-50%);
z-index: 1;
margin-left: -100px;
h1 {
text-transform: uppercase;
font-size: 35px;
font-family: sans-serif;
}
.fading-effect {
position: absolute;
top: 0;
bottom: 0;
right: 0;
width: 100%;
background: white;
-webkit-animation: text-slide 4s cubic-bezier(.5,.5,0,1);
animation: text-slide 4s cubic-bezier(.5,.5,0,1);
animation-fill-mode: forwards;
-webkit-animation-fill-mode: forwards;
}
}
@media screen and (max-width: 1000px) {
.image-container img {
height: 150px;
}
.text-container {
margin-left: -80px;
}
.text-container h1 {
font-size: 30px;
}
}
@media screen and (max-width: 450px) {
.image-container img {
height: 120px;
}
.text-container {
margin-left: -70px;
}
.text-container h1 {
font-size: 25px;
}
}