我曾尝试使用CSS关键帧制作动画,但设计已中断。因为它只是一个简单的动画代码,如何在不编写媒体查询的情况下使其具有响应性?
div {
font-family: "Comic Sans MS", sans-serif;
margin-left: 200px;
margin-top: 200px;
}
span {
color: white;
font-size: 35px;
padding: 14px 25px;
margin: 5px;
border-radius: 10px;
display: inline-block;
animation: move 0.8s infinite linear;
}
@keyframes move {
0% {
transform: translateY(0px);
opacity: 1.0;
}
50% {
transform: translateY(-50px);
opacity: 0.5;
}
100% {
transform: translateY(0px);
opacity: 1.0;
}
}
span:nth-child(1) {
animation-delay: 0.1s;
background-color: red;
box-shadow: 5px 5px 0 black;
}
span:nth-child(2) {
animation-delay: 0.2s;
background-color: blue;
box-shadow: 5px 5px 0 black;
}
span:nth-child(3) {
animation-delay: 0.3s;
background-color: green;
box-shadow: 5px 5px 0 black;
}
span:nth-child(4) {
animation-delay: 0.4s;
background-color: orangered;
box-shadow: 5px 5px 0 black;
}
span:nth-child(5) {
animation-delay: 0.5s;
background-color: springgreen;
box-shadow: 5px 5px 0 black;
}
span:nth-child(6) {
animation-delay: 0.6s;
background-color: blueviolet;
box-shadow: 5px 5px 0 black;
}
span:nth-child(7) {
animation-delay: 0.7s;
background-color: purple;
box-shadow: 5px 5px 0 black;
}
<div>
<span>W</span>
<span>E</span>
<span>L</span>
<span>C</span>
<span>O</span>
<span>M</span>
<span>E</span>
</div>
最佳答案
在不使用媒体查询的情况下,您只能进行操作,但是在此示例中,我删除了设置的margin-left
值,而是使用了一个百分比,并将包含的div
设置为一个百分比宽度。我还将父级div
设置为flex容器-这样可以确保在较小的屏幕上包装内容不会遇到麻烦。
然后,我将vw
单位用作字体大小,因此它将随窗口宽度缩放。此外,框上的填充也需要设置为百分比。您可以在这里玩:
https://jsfiddle.net/disinfor/s5hkyr2f/3/
div {
font-family: "Comic Sans MS", sans-serif;
margin: 200px auto 0;
width: 90%;
display: flex;
justify-content: center;
}
span {
color: white;
font-size: 4vw;
padding: 2% 3%;
margin: 5px;
border-radius: 10px;
display: inline-block;
animation: move 0.8s infinite linear;
}
@keyframes move {
0% {
transform: translateY(0px);
opacity: 1.0;
}
50% {
transform: translateY(-50px);
opacity: 0.5;
}
100% {
transform: translateY(0px);
opacity: 1.0;
}
}
span:nth-child(1) {
animation-delay: 0.1s;
background-color: red;
box-shadow: 5px 5px 0 black;
}
span:nth-child(2) {
animation-delay: 0.2s;
background-color: blue;
box-shadow: 5px 5px 0 black;
}
span:nth-child(3) {
animation-delay: 0.3s;
background-color: green;
box-shadow: 5px 5px 0 black;
}
span:nth-child(4) {
animation-delay: 0.4s;
background-color: orangered;
box-shadow: 5px 5px 0 black;
}
span:nth-child(5) {
animation-delay: 0.5s;
background-color: springgreen;
box-shadow: 5px 5px 0 black;
}
span:nth-child(6) {
animation-delay: 0.6s;
background-color: blueviolet;
box-shadow: 5px 5px 0 black;
}
span:nth-child(7) {
animation-delay: 0.7s;
background-color: purple;
box-shadow: 5px 5px 0 black;
}
<div>
<span>W</span>
<span>E</span>
<span>L</span>
<span>C</span>
<span>O</span>
<span>M</span>
<span>E</span>
</div>