简体   繁体   English

:: before和:: after上SCSS中的动态类名称

[英]Dynamic Class names in SCSS on an ::before and ::after

I'm trying to make dynamic classes for my webpage but can't seem to get it to work. 我正在尝试为我的网页创建动态类,但似乎无法使其正常工作。

What I am trying to do is make it so an ::before and ::after 's colours will change depending on the class it's assigned to the parent 'picture' class. 我想做的是使:: before和:: after的颜色根据分配给父“图片”类的类而改变。

I have highlighted in my SCSS code below which lines need to be dynamic. 我已在我的SCSS代码中突出显示了哪些行需要动态显示。

.picture {
      display: inline-block;
      height: 130px;
      width: 130px;
      margin-bottom: 50px;
      z-index: 1;
      position: relative;
      &:before {
        content: "";
        width: 100%;
        height: 0;
        border-radius: 50%;
        background-color: #1369ce; //COLOUR HAS TO BE DYNAMIC
        position: absolute;
        bottom: 135%;
        right: 0;
        left: 0;
        opacity: 0.9;
        transform: scale(3);
        transition: all 0.3s linear 0s;
      }
      &:after {
        content: "";
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #1369ce; //COLOUR HAS TO BE DYNAMIC
        position: absolute;
        top: 0;
        left: 0;
        z-index: -1;
      }
}

For the colours in the ::before and ::after to be dynamic. 为了使:: before和:: after中的颜色具有动态性。

::before and ::after 's colours will change depending on the class it's assigned to the parent 'picture' class. :: before和:: after的颜色将根据分配给父“图片”类的类的不同而变化。

I guess your classes and colors will be defined already, the colors are dynamic but not infinite or uncertain. 我想您的类和颜色已经定义好了,颜色是动态的,但不是无限或不确定的。

In your Sass code define classes as below 在您的Sass代码中,定义以下类

.some-dynamic-class{
  .picture{
    &:before,
    &:after{
      background-color:#some-color;
    }
  }
}

And then in your Angular code implement your dynamic class logic, preferably by an input; 然后,在Angular代码中,最好通过输入来实现动态类逻辑。

@Input() dynamicClass:string;

And then in your template use ngClass on the parent element 然后在模板中对父元素使用ngClass

<parent-element [ngClass]="dynamicClass">
    <div class="picture">
    </div>
</parent-element>

EDIT: Then in your case, from your comment, the sass code should be like this. 编辑:然后在您的情况下,从您的注释中,sass代码应如下所示。

$rankColors: trainee #00AA00, traineebuild #00AA00, mod #55FF55, builder #55FF55, srmod #FFFF55, architect #FFFF55, developer #FFAA00, admin #FF5555, manager #AA0000;
@each $i in $rankColors {
  .#{nth($i, 1)} {
    .picture{
      &:before,
      &:after{
        background-color:nth($i, 2);
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM