我有一个Angular 项目,其组件如下所示:

            <div class="stars-container mt-2">
                <div *ngFor="let star of starsArray; let i = index" class="star">
                    <svg xmlns="http://www.w3.org/2000/svg" width="128" height="128" viewBox="0 0 24 24">
                        <path fill="#ffffff"
                            d="m7.69 19.346l1.614-5.33L5.115 11h5.216L12 5.462L13.67 11h5.215l-4.189 3.015l1.614 5.331L12 16.07z" />
                    </svg>
                </div>
            </div>

其中startsArray为:

starsArray: boolean[] = new Array(5);

公务员叙用计划是:

.stars-container {
    display: flex;
    justify-content: left;
    align-items: center;
    gap: .7rem;
    cursor: pointer;
    flex-direction: row-reverse;

    .star {
        transition: .3s ease;

        svg {
            width: 3rem;
            height: auto;

            path {
                fill: $gris; // Color de las estrellas no seleccionadas
                transition: fill .3s ease; // Transición suave del color
            }
        }

        &:hover~.star svg path {
            fill: $primario; // Color de las estrellas seleccionadas al hacer hover
        }

        &:hover svg path {
            fill: $primario; // Color de la estrella seleccionada al hacer hover
        }
    }
}

.star:hover svg path,
.star:hover~.star svg path,
.star.highlighted svg path {
    fill: $primario;
}

此时此刻,我已经实现了当我在一颗星星上悬停时,左边的星星也会改变 colored颜色 .尽管如此,我希望当我按下一个星号时,左侧的所有星号和被点击的星号都会永久地改变 colored颜色 (在浏览器会话中永久改变,在数据库中不会).

我试了几种方法,但都不管用.

推荐答案

  • 我们可以使用ngClass,并根据用户的 Select 添加一个类selected
  • 我们可以使用@Input@Output来更新名为rating的属性中用户的选定值
  • 我们可以使用emits 5 - then latest value的逻辑,因为我们显示的是恒星的反转.

请查看下面的工作堆栈闪电战和代码

import { CommonModule } from '@angular/common';
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';

@Component({
  selector: 'app-star-rating',
  standalone: true,
  template: `
    <div class="stars-container mt-2">
        <div *ngFor="let star of starsArray; let i = index" class="star" (click)="selectRating(i)" [ngClass]="{'selected': 5 - rating <= i}">
            <svg xmlns="http://www.w3.org/2000/svg" width="128" height="128" viewBox="0 0 24 24">
                <path fill="#00000"
                    d="m7.69 19.346l1.614-5.33L5.115 11h5.216L12 5.462L13.67 11h5.215l-4.189 3.015l1.614 5.331L12 16.07z" />
            </svg>
        </div>
    </div>
  `,
  styles: `
  .stars-container {
    display: flex;
    justify-content: left;
    align-items: center;
    gap: .7rem;
    cursor: pointer;
    flex-direction: row-reverse;

    .star {
        transition: .3s ease;

        svg {
            width: 3rem;
            height: auto;

            path {
                fill: gray; // Color de las estrellas no seleccionadas
                transition: fill .3s ease; // Transición suave del color
            }
        }

        &:hover ~ .star svg path {
            fill: gold; // Color de las estrellas seleccionadas al hacer hover
        }

        &:hover svg path {
            fill: gold; // Color de la estrella seleccionada al hacer hover
        }

        &.selected ~ .star svg path {
            fill: gold; // Color de las estrellas seleccionadas al hacer hover
        }
 
        &selected svg path {
            fill: gold; // Color de la estrella seleccionada al hacer hover
        }
    }
}

.star:hover svg path,
.star:hover~.star svg path,
.star.highlighted svg path {
    fill: $primario;
}
  `,
  imports: [CommonModule],
})
export class StarRatingComponent implements OnInit {
  @Input() rating: number = 0;
  @Output() ratingChange: EventEmitter<number> = new EventEmitter<number>();
  starsArray: boolean[] = new Array(5);

  selectRating(i: number) {
    this.ratingChange.emit(5 - i);
  }
  constructor() {}

  ngOnInit() {}
}

Stackblitz Demo

Html相关问答推荐

如何将值从Google Sheets侧边栏中的表单中的输入传递到Google Sheets单元格中使用Textile()

在CSS中不保持圆形图像形状的边框半径属性

选中/取消选中带有_hyperscript的多个复选框

Div内容防止同级大小增加

悬停效果在新西兰表上不起作用

assets资源 净值元素不会扩展到涵盖子项

尽管div高度为100%,div中的内容仍会溢出

我无法动态嵌入Instagram帖子,因为无法分配Instagram固定链接

禁用的文本区域会丢失换行符

浏览器是否可以呈现存储在代码点0x09处的字形?

如何解决水平塌陷问题?

如何在Go模板中传入途中创建的 map

如何根据剪辑路径边缘设置文本边距?

视频添加到 Html 与 Github 不同步

XPATH Select h2标签和

标签之间或h2标签和 标签之间的td元素,以立即为准

如何截断一些文本并用双引号引起来?

透明渐变凹矩形

如何制作一个空的网格模板行来填充剩余空间?

那边界从何而来?

使用 CSS flexbox 和溢出顶部而不是底部