小程序6颗灰色星星根据成绩分数来展示所点亮星星 | 熊阿哥博客

小程序6颗灰色星星根据成绩分数来展示所点亮星星

微信   2025-02-08 11:31   62   0  

12727_bz5g_4269.png

index.wxml

<view class="star-rating">
  <block wx:for="{{stars}}" wx:key="index">
    <view class="star {{index < activeStars ? 'active' : ''}}" bindtap="setRating" data-index="{{index}}">
      ★
    </view>
  </block>
</view>


index.wxss

.star-rating {
  display: flex;
}

.star {
  font-size: 40rpx;
  color: #ccc; /* 默认灰色 */
  margin-right: 10rpx;
}

.star.active {
  color: #ffd700; /* 点亮后的颜色 */
}


index.js

// Page({
//   data: {
//     stars: [0, 1, 2, 3, 4, 5], // 6颗星星
//     activeStars: 3, // 默认点亮3颗星星,可以根据成绩数动态设置
//   },

//   // 设置评分
//   setRating: function (e) {
//     const index = e.currentTarget.dataset.index;
//     this.setData({
//       activeStars: index + 1,
//     });
//   },
// });

//如果你需要根据成绩数动态设置点亮的星星数量,可以在 onLoad 或其他生命周期函数中更新 activeStars 的值:
Page({
  data: {
    stars: [0, 1, 2, 3, 4, 5],
    activeStars: 0, // 初始化为0
  },

  onLoad: function (options) {
    const score = 4; // 假设成绩为4
    this.setData({
      activeStars: score,
    });
  },
});


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