简体   繁体   English

Angular 根据值切换不透明度

[英]Angular switch opacity based on value

So I have these two flags:所以我有这两个标志:

在此处输入图像描述

<div class="flag flag-srb" id="2" (click)="changeLanguage(2)">
    <img src="assets/flags/srb.png" alt="srb" height="64" width="64">
</div>
<div class="flag flag-eng" id="1" (click)="changeLanguage(1)">
    <img src="assets/flags/eng.png" alt="eng" height="64" width="64">
</div>

By default the opacity is 0.5 in both of these.默认情况下,这两者的不透明度都是 0.5。 The css: css:

.flag {
    display: inline;
    padding-right: 10px;
    opacity: 0.5;
}

Now I want to change the opacity to 1 depeding on the one the user has selected.现在我想将不透明度更改为 1 取决于用户选择的那个。 Then when he switches to a different flag (clicks on a different flag) the opacity changes to 1 for the one he selected and reverts to 0.5 the other one.然后,当他切换到不同的标志(单击不同的标志)时,他选择的一个的不透明度变为 1,另一个恢复为 0.5。

When he clicks the flag he activates this function:当他点击标志时,他激活了这个 function:

 changeLanguage(id){
    this.sightService.changeLanguageId(id);
    this.sightService.getSights().subscribe(sights => {
      this.sights = sights;
    });

    console.log("this.sights is now: ", this.sights);
    console.log("id is",id);

    var testNula = document.getElementById(id); //GET PICTURE WITH ID
    var testDva = document.getElementById(id).id; //GET PICTURE ID, THE NUMBER

    if(testDva == this.sights[0].pin_lang_id) { //WHAT I TRIED
      console.log("THIS ONE IS SELECTED", testDva);
      testNula.style.opacity = "1";
    } else if (testDva != this.sights[0].pin_lang_id) {
      testNula.style.opacity = "0.5";
    }
  }

Note that this.sights[0].pin_lang_id is loaded with a service.注意this.sights[0].pin_lang_id加载了一个服务。 So that changes when he clicks the flag.因此,当他单击标志时,情况会发生变化。 I wanted to change the opacity using this.我想用这个来改变不透明度。

Also, when the page loads the this.sights[0].pin_lang_id is 2, so the first flag should be opacity 1, the other 0.5.另外,当页面加载时this.sights[0].pin_lang_id是 2,所以第一个标志应该是 opacity 1,另一个是 0.5。 When he clicks the English flag, the first flag should be 0.5 while the English 1.当他点击英文标志时,第一个标志应该是 0.5 而英文 1。

in the.ts create a new variable selectedLang in the changeLanguageId update the variable based on the index在.ts中新建一个变量selectedLang ,在changeLanguageId中根据索引更新变量

 changeLanguage(id){
    this.sightService.changeLanguageId(id);
    this.sightService.getSights().subscribe(sights => {
      this.sights = sights;
    });
    this.selectedLang = id;
  }
<div class="flag flag-srb" id="2" [style.opacity]="{{this.selectedLang === 2 ? 1 : 0.5}}" (click)="changeLanguage(2)">
    <img src="assets/flags/srb.png" alt="srb" height="64" width="64">
</div>
<div class="flag flag-eng" id="1" [style.opacity]="{{this.selectedLang === 1 ? 1 : 0.5}}" (click)="changeLanguage(1)">
    <img src="assets/flags/eng.png" alt="eng" height="64" width="64">
</div>

You can set a variable as selected flag index您可以将变量设置为选定的标志索引

var selectedflag 

index;指数;

Then as per the service gives you the index set the flags value然后根据服务为您提供索引设置标志值

this.selectflagindex =this.sights[0].pin_lang_id;

Then in the template compare the selected index and set the opacity to 1 but by default set it to 0.5 for all然后在模板中比较选定的索引并将不透明度设置为 1,但默认情况下将其设置为 0.5

You div goes like this你的 div 是这样的

<div [ngStyle]="{'opacity':check selected(id)}" </div>


checkselected(id){ return this.selectedFlag == id ? '1' :'0.5'}

By sending appropriate id the value of opacity will be set for the div.通过发送适当的 id,将为 div 设置不透明度值。

This will do for any amount of language not only for two这将适用于任何数量的语言,而不仅仅是两个

Use [class]:使用[类]:

<div class="flag flag-srb" id="2" [class.fullOpacity]="fullOpacity==2" (click)="changeLanguage(2); fullOpacity="2">
    <img src="assets/flags/srb.png" alt="srb" height="64" width="64">
</div>
<div class="flag flag-eng" id="1" [class.fullOpacity]="fullOpacity==1"(click)="changeLanguage(1);[class.fullOpacity]="fullOpacity==1">
    <img src="assets/flags/eng.png" alt="eng" height="64" width="64">
</div>

.full-opacity{
    opacity:1 // you may need to set !important
}

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

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