2013-10-22 41 views
1

我有这个SASS混入,应该让一个按钮闪烁:为什么-webkit-keyframes无法在我的SASS mixin中工作?

@mixin background_animation($color) { 
    -webkit-animation: backgroundAnimation 800ms infinite; 
    @-webkit-keyframes backgroundAnimation { 
    0%  {background-color: $color;} 
    50%  {background-color: red;} 
    100% {background-color: $color;} 
    } 
} 

我使用它是这样的:

@include background_animation(#000000); 

但是,它不工作。该按钮的背景颜色不会闪烁。

有人可以告诉我我在这里失踪了吗?

P.S.代码工作正常时,不包括它作为一个混合。

生成CSS是这样的:

-webkit-animation-delay: 0s; 
-webkit-animation-direction: normal; 
-webkit-animation-duration: 0.800000011920929s; 
-webkit-animation-fill-mode: none; 
-webkit-animation-iteration-count: infinite; 
-webkit-animation-name: backgroundAnimation; 
-webkit-animation-timing-function: cubic-bezier(0.25, 0.1, 0.25, 1); 

... other rules omitted for brevity 
+0

生成的CSS看起来像什么? – cimmanon

+0

@cinnamoon:我更新了我的OP。 – Tintin81

+0

所提供的代码不可能生成该CSS。 – cimmanon

回答

2

SASS不产生编译后的预期效果。这就是你得到这是错误的:

.box { 
    -webkit-animation: backgroundAnimation 800ms infinite; 
} 
@-webkit-keyframes backgroundAnimation { 
    .box 0% { 
    background-color: black; 
    } 
    .box 50% { 
    background-color: red; 
    } 
    .box 100% { 
    background-color: black; 
    } 
} 

用法:

.box { 
    @include background_animation(#000000); 
} 

基本上你不想为关键帧.box的选择。

这里是working DEMO器(Chrome)

UPDATE

您在这里采取略有错误的做法。试试这个代码片段,而不是:

@mixin keyframes($animation-name) { 
    @-webkit-keyframes $animation-name { 
    @content; 
    } 
    /* 
    Other browser prefixes here 
    @-moz-keyframes $animation-name { 
    @content; 
    }*/ 
} 

@mixin animation($str) { 
    -webkit-animation: #{$str}; 
} 

@include keyframes(background_animation) { 
    0%  {background-color: red;} 
    50%  {background-color: green;} 
    100% {background-color: blue;} 
} 

div { 
    @include animation('background_animation 800ms infinite'); 
} 

应该编译这样:

@-webkit-keyframes background_animation { 
    0% { 
    background-color: red; 
    } 

    50% { 
    background-color: green; 
    } 

    100% { 
    background-color: blue; 
    } 
} 
/* 
Other browser prefixes here 
@-moz-keyframes $animation-name { 
    @content; 
}*/ 
div { 
    -webkit-animation: background_animation 800ms infinite; 
} 

将会产生this result in chrome

+0

嗨,谢谢你的帮助。你的小提琴很容易理解。这实际上是我在切换到SASS之前所做的。对我来说主要的问题仍然是将至少一个'color'传递给SASS mixin。我仍然不明白该怎么做。 – Tintin81

+1

查看更新的答案。希望这可以帮助你。祝你好运! –

+0

再次感谢!你的代码工作,但我的问题仍然是,我需要传递一个'color'到mixin。据我所见,你在那里硬编码“红色”,“绿色”和“蓝色”。但有时我需要对其他元素产生影响,并且一种颜色不同,例如而不是'绿色'我需要'橙色'。仍然试图找到一个解决方案...... – Tintin81

相关问题