我正在尝试做一个简单的动画使用反应,关键帧,CSS模块(和SASS)。问题是CSS模块散列关键帧名称的方式与其散列本地类的方式相同。
JS代码
//...
export default () => {
const [active, setActive] = useState(false);
return(
<div className={active ? 'active' : 'inactive'}
onClick={() => setActive(!active)}
>content</div>
)
}试图让所有东西都变得全局,使用this源代码作为教程(不编译):
//default scope is local
@keyframes :global(animateIn) {
0% { background: black; }
100% { background: orange; }
}
@keyframes :global(animatOut) {
0% { background: orange; }
100% { background: black; }
}
:global {
.active {
background: orange;
animation-name: animateIn;
animation-duration: 1s;
}
.inactive {
background: black;
animation-name: animateOut;
animation-duration: 1s;
}
}更改此设置也不起作用:
:global {
@keyframes animateIn {
0% { background: black; }
100% { background: orange; }
}
@keyframes animateOut {
0% { background: orange; }
100% { background: black; }
}
}另一次尝试(无效):
@keyframes animateIn {
0% { background: black; }
100% { background: orange; }
}
@keyframes animateOut {
0% { background: orange; }
100% { background: black; }
}
:global {
.active {
background: orange;
:local {
animation-name: animateIn;
}
animation-duration: 1s;
}
.inactive {
background: black;
:local {
animation-name: animateOut;
}
animation-duration: 1s;
}
}如何在CSS模块全局范围内使用关键帧?是否可以在全局范围类中使用局部范围关键帧?
发布于 2019-08-27 12:03:39
您的第三次尝试几乎没问题,您只需在:local之前添加&,并确保它们之间有一个空格。通过这样做,您可以切换到选择器中的本地作用域。
:global {
.selector {
& :local {
animation: yourAnimation 1s ease;
}
}
}
@keyframes yourAnimation {
0% {
opacity: 0;
}
to {
opacity: 1;
}
},它编译为
.selector {
animation: [hashOfYourAnimation] 1s ease;
}发布于 2019-08-30 03:04:05
最初的答案工作得很好。这在没有SASS的情况下工作:
:global {
.selector {
// global selector stuff ...
}
.selector :local {
animation: yourAnimation 1s ease;
}
}
@keyframes yourAnimation {
0% {
opacity: 0;
}
to {
opacity: 1;
}
}发布于 2021-08-28 14:18:50
在我的例子中,上面的解决方案都不起作用,但我发现css-loader可以解析所有的camelCased单词,所以我将我的@keyframes动画名称更改为snake_case,这起作用了!
https://stackoverflow.com/questions/57634370
复制相似问题