小程序星评根据成绩来高亮显示

微信   2025-02-08 11:40   61   0  

13230_rkm7_9549.png

11059_fqdh_5653.png

11058_owbo_2121.png

index.wxml

<view class="star-container">
  <block wx:for="{{stars}}" wx:key="index">
    <image class="star" src="{{item ? '/images/stars_select.png' : '/images/stars_unselect.png'}}"></image>
  </block>
</view>


index.wxss

.star-container {
  display: flex;
}

.star {
  width: 40px; /* 根据实际情况调整 */
  height: 40px; /* 根据实际情况调整 */
}


index.js

Page({
  data: {
    stars: [false, false, false, false, false, false], // 默认都是灰色星星
    score: 50 // 假设这是你的成绩变量
  },

  onLoad: function(options) {
    this.calculateStars();
  },

  calculateStars: function() {
    let score = this.data.score;
    let starCount = Math.round(score / (100 / 6)); // 假设满分是100分,根据实际需求调整
    let stars = Array(6).fill(false).map((item, index) => index < starCount);

    this.setData({ stars });
  },

  setScore: function(newScore) {
    this.setData({ score: newScore }, () => {
      this.calculateStars();
    });
  }
});


博客评论
还没有人评论,赶紧抢个沙发~
发表评论
说明:请文明发言,共建和谐网络,您的个人信息不会被公开显示。