【问题标题】:Angular: star rating角度:星级
【发布时间】:2019-08-01 05:47:28
【问题描述】:

我想实施评级星系统。单击后返回正确的星值。我在正确显示所选星星时遇到问题。就像在这张照片中一样,它没有向我显示黄色的 3 颗星。如何改变它? 组件:

 export class RatingStarComponent implements OnInit {
  stars: number[] = [1, 2, 3, 4, 5];
  constructor() { }
  ngOnInit() {
  }
  countStar(star) {
    console.log('Value of star', star);
  }
}

HTML:

 <div class="row">
  <div class="col-sm-12">
      <ul class="list-inline rating-list" *ngFor="let star of stars" style="display: inline-block" >
          <li (click)="countStar(star)"><i   class="fa fa-star "></i></li> 
      </ul>
  </div>
</div>

CSS:

  .rating-list li {
  float: right;
  color: #ddd;
  padding: 10px 5px;

}

.rating-list li:hover,
.rating-list li:hover ~ li {
  color: #ffd700;
}

.rating-list {
  display: inline-block;
  list-style: none;
}

欢迎任何帮助或建议。

【问题讨论】:

标签: html css angular


【解决方案1】:

我尝试使用材质图标。它正在工作。

HTML

<ul class="list-inline rating-list" *ngFor="let star of stars; let i= index">
    <li (click)="countStar(star)" [ngClass]="{'selected': (star <= selectedValue)}" (mouseover)="addClass(star)"
         (mouseout)="removeClass()"> <span class="material-icons">star</span>
    </li>
</ul>

ts

stars: number[] = [1, 2, 3, 4, 5];
selectedValue: number = 0;
isMouseover = true;

countStar(star: number) {
  this.isMouseover = false;
  this.selectedValue = star;
}

 addClass(star: number) {
  if (this.isMouseover) {
    this.selectedValue = star;
  }
 }

 removeClass() {
   if (this.isMouseover) {
      this.selectedValue = 0;
   }
 }

CSS

.rating-list {
  display: inline-block;
  list-style: none;
}
.rating-list li {
  font-size: 20px;
  float: right;
  color: #ddd;
  padding: 10px 5px;
}
.rating-list li:hover {
  color: #ffd700;
}
.rating-list li:hover ~ li {
  color: #ffd700;
}
.rating-list li.selected {
  color: #ffd700;
}

【讨论】:

    【解决方案2】:

    改进版

    在这个解决方案中,即使在悬停时,所有以前的星星的颜色都会变成黄色:

    HTML

    <div class="row">
      <div class="col-sm-12">
          <ul class="list-inline rating-list" *ngFor="let star of stars; let i= index" style="display: inline-block"> 
              <li (click)="countStar(star)" id="{{'starId'+i}}" [ngClass]="{'selected': (star <= selectedValue)}" 
               (mouseover)="addClass(star)" (mouseout)="removeClass(star)"> ★
              </li>
          </ul>
      </div>
    </div>
    
    

    Component.ts

    stars: number[] = [1, 2, 3, 4, 5];
    selectedValue: number = 0;
    
    countStar(star) {
        this.selectedValue = star;
      }
    
    addClass(star) {
       let ab = "";
       for (let i = 0; i < star; i++) {
         ab = "starId" + i;
         document.getElementById(ab).classList.add("selected");
       }
    }
    removeClass(star) {
       let ab = "";
       for (let i = star-1; i >= this.selectedValue; i--) {
         ab = "starId" + i;
         document.getElementById(ab).classList.remove("selected");
       }
    }
    
    
    

    SCSS

    .rating-list {
        li {
        font-size: 20px;
            float: right;
            color: #ddd;
            padding: 10px 5px;
            &:hover {
                color: #ffd700;
                ~ {
                    li {
                        color: #ffd700;
                    }
                }
            }
        }
        li.selected {
            color: #ffd700;
        }
        display: inline-block;
        list-style: none;
    }
    
    

    详情请见this example

    【讨论】:

      【解决方案3】:

      你必须存储选择的值:

      countStar(star) {
          this.selectedValue = star;
          console.log('Value of star', star);
      }
      

      并添加类,该类将更改所有评级小于或等于评级的星星的颜色:

      .rating-list li.selected {
          color: #ffd700;
      }
      
      <li (click)="countStar(star)"
          [ngClass]="{'selected': (star <= selectedValue)}">
              <i class="fa fa-star"></i>
      </li> 
      

      详情请见full example

      【讨论】:

      • 为我节省了数小时的工作量,这很容易使用纯 Angular 实现,无需使用任何外部 css javascript 框架。
      猜你喜欢
      • 1970-01-01
      • 2019-04-14
      • 2023-03-11
      • 2015-01-14
      • 2017-04-06
      • 1970-01-01
      • 1970-01-01
      • 2016-06-21
      • 2016-10-26
      相关资源
      最近更新 更多