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
604 views
in Technique[技术] by (71.8m points)

css - SASS for loop updating hsla lightness returns error $lightness: "96.77419" is not a number for `hsla'

I'm trying to loop a set amount of times gradually decreasing the lightness value of hsla but when I run the loop I get an error $lightness: "96.77419" is not a number forhsla'`. Can anyone advise me where I'm going wrong with this or how it can be improved?

Code

$iterations: 31;
$base: 100;
$math: $base / $iterations;

li {
  background: #919190;
  height: 40px;
  line-height: 40px;
  color: #191919;
  text-align: center;
}

@for $i from 1 through $iterations {
 .options:nth-of-type(#{$i}) {
    background: hsla(60, 1, #{($base - $math)}, 1);
}

Codepen http://codepen.io/styler/pen/BHwjc

Sassmeister http://sassmeister.com/gist/e99733697e1b38b794fa

What I really want to do is be able to gradually increase the colour to make a shade palette, really want to be able to use this multiple times with multiple different amounts etc so it would be great if you could give me some additional advice to make this.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Sass gave you the answer: you're using strings when you shouldn't be (note the quotations in the error, that's a sure sign of a string). Interpolation gives you a string all the time no matter what. Because hsla() expects all arguments to be numbers, passing it a string results in getting the string hsla() instead of the Sass color representation for hsla(), and the lighten() function can only accept colors.

So just stop giving it a string:

.foo {
    background: hsla(60, 1, ($base - $math), 1);
}

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

...