微信小程序星星评分可显示可评分components附上完整代码

分享一下写的很多评价系统常用的打星评价的星星组件,单独提取出的组件可以直接使用。

发出来的目的是因为自己也写过一次,但是代码搞丢了,这次用到了又重新写了一个,干脆发到博客上,方便自己查看。

WXML文件:

<view class="starArea" >
    <view class="starBox" data-id="{{index}}" wx:for="{{array}}" bindtouchstart="scoringStart" bindtouchmove="moveing" bindtouchend="scoringEnd">
       <image class="star" src="{{item}}"></image>
    </view>
</view>

       WXSS文件:

.starArea{
  height: 50rpx;
  display: flex;
}
.starBox{
  height: 50rpx;
  width: 50rpx;
  display: flex;
  justify-content: center;
  align-items: center;
}
.star{
  width: 45rpx;
  height: 45rpx;
}

    JS文件:

   

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    //分数 10分制 
    score: {
      type: Number,
      value: '',
      observer: function (newVal, oldVal, changedPath) {
        this.createStar(newVal)
      }
    },
   //是否不可打分编辑 默认值不可打分
    edit: {
      type: Boolean,
      value: true,
      observer: function (newVal, oldVal, changedPath) {
        this.setData({
          noEdit: newVal
        })
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    array:[],
    starFull:'/images/xin2.png',
    starHalf:'/images/banke.jpg',
    starNull:'/images/xin1.png',
    noEdit:false,
    x:0
  },

  /**
   * 组件的方法列表
   */
  methods: {
    createStar: function (newVal){
      this.data.array = [];
      this.data.score = newVal;
      for (let i = 10; i > 0; i -= 2) {

        newVal = newVal - 2;
        if (newVal >= 0) {
          this.data.array.push(this.data.starFull);
        }
        //半颗星
        // else if (newVal==-1){
        //   this.data.array.push(this.data.starHalf);
        // }
        else {
          this.data.array.push(this.data.starNull);
        }
      }
      this.setData({
        array: this.data.array
      })
    },
    scoringStart:function(e){
      if (this.data.noEdit)return;
      let index = e.currentTarget.dataset.id
      for(let i = index;i>=0;i--){
        this.data.array[i] = this.data.starFull
      }
      for (let i = index+1; i <= 4; i++) {
        this.data.array[i] = this.data.starNull
      }
      this.data.score = index*2;
      this.setData({
        array: this.data.array
      })
      this.data.x = e.changedTouches[0].pageX
    },
    scoringEnd:function (e) {
      if (this.data.noEdit) return;
      this.triggerEvent("score", { score: this.data.score})
    },
    moveing: function (e) {
      // 移动触发频率过高,渲染页面不合适。
    }
  }

设计仅有五颗星,后来取消的半颗星,那段注释掉了。

有需要的朋友自取。

在下设计拙劣,有更好设计建议,欢迎大家指正。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

来自远方的猪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值