2015-11-13 78 views
-1

首先,請注意,我對Sass真的很陌生。sass mixin參數可以是一個全局變量嗎?

我正在嘗試創建一個爲我編譯a:active和a:hover效果的混入。我也試圖將mixin標準化,以便可以在不同的場景中使用。

現在的問題是,$main-color全局變量可以像下面的mixin一樣傳遞參數的默認值嗎?所以如果你沒有通過一個特定的值,它總是使用#C5003E作爲默認值。

$main-color: #C5003E; 

@mixin underLine($color: $main-color, $underline-thickness: 1px,) 

blockquote a { 
    color: $bg-color; 

    // 
    // Display 'special dashed' underline on hover 
    // 
    background: linear-gradient(90deg, $color 66%, transparent 0) repeat-x; 
    background-size: .2em $underline-thickness; 
    background-position: 0 1.3em; 

    // 
    // Display 'special' underline on hover 
    // 
    &:hover { 
    background: linear-gradient($color, $color) no-repeat; 
    background-size: 100% $underline-thickness; 
    background-position: 0 1.3em; 
    } 
} 

我也不知道怎麼standarize這個混入

太謝謝你了!

+0

試試看看? – cimmanon

回答

-1

當然,它工作正常,你可以使用全局變量像$main-color作爲傳遞的參數

$main-color: #C5003E; 

@mixin underLine($color: $main-color, $underline-thickness: 1px) { 
    blockquote a { 
    color: $color; 

    // Display 'special dashed' underline on hover 
    background: linear-gradient(90deg, $color 66%, transparent 0) repeat-x; 
    background-size: .2em $underline-thickness; 
    background-position: 0 1.3em; 

    // Display 'special' underline on hover 
    &:hover { 
     background: linear-gradient($color, $color) no-repeat; 
     background-size: 100% $underline-thickness; 
     background-position: 0 1.3em; 
    } 
    } 
} 

這裏是如何使用的mixin:

@include underLine($main-color, 2px) 

的這裏是編譯CSS :

blockquote a { 
    color: #C5003E; 
    background: linear-gradient(90deg, #C5003E 66%, transparent 0) repeat-x; 
    background-size: 0.2em 2px; 
    background-position: 0 1.3em; 
} 

blockquote a:hover { 
    background: linear-gradient(#C5003E, #C5003E) no-repeat; 
    background-size: 100% 2px; 
    background-position: 0 1.3em; 
} 
+0

你甚至沒有一個實際的問題在這裏,沒有必要這個問題或你的答案甚至存在。 – cimmanon

+0

最初我不知道它是否可以用一個全局變量作爲Sass參數mixin。 正如你告訴我,我試了一下,發現你可以,所以我回答我自己的問題之前,我找到了解決方案 – IgnaciodeNuevo

+0

但你沒有一個實際的問題。你寫了一些代碼並問「這是否工作?」如果你在問你的問題之前曾經考慮過甚至*測試它,那麼你會自己發現它的。 – cimmanon

相關問題