@for 在样式组件中循环
@for loops in styled-components
我试图在 styled-components 文档中找到一种创建循环的方法。例如,我可以在 styled-components 中实现这个 scss
循环吗?
@for $i from 0 through 20 {
#loadingCheckCircleSVG-#{$i} {
animation: fill-to-white 5000ms;
animation-delay: ($i - 1.5) * 1s;
fill: white;
opacity: 0;
}
}
可能吗?
我们目前不支持 scss 的迭代语法。但是,由于 styled-components 接受 JS 插值,您只需编写一个简单的 JS 函数即可制作所需的 CSS 并将其放入。
import styled, { css } from "styled-components";
function createCSS() {
let styles = '';
for (let i = 0; i < 20; i += 1) {
styles += `
#loadingCheckCircleSVG-${i} {
animation: fill-to-white 5000ms;
animation-delay: ${i - 1.5}s;
fill: white;
opacity: 0;
}
`
}
return css`${styles}`;
}
const Thing = styled.div`
${createCSS()};
`
我试图在 styled-components 文档中找到一种创建循环的方法。例如,我可以在 styled-components 中实现这个 scss
循环吗?
@for $i from 0 through 20 {
#loadingCheckCircleSVG-#{$i} {
animation: fill-to-white 5000ms;
animation-delay: ($i - 1.5) * 1s;
fill: white;
opacity: 0;
}
}
可能吗?
我们目前不支持 scss 的迭代语法。但是,由于 styled-components 接受 JS 插值,您只需编写一个简单的 JS 函数即可制作所需的 CSS 并将其放入。
import styled, { css } from "styled-components";
function createCSS() {
let styles = '';
for (let i = 0; i < 20; i += 1) {
styles += `
#loadingCheckCircleSVG-${i} {
animation: fill-to-white 5000ms;
animation-delay: ${i - 1.5}s;
fill: white;
opacity: 0;
}
`
}
return css`${styles}`;
}
const Thing = styled.div`
${createCSS()};
`