使用 CSS3 动画和 Sass @for 循环触发列表
Triggering a list with CSS3 animations and Sass @for loop
我正在尝试使用 for 循环在 Sass 中创建关键帧动画,但我在编写它时遇到了困难。我想要的是让每个项目一个接一个地出现,但稍有延迟。有点像一堆卡片。
这是一个codepen。这是代码:
<ul>
<li><a href="#About">About</a></li>
<li><a href="#Contact">Contact</a></li>
<li><a href="Labs">Labs</a></li>
</ul>
html {
font-size: 62.5%;
box-zising: border-box;
}
*,
*:before,
*:after {
box-zising: inherit;
}
html, body {
width: 100vw;
height: 100vh;
background: black;
font-size: 1rem;
}
ul {
position: fixed;
display: flex;
flex-direction: column;
align-items: flex-start;
border: 1px solid white;
width: 100vw;
height: 100vh;
}
li {
list-style: none;
border: 1px solid white;
width: 100%;
text-align: center;
margin-top: 10px;
background: red;
@for $i from 1 through 4 {
&:nth-of-type(#{$i}) {
animation: slideTop;
animation-duration: 1.5s + (40ms * $i);
animation-iteration-count: 5;
animation-delay: 2.5s + (40ms * $i);
}
}
a {
display: block;
padding: 50px;
color: white;
font-size: 24px;
text-decoration: none;
letter-spacing: 0.1em;
}
}
@keyframes slideTop {
0% {
opacity: 0;
transform: scaleY(0);
}
100% {
opacity: 1;
transform: scaleY(50px);
}
}
看这里 > jsfiddle
你必须设置一个更大的 animation-delay
这样你才能看到 li
一个接一个地出现
并且您必须将 opacity:0
与 animation-fill-more:forwards
一起设置,因此起初 li
被隐藏,然后它们出现并与 opacity:1
保持一致
让我知道这是否是您要找的东西
代码:
(代码段不起作用,因为它没有 SASS;我只是把它放在网站上,以便代码在此处可见)
html {
font-size: 62.5%;
box-zising: border-box;
}
*,
*:before,
*:after {
box-zising: inherit;
}
html, body {
width: 100vw;
height: 100vh;
background: black;
font-size: 1rem;
}
ul {
position: fixed;
display: flex;
flex-direction: column;
align-items: flex-start;
border: 1px solid white;
width: 100vw;
height: 100vh;
}
li {
list-style: none;
border: 1px solid white;
width: 100%;
text-align: center;
margin-top: 10px;
background: red;
opacity:0;
@for $i from 1 through 4 {
&:nth-child(#{$i}) {
animation: slideTop;
animation-duration: 1s + ($i*400ms);
animation-iteration-count: 1;
animation-delay: 2.5s + ($i*400ms);
animation-fill-mode:forwards;
}
}
a {
display: block;
padding: 50px;
color: white;
font-size: 24px;
text-decoration: none;
letter-spacing: 0.1em;
}
}
@keyframes slideTop {
0% {
opacity: 0;
transform: scaleY(0);
}
100% {
opacity: 1;
transform: scaleY(50px);
}
}
<ul>
<li><a href="#About">About</a></li>
<li><a href="#Contact">Contact</a></li>
<li><a href="Labs">Labs</a></li>
</ul>
我正在尝试使用 for 循环在 Sass 中创建关键帧动画,但我在编写它时遇到了困难。我想要的是让每个项目一个接一个地出现,但稍有延迟。有点像一堆卡片。
这是一个codepen。这是代码:
<ul>
<li><a href="#About">About</a></li>
<li><a href="#Contact">Contact</a></li>
<li><a href="Labs">Labs</a></li>
</ul>
html {
font-size: 62.5%;
box-zising: border-box;
}
*,
*:before,
*:after {
box-zising: inherit;
}
html, body {
width: 100vw;
height: 100vh;
background: black;
font-size: 1rem;
}
ul {
position: fixed;
display: flex;
flex-direction: column;
align-items: flex-start;
border: 1px solid white;
width: 100vw;
height: 100vh;
}
li {
list-style: none;
border: 1px solid white;
width: 100%;
text-align: center;
margin-top: 10px;
background: red;
@for $i from 1 through 4 {
&:nth-of-type(#{$i}) {
animation: slideTop;
animation-duration: 1.5s + (40ms * $i);
animation-iteration-count: 5;
animation-delay: 2.5s + (40ms * $i);
}
}
a {
display: block;
padding: 50px;
color: white;
font-size: 24px;
text-decoration: none;
letter-spacing: 0.1em;
}
}
@keyframes slideTop {
0% {
opacity: 0;
transform: scaleY(0);
}
100% {
opacity: 1;
transform: scaleY(50px);
}
}
看这里 > jsfiddle
你必须设置一个更大的 animation-delay
这样你才能看到 li
一个接一个地出现
并且您必须将 opacity:0
与 animation-fill-more:forwards
一起设置,因此起初 li
被隐藏,然后它们出现并与 opacity:1
保持一致
让我知道这是否是您要找的东西
代码:
(代码段不起作用,因为它没有 SASS;我只是把它放在网站上,以便代码在此处可见)
html {
font-size: 62.5%;
box-zising: border-box;
}
*,
*:before,
*:after {
box-zising: inherit;
}
html, body {
width: 100vw;
height: 100vh;
background: black;
font-size: 1rem;
}
ul {
position: fixed;
display: flex;
flex-direction: column;
align-items: flex-start;
border: 1px solid white;
width: 100vw;
height: 100vh;
}
li {
list-style: none;
border: 1px solid white;
width: 100%;
text-align: center;
margin-top: 10px;
background: red;
opacity:0;
@for $i from 1 through 4 {
&:nth-child(#{$i}) {
animation: slideTop;
animation-duration: 1s + ($i*400ms);
animation-iteration-count: 1;
animation-delay: 2.5s + ($i*400ms);
animation-fill-mode:forwards;
}
}
a {
display: block;
padding: 50px;
color: white;
font-size: 24px;
text-decoration: none;
letter-spacing: 0.1em;
}
}
@keyframes slideTop {
0% {
opacity: 0;
transform: scaleY(0);
}
100% {
opacity: 1;
transform: scaleY(50px);
}
}
<ul>
<li><a href="#About">About</a></li>
<li><a href="#Contact">Contact</a></li>
<li><a href="Labs">Labs</a></li>
</ul>