如何将循环中途反转?

时间:2014-07-28 13:15:02

标签: css loops for-loop iteration stylus

我有这段代码:

for i in (1..5) 
 div:nth-child({i})::after
  content \'i*i\'

for i in (6..10) 
 div:nth-child({i})::after
  ij = (i - 5)
  content \'(ij*ij)\'

for i in (1..10) 
  div:nth-child({i})
  ij = (i - 5)
  if i >= 6
   height ij * ij px
  else 
   height i * i px

哪个输出:

enter image description here

但是我需要它在25时反转,所以它会:

1 4 9 16 25 25 16 9 4 1

不确定怎么做?

2 个答案:

答案 0 :(得分:1)

更改计算ij的方式,从 ij =(i - 5) 改为 ij = 11 - i

for i in (1..5) 
 div:nth-child({i})::after
  content \'i*i\'

for i in (6..10) 
 div:nth-child({i})::after
  ij = 11-i
  content \'(ij*ij)\'

for i in (1..10) 
  div:nth-child({i})
  ij = 11-i
  if i >= 6
   height ij * ij px
  else 
   height i * i px

答案 1 :(得分:0)

好吧,我会给你一种方法来反转一个循环,但是你需要在你的代码中将它集成到你自己的代码中 :

    var irt=1;
    for(i=0;(i<10 && irt>0) || (i>0 && irt<0) ;i+=irt){
    if(i==5){//this is where it well bi reversed
    irt=-1;
    }
//do whatever you want to do here
    }