Ionic 使用Angular 显示或隐藏元素的逻辑

mpbci0fu  于 2022-12-09  发布在  Ionic
关注(0)|答案(2)|浏览(163)

所以我在显示或隐藏元素时遇到了一些逻辑问题。
我有这个功能来显示或隐藏“span”元素,通过它我可以在每个文本上显示不同的颜色。

startWritting() {
// const spanOne = <HTMLInputElement>document.getElementById('one');
// const spanTwo = <HTMLInputElement>document.getElementById('two');
// const spanThree = <HTMLInputElement>document.getElementById('three');
// const spanFour = <HTMLInputElement>document.getElementById('four');

const durCharFwd = 0.1; // character typed
const durFullGap = 2000; // time between typed/delete
const durCharBwd = 0.8; // character deleted
const durDoneGap = 1000;
let strings = ['Your value', 'Organic', 'Gluteen-free', 'Fair-trade'];
let durTotal;
setInterval(() => {
  strings.forEach((string, i) => {
    durTotal =
      string.length * (durCharFwd + durCharBwd) + durFullGap + durDoneGap;
    setInterval(() => {
      this.elements.forEach((el, index) => {
        if (index === i) {
          el.isSelected = true;
        } else {
          el.isSelected = false;
        }
      });
    }, durTotal);
  });
}, durTotal);

}
这是我的HTML代码

<ng-container *ngFor="let el of elements">
        <span
          *ngIf="el.isSelected"
          [id]="el.id"
          class="animated-text"
        ></span>
      </ng-container>

顺便说一下,这是编辑元素的打字动画,因为你可以看到完整的html和css动画代码在这里https://codepen.io/matadantelinga/pen/ExQeqjj我想知道,如果有人在这里可以建议我的逻辑?

8xiog9wr

8xiog9wr1#

这样有用吗?
https://stackblitz.com/edit/angular-ivy-cr9lq4?file=src%2Fapp%2Fapp.component.html,src%2Fapp%2Fapp.component.ts,src%2Fapp%2Fapp.component.css

export class AppComponent {
  name = 'Angular ' + VERSION.major;

  text = '';
  color = '';

  texts = ['One', 'Two', 'Three', 'Four'];
  colors = ['cyan', 'lightgreen', 'salmon', 'coral'];

  constructor(private cdRef: ChangeDetectorRef) {}

  ngOnInit() {
    timer(0, 500)
      .pipe(map((i) => i % 4))
      .subscribe((i) => {
        this.text = this.texts[i];
        this.color = this.colors[i];
        this.cdRef.detectChanges();
      });
  }
}

EDIT将打字机添加到堆栈 lightning 战

https://stackblitz.com/edit/angular-ivy-cr9lq4?file=src%2Fapp%2Fapp.component.html,src%2Fapp%2Fapp.component.ts,src%2Fapp%2Fapp.component.css

kqlmhetl

kqlmhetl2#

只声明一个数组颜色

colors=['red','blue','yellow','green']

只需添加[style.color]="colors[i%colors.length]"

<ng-container *ngFor="let el of elements;let i=index">
        <span *ngIf="el.isSelected"
          [style.color]="colors[i%colors.length]"
          [id]="el.id"
          class="animated-text"
        ></span>
</ng-container>

相关问题