代码之家  ›  专栏  ›  技术社区  ›  Apple Orange

我怎样才能把zindex的字体设置成角5

  •  1
  • Apple Orange  · 技术社区  · 6 年前

    我正在试着设置 zindex 对于使用typescript的类。但它不起作用。如果你有什么想法,请和我分享。

     ngOnInit() { (<HTMLElement>document.querySelector('.pagemode')).style.z-index = 1;}
    
    3 回复  |  直到 5 年前
        1
  •  4
  •   Gurpreet Singh    6 年前

    你可以用 NgStyle set zindex指令

    [ngStyle]="{'z-index':condition ? 9 : 0 }"
    
        2
  •  2
  •   Drenai    5 年前

    您最好使用内联样式绑定,而不是使用 querySelector -视情况而定

    <p [style.z-index]="1">
      Some text
    </p>
    
        3
  •  -1
  •   RajeshKdev    6 年前

    你可以用 NgClass 指示而不是操纵 DOM .

    在CSS/SCSS文件中:

    .class1 {
      z-index: -1;
    }
    
    .class2 {
       z-index: 1;
    }
    

    在TS文件中:

    YourBooleanVariableNameHere : boolean;
    ngOnInit() {
        this.YourBooleanVariableNameHere = true;
    }
    

    在HTML文件中:

    如果满足您的条件:

    <some-element [ngClass]="{'class1 class2' : YourBooleanVariableNameHere }">...</some-element>
    

    如果您的条件不满足:请注意 ! 带变量的符号

    <some-element [ngClass]="{'class1' : !YourBooleanVariableNameHere }">...</some-element>
    

    Usage Notes 更多示例。参考 here 更多方法。

    或者你也可以尝试同样的方法 , 不建议这样做!

    更新:

    ngOnInit() {
        document.getElementsByClassName("pagemode")[0].style.zIndex = 1;
    }
    

    Working example available here !