题
After discovering that Sass doesn't have exponential abilities, I decided to make my own pow() function. This is my unsuccessful attempt:
@function pow($x,$p) {
$u: unit($x);
$x: $x / 1#{$u};
$p: round($p);
@if $p == 0 {
@return 0;
} @else if $p == 1 {
@return $x;
} @else {
@for $i from 1 through abs($p) {
$x: $x * $x;
}
}
@if $p < 0 {
$x: 1 / $x;
}
@return $x + $u;
}
The part I'm stuck with is stripping off units. I know that 12px / 1px = 12
, but in my case the unit is unknown, and 12px / 1#{unit(12px)}
doesn't work because it equals to "12px/1px"
(yes, with quotes). The unquote
function doesn't seem to work for me, I don't know why.
In case you're wondering, the reason why I want to strip units (and return them in the end) is because 12px * 12px = 144px*px
.
解决方案
Instead writing pow
in Sass use Ruby http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html
不隶属于 StackOverflow