2013-10-22 37 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

相關問題