欢迎光临,了解微信小程序开发,就上易用通!

微信小程序开发之五星评分(含半颗星评分)

发布:2018-01-31 12:09浏览: 来源:网络 作者:cola


一位同学说要写五星评分.要有半颗星的评分. 
于是我做了个玩具.有空了做模块化,这代码看不下去了. 
star03.gif

代码: 
1.index.wxml
<!--index.wxml-->

<block wx:for="{{stars}}">

  <image class="star-image" style="left: {{item*150}}rpx" src="{{key > item ?(key-item == 0.5?halfSrc:selectedSrc) : normalSrc}}">

    <view class="item" style="left:0rpx" data-key="{{item+0.5}}" bindtap="selectLeft"></view>

    <view class="item" style="left:75rpx" data-key="{{item+1}}" bindtap="selectRight"></view>

  </image>

</block>

2.index.wxss




.item {

  position: absolute;

  top: 50rpx;

  width: 75rpx;

  height: 150rpx;

}


3.index.js
//index.js



//获取应用实例

var app = getApp()

Page({

  data: {

    stars: [0, 1, 2, 3, 4],

    normalSrc: '../../images/normal.png',

    selectedSrc: '../../images/selected.png',

    halfSrc: '../../images/half.png',

    key: 0,//评分

  },

  onLoad: function () {

  },

  //点击右边,半颗星

  selectLeft: function (e) {

    var key = e.currentTarget.dataset.key

    if (this.data.key == 0.5 && e.currentTarget.dataset.key == 0.5) {

      //只有一颗星的时候,再次点击,变为0颗

      key = 0;

    }

    console.log("得" + key + "分")

    this.setData({

      key: key

    })



  },

  //点击左边,整颗星

  selectRight: function (e) {

    var key = e.currentTarget.dataset.key

    console.log("得" + key + "分")

    this.setData({

      key: key

    })

  }

})





免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。