2012-10-03 57 views
0

我已經創建了一個LESS mixin,用於接受多個數值的邊界,並使用rem測量單位和px回退來分析css值/屬性對。LESS mixin邏輯

這是我需要幫助:

  1. 我想混入到能夠接受有效的基於字符串的財產,如「自動」或「繼承」
  2. 我想只能聲明這些參數是多餘的。例如。如果我只需要一個保證金餘額,那麼我只需要寫一些類似.margin(nope,nope,nope,1);的東西。

這是我到目前爲止有:

.margin(@sizeValueTop: auto, @sizeValueRight: 0, @sizeValueBottom: 0, @sizeValueLeft: 0) { 
    @pxValueTop: (@sizeValueTop * 16); 
    @remValueTop: (@sizeValueTop); 
    @pxValueRight: (@sizeValueRight * 16); 
    @remValueRight: (@sizeValueRight); 
    @pxValueBottom: (@sizeValueBottom * 16); 
    @remValueBottom: (@sizeValueBottom); 
    @pxValueLeft: (@sizeValueLeft * 16); 
    @remValueLeft: (@sizeValueLeft); 
    margin-top: ~"@{pxValueTop}px"; 
    margin-top: ~"@{remValueTop}rem"; 
    margin-right: ~"@{pxValueRight}px"; 
    margin-right: ~"@{remValueRight}rem"; 
    margin-bottom: ~"@{pxValueBottom}px"; 
    margin-bottom: ~"@{remValueBottom}rem"; 
    margin-left: ~"@{pxValueLeft}px"; 
    margin-left: ~"@{remValueLeft}rem"; 
} 

body{ 
    .margin(1,1,1,1); 
} 

任何幫助將是巨大的。

回答

1

1)要防止它做auto * 16的數學?這是不可能的,除非他們在我看不到的時候滑入if/else控制語句。衛兵是你檢查論證類型的唯一方法。這意味着同一個mixin的多個聲明可以涵蓋所有的基礎。雖然,你可以有mixin調用其他mixins ...

2)也是不可能的。將最有可能離開的參數放置爲最後的默認值。在你的例子中,在0之上輸入nope沒有什麼優勢,因爲你的參數的3/4缺省值是0