CSS   发布时间:2022-04-17  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了css – 通过SASS循环嵌套选择器大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这个 HTML
<ul> // first level
  <li>
    <ul></ul> // second level
  </li>
  <li>
    <ul> // second level
      <li>
        <ul></ul> // third level
      </li>
    </ul>
  </li>
</ul>

有没有可能将嵌套列表相互循环,得到这样的东西:

ul {
 padding: 10px;
 ul {
  padding: 20px;
  ul {
    padding: 30px;
    ... through 10 * ul
  }
 }
}

有了这个我只有列表乘法.

@for $i from 1 through 10 {
    ul {
        padding-left: 100 - (10 * $i) + px;
    }
  }

解决方法

对于3.4之前的Sass版本,您需要执行此操作的功能不是本机Sass的一部分(并且递归mixin在某些版本中不起作用).你需要名为nest()的Compass函数.
$sel: '';
@for $i from 1 through 10 {
    // remove !global flag if you're using Sass < 3.3
    $sel: if($i == 1,"ul",nest($sel,"ul")) !global;

    #{$sel} {
        padding-left: 10px * $i;
    }
}

Sass 3.4有一个名为SELEctor-nest()的函数的原生版本:

$sel: '';
@for $i from 1 through 10 {
    $sel: if($i == 1,SELEctor-nest($sel,"ul")) !global;

    #{$sel} {
        padding-left: 10px * $i;
    }
}

输出:

ul {
  padding-left: 10px;
}

ul ul {
  padding-left: 20px;
}

ul ul ul {
  padding-left: 30px;
}

ul ul ul ul {
  padding-left: 40px;
}

ul ul ul ul ul {
  padding-left: 50px;
}

ul ul ul ul ul ul {
  padding-left: 60px;
}

ul ul ul ul ul ul ul {
  padding-left: 70px;
}

ul ul ul ul ul ul ul ul {
  padding-left: 80px;
}

ul ul ul ul ul ul ul ul ul {
  padding-left: 90px;
}

ul ul ul ul ul ul ul ul ul ul {
  padding-left: 100px;
}

如果你想拼出最初的ul,那么你会这样做:

ul {
    // parent styles
    border: 1px solid;

    $sel: '';
    @for $i from 2 through 10 {
        $sel: if($i == 2,'ul','ul')); // note no !global flag

        #{$sel} {
            padding-left: 10px * $i;
        }
    }
}

请注意,您可以使用简单的字符串连接而不是这些函数中的任何一个,但它仅适用于您只嵌套单个选择器的情况.如果您有2个或更多选择器,这是绝对必要的:

$sel: '';
@for $i from 1 through 3 {
    $sel: if($i == 1,"article,section",section")) !global;

    #{$sel} {
      h1 {
        padding-left: 10px * $i;
      }
    }
}

输出:

article h1,section h1 {
  padding-left: 10px;
}

article article h1,article section h1,section article h1,section section h1 {
  padding-left: 20px;
}

article article article h1,article article section h1,article section article h1,article section section h1,section article article h1,section article section h1,section section article h1,section section section h1 {
  padding-left: 30px;
}

大佬总结

以上是大佬教程为你收集整理的css – 通过SASS循环嵌套选择器全部内容,希望文章能够帮你解决css – 通过SASS循环嵌套选择器所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。