Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

Multiple values for one property with variable argument lists in Sass

I'm looking to have a mixin like +stacktextshadow(blue, red, green) spit out text-shadow: 1px 1px 0 blue, 2px 2px 0 red, 3px 3px 0 green;

Currently this is what I have:

=stacktextshadow($shadows...)
  @for $i from 1 through length($shadows)
    $shadow1: append(1px 1px 0, nth($shadows,1))
    $shadow2: append(2px 2px 0, nth($shadows,2))
    $shadow3: append(3px 3px 0, nth($shadows,3))
    text-shadow: $shadow1, $shadow2, $shadow3

h1
  +stacktextshadow(blue, red, green)

Which gives me:

h1 {
  text-shadow: 1px 1px 0 blue, 2px 2px 0 red, 3px 3px 0 green;
  text-shadow: 1px 1px 0 blue, 2px 2px 0 red, 3px 3px 0 green;
  text-shadow: 1px 1px 0 blue, 2px 2px 0 red, 3px 3px 0 green; }

Tripled. And I know why, because it's running the text-shadow property declaration three times in the @for loop. I'd like it to only do that once. However, when I take the text-shadow out of the foor loop, it doesn't have access to $shadow1, $shadow2, etc.

Also, I'd like to not repeat myself with something along the lines of: $shadow($i): append($i*1px $i*1px 0, nth($shadows,$i)) (which obviously doesn't work) so that it is all done dynamically—whether I pass one argument into the mixin, or 20.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You can create a variable outside the loop to "collect" the shadow values, and then use that variable in your text-shadow property. Example:

=stacktextshadow($shadows...)
  $all: ()
  @for $i from 1 through length($shadows)
    $all: append($all, append($i*1px $i*1px 0, nth($shadows, $i)), comma)

  text-shadow: $all

h1
  +stacktextshadow(blue, red, green)

Output:

h1 {
  text-shadow: 1px 1px 0 blue, 2px 2px 0 red, 3px 3px 0 green; }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...