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

Adding two values in Sass function creates double instance of 'px'

I have a custom function in Sass that takes an em value, strips the unit and creates a px value:

@function stripAndPx($in){
  $stripped: $in / ($in * 0 + 1); //strip unit
  @return #{ (($stripped) * 16) + 'px'}; //convert to px
}

I am then attempting to take an em variable:

$em-variable: 1em;

And add it to a pixel-based variable:

$px-variable: 20px;

Example:

right: $px-variable + (stripAndPx($em-variable));

Which creates this output:

right: 20px16px;

I am expecting both values to be added:

right: 36px;

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Your function is returning a string, not a number. Sass assumes that you want to concatenate them because that's all it can do with a string and any other type.

@function stripAndPx($in){
  $stripped: $in / ($in * 0 + 1); //strip unit
  @return (($stripped) * 16) * 1px; //convert to px
}

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

...