2013-09-05 25 views
17

我需要在我SCSS代碼來定義的寬度像這樣:Sass是連接而不是添加?

#example { 
    width: $currentWidth + 349 !important; 
} 

$currentWidth由循環定義。

然而,薩斯最終總是串聯的兩個數字,而不是做算術。

我也曾嘗試:

width: #{$currentWidth + 349}px !important;

仍然導致串聯。

我不知道我在做什麼錯?我知道這是令人難以置信的基本的,但我也似乎無法找到薩斯如何處理算術

+4

是'$ currentWidth'字符串或數?如果它是一個字符串,那麼它將連接。薩斯只能做算術,如果兩個實體是數量型的(即薩斯解釋'「10」 + 349'它'同樣的方式「肉餅」 + 349')。 – cimmanon

回答

26

假設$currentWidth是一個整數良好的信息。

SASS:

$currentWidth: 30; 

#example { 
    width: unquote(($currentWidth + 349) + 'px ' + !important); 
} 

CSS OUTPUT:

#example { 
    width: 379px !important; 
} 

你可以在這裏進行測試:

http://sass-lang.com/try.html

+0

通過將單位連接到像這樣的整數,你會得到一個字符串。這是錯誤的,你應該*永遠不*做這個*永遠*。 – cimmanon

+1

正是我說:'($ currentWidth + 349)+ '像素''是一個字符串。如果你試圖將單位價值改變爲統一價值,你應該從不**這樣做。 – cimmanon

+0

好吧,我看你在說什麼,但你沒有真正解釋爲什麼「從不」這樣做。另外,你會推薦什麼?這會'#示例{ 寬度:ABS($ currentWidth + 349)+ PX重要;! }'是更好的方法嗎? –

相關問題