Sass loop incrementing by 100 - css

I want to write a for loop which increments with 100 each time ($i), and increments another variable by 50 each time ($j).
I tried to follow the examples in other questions about sass incremental loops, but I can't seem to be able to figure out how to apply to my case specifically.
$max: 2600;
$step: 100;
$j: 150;
#for $i from 1000 through $max {
$i: $i + $step;
$j: $j + 50;
// stuff
#media screen and (min-height: #{$i}px) {
.item {
transform:
translateY(calc(-100% - #{$j}px));
}
}
}
this is the current CSS output:
#media screen and (min-height: 1100px) {
.item {
transform: translateY(calc(-100% - 200px)); } }
#media screen and (min-height: 1101px) {
.item {
transform: translateY(calc(-100% - 250px)); } }
#media screen and (min-height: 1102px) {
.item {
transform: translateY(calc(-100% - 300px)); } }
// ..etc
the desired CSS output:
#media screen and (min-height: 1000px) { // $i is 1000
.item {
transform:
translateY(calc(-100% - 150px)); // $j is 150
}
}
#media screen and (min-height: 1100px) { // $i is 1100
.item {
transform:
translateY(calc(-100% - 200px)); // $j is 200
}
}
#media screen and (min-height: 1200px) { // $i is 1200
.item {
transform:
translateY(calc(-100% - 250px)); // $j is 250
}
}
...
// and so on and so forth until min-height is 2600
EDIT
This is the working code, thank you
$max: 26;
$step: 100;
$j: 100;
#for $i from 10 through $max {
$k: $i * $step;
$j: $j + 50;
#media screen and (min-height: #{$k}px) {
.item {
transform:
translateY(calc(-100% - #{$j}px));
}
}
}

$max: 26;
$step: 100;
$j: 150;
#for $i from 1 through $max {
$k: $i * $step;
$j: $j + 50;
// Use $k and $j in the styles instead of $i
Instead of adding $step to $i, you need to multiply * it with the $step. Also, change the variable name from $i to something else since it will override the loop variable $i.
Edit: Just realized you will also need to change your start and end of the loop.
IMPROVED ANSWER:
My old answer works but, I believe it works at the expense of readability. I would suggest you initialize $k with an initial value just like you did with $j and add $step to $k each time independent of $i in each iteration:
$max: 26;
$j: 150;
$k: 100;
$stepK: 100;
$stepJ: 50;
#for $i from 10 through $max {
$k: $k + $stepK;
$j: $j + $stepJ;
// Use $k and $j in the styles instead of $i

Related

How to reverse number in sass

I have a sass class like this:
#for $column from 1 through $columns {
.column-#{$column} {
#extend %set-margin;
flex: 100% / $column;
}
}
and the result like this:
.column-1 {
flex: 100%
}
.column-2 {...}
...
I want reverse the result like:
.column-12 {
flex: 100%
}
.column-11 {...}
...
how to reverse the result ?
You can just switch around the two variables 1 and $columns and divide the 100% by the inverse:
$columns: 4;
#for $column from $columns through 1 {
.column-#{$column} {
#extend %set-margin;
flex: 100% / ($columns - $column + 1);
}
}
Simply like below:
$columns:12;
#for $column from 1 through $columns {
.column-#{$column} {
flex: 100% / ($columns - $column + 1);
}
}

How to remove some value of condition in scss?

I have a condition in scss, but I want to remove my value, how can I remove that value in scss? I put some code in bellow and also with image too
$color-sp: color-sp;
$width: 100%;
#for $i from 1 through 12 {
#for $j from 12 through 1 {
.#{$color-sp}-#{$i} {
width: $j;
}
}
}
I put some link to know my result :
https://www.sassmeister.com/gist/3d049565b0be28429f7d3803baf1bcb1
You can achieve that with an if statement
$color-sp: color-sp;
$width: 100%;
$max: 12;
$min: 1;
$j: $max;
#for $i from $min through $max {
#{$color-sp}-#{$i} {
width: $j;
}
$j : $j - 1;
}

Compile error with SCSS if-else statement

I have made a condition in scss but when I compile, the result cannot appear in css file, I hope anyone can help me to solve my problem, I put code in the bellow :
$color-sp: color-sp;
$width: 100%;
#for $i from 1 through 2 {
#if $color-sp == 1 {
.#{$color-sp}-#{$i} {
width: $width / 1;
}
} #else if $color-sp == 2 {
.#{$color-sp}-#{$i} {
width: $width / 2;
}
}
}
I think you have to put if condition on $i instead of $color-sp. Check below code it will produce two classes .
try it on https://www.sassmeister.com/
$color-sp: color-sp;
$width: 100%;
#for $i from 1 through 2 {
#if $i == 1 {
.#{$color-sp}-#{$i} {
width: $width / 1;
}
} #else if $i == 2 {
.#{$color-sp}-#{$i} {
width: $width / 2;
}
}
}
And more optimized code for creating classes is below:
$color-sp: color-sp;
$width: 100%;
#for $i from 1 through 2 {
.#{$color-sp}-#{$i} {
width: $width / $i;
}
}

My SASS loop is breaking using interpolation and percentage

This code is breaking my SASS process for some reason..
#for $i from 1 through 100 {
.t-#{$i} {
top: #{$i}%;
}
}
You can change to this:
#for $i from 1 through 100 {
.t-#{$i} {
top: 1% * $i
}
}
From SASS docs:
Here is an example
You can also write like this
#for $i from 1 through 100 {
.t-#{$i} {
top: #{$i + '%'};
}
}

SASS for loop to output increments of 15 [duplicate]

This question already has answers here:
Adding a unit to a number in Sass
(2 answers)
Closed 7 years ago.
I am trying to output the following:
.time15 {
width: 25%;
}
.time30 {
width: 50%;
}
.time45 {
width: 75%;
}
.time60 {
width: 100%;
}
.time75 {
width: 125%;
}
All the way up to .time1440
Here is my code:
$max: 60 * 24;
$step: 15;
#for $i from 15 through ceil($max/$step) {
$value: ($i - 1)*$step + 1;
$width: $value / 60 * 100;
.time{$value} {
width: $value%
}
}
I'm getting the following syntax error though when trying to compile:
Error: Invalid CSS after "... &.time{$value": expected ":", was "} {"
The issue is that SCSS can't handle the following line:
width: $value%;
It is safer to do
width: percentage($value);
Apart from that, I think that the interpolation of the selector is also wrong.
In my test I changed it to .time-#{$value} and that worked for me.
Full example:
$max: 60 * 24;
$step: 15;
#for $i from $step through ceil($max/$step) {
$value: ($i - 1) * $step + 1;
$width: $value / 60 * 100;
.time-#{$value} {
width: percentage($value);
}
}
this maybe what you're looking for:
$max: 60 * 24;
$step: 15;
#for $i from 1 through ($max/$step) {
$value: $step * $i;
$width: $i * 25;
.time#{$value} {
width: $width + 0%
}
}

Resources