4Manuals

  • PDF Cloud HOME

在SASS中使用类数组 Download

    CSS / JS-如何覆盖一个孩子而不是其他孩子的父母过渡 CSS在像素中设置多个颜色渐变? 创建一个没有固定布局的HTML表,设置最大宽度,并且行不换行但溢出:隐藏 IntersectionObserver观察视口中的document.body上边缘 使JPG图像适合SVG图像 如何在JS中强制重绘以进行CSS过渡? 在自定义类Bootstap 4中的Sass中使用margin和padding变量 单击多选选项时,删除选项的蓝色 如何使用JavaScript将child.jsp包含为模式或叠加层? jQuery中是否存在存在/加载的任何元素?

使用SASS / SCSS。我有以下内容:

body:not(.sessions):not(.registrations):not(.home):not(.posts.new) {
  padding-top: 4.5rem;
  padding-bottom: 7rem;
}

这根本不可读。如何通过使用类数组来重构它:

$full-page-classes: .sessions .registrations .home .posts.new

1 个答案:

答案 0 :(得分:1)

如果我理解您的意思是正确的,则可能要像这样使用 array :

$full-page-classes: '.sessions', '.registrations', '.home', '.posts.new';

@each $page-class in $full-page-classes {
  body:not(#{$page-class}) {
    padding-top: 4.5rem;
    padding-bottom: 7rem;
  }
}

输出:

body:not(.sessions) {
  padding-top: 4.5rem;
  padding-bottom: 7rem;
}

body:not(.registrations) {
  padding-top: 4.5rem;
  padding-bottom: 7rem;
}

body:not(.home) {
  padding-top: 4.5rem;
  padding-bottom: 7rem;
}

body:not(.posts.new) {
  padding-top: 4.5rem;
  padding-bottom: 7rem;
}

您可以在sassmeister

上尝试

更新:

如果要生成内联 CSS,可以尝试以下操作:

$full-page-classes: '.sessions', '.registrations', '.home', '.posts.new';

$temp: '';

@each $page-class in $full-page-classes {
  $temp: $temp + ':not(#{$page-class})';
}

@each $item in $temp {
  body#{$item} {
    padding-top: 4.5rem;
    padding-bottom: 7rem;
  }
}

输出:

body:not(.sessions):not(.registrations):not(.home):not(.posts.new) {
  padding-top: 4.5rem;
  padding-bottom: 7rem;
}

更新2:

@function str-replace($string, $search, $replace: '') {
  $index: str-index($string, $search);

  @if $index {
    @return str-slice($string, 1, $index - 1) + $replace + str-replace(str-slice($string, $index + str-length($search)), $search, $replace);
  }

  @return $string;
}

$full-page-classes: ' .sessions, .registrations, .home, .posts.new,';

@each $x in str-replace(str-replace($full-page-classes, ' ', ':not('), ',', ')') {
  body#{$x} {
    padding-top: 4.5rem;
    padding-bottom: 7rem;
  }
}

参考:str-replace function



Similar searches
    如何使用OpenIddict使刷新和注销刷新和访问令牌无效(使用内存数据库) 未定义结果foreach数组javascript 无法在额外的属性扩展上获取属性'mavenRepo',因为它在React Native Jitsi Meet中不存在 Samsung SRS582DTKGM Refrigerator User Manual 如何在Godadday上使用托管网址解决bot框架模拟器中的404发布错误?