Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

Skipping an optional argument in Sass mixin

I have this mixin to handle a simple CSS3 linear gradient:

@mixin linear-gradient($from, $to, $dir: bottom, $dir-webkit: top, $ie-filters: false) {
    background-color: $to;
    background-image: -webkit-linear-gradient($dir-webkit, $from, $to);
    background-image: linear-gradient(to $dir, $from, $to);
    @if $ie-filters == true and $old-ie {
         filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#{ie-hex-str($from)}', endColorstr='#{ie-hex-str($to)}');
    }
}

$dir is short for 'direction'.

If I need to make $ie-filters 'true' and I don't need to change the $dir / $dir-webkit default values I still need to redeclare them which obviously isn't very DRY and optimal, so I'd have to do this:

@include linear-gradient(#7a7a7a, #1a1a1a, bottom, top, true);

When I just want to do this:

@include linear-gradient(#7a7a7a, #1a1a1a, true);

How do I skip over arguments in this way when calling a mixin?

PS if you're wondering about the $dir-webkit argument it's for Webkit as it still doesn't handle the new gradient syntax (see: http://generatedcontent.org/post/37949105556/updateyourcss3 -> New gradient syntax), the direction needs to be the opposite of the standard syntax.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Starting from SASS 3.1 you can pass named arguments to do that:

@include linear-gradient($from: #7a7a7a, $to: #1a1a1a, $ie-filters: true);

The rest will be default.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...