微信小程序实现五星评价功能

 更新时间:2022年08月28日 10:31:01   作者:远方有多远_  
这篇文章主要为大家详细介绍了微信小程序实现五星评价功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了微信小程序实现五星评价的具体代码,供大家参考,具体内容如下

需求如图:

1个星-很不满意;

2个星-不满意;

3个星-一般;

4个星-还不错;

5个星-很满意;

找了demo,删删改改,demo地址:微信小程序实现星星评价效果

需要的页面引入:

json:

{
      "usingComponents": {
        "star": "../../components/star/star"
      },
      "navigationBarTitleText": "评价"
 }

wxml:

<star></star>

组件代码:

wxml:

<!--components/star/star.wxml-->

<view class='buy' bindtap='showBuyModal'>测试版评价本次维修服务</view>

<!-- 点击弹出遮罩层 -->
<view class="cover_screen" bindtap="hideBuyModal" wx:if="{{showModalStatus}}"></view>
<!-- 点击立即购买 弹窗 -->
<view animation="{{animationData}}" class="buy_box" wx:if="{{showModalStatus}}">
  <view class='startitle'>{{tatle}}</view>
  <!-- 星星start -->
  <view class='container'>
    <view class='evaluate_contant'>
      <!--外层循环控制有几个评价条目 -->
      <block wx:for='{{evaluate_contant}}' wx:key='' wx:for-index='idx'>
        <view class='evaluate_item'>
          <view class='evaluate_title'>{{staritem}}</view>
          <!--星星评价 -->
          <view class='evaluate_box'>
            <!--内层循环展示每个评价条目的星星 -->
            <block wx:for="{{stars}}" wx:key=''>
              <image class="star-image" style="left: {{item*80}}rpx" src="{{scores[idx] > item ?normalSrc:selectedSrc}}">
                <view class="staritem" data-score="{{item + 1}}" data-idx='{{idx}}' bindtap="selectRight"></view>
              </image>
            </block>
          </view>

        </view>
      </block>
      <view class="evaluation">{{evaluation}}</view>
      <!-- <button class='sub_button' bindtap='submit_evaluate' type='primary'>提交</button> -->
    </view>
  </view>
  <!-- 星星end -->
  <view class="detail-btn">
  <button class="done-person" style='border:4rpx solid #FF4A4A;background-color: #FFFFFF;font-weight: bold;color: #FF4A4A;font-size:28rpx;' bindtap='hideBuyModal' plain='{{plain}}'>取消</button>

    <button class="done-person" style='border:none;background-color: #FF4A4A;font-weight: bold;color: #FFFFFF;font-size:28rpx;' bindtap='submit_evaluate' plain='{{plain}}'>提交</button>

    <!-- <button type="primary" bindtap='hideBuyModal' class=""> 取消</button>
    <button type="primary" bindtap='submit_evaluate' class=""> 确定 </button> -->
  </view>
</view>

js:

// components/star/star.js

Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    showModalStatus: false,
    tatle: "您对这次服务的评价",
    // 星星
    evaluate_contant: ['一', ],
    stars: [0, 1, 2, 3, 4],
    normalSrc: '../../image/star_gray.png',
    selectedSrc: '../../image/star_red.png',
    score: 0,
    scores: [0],
    evaluation:'  ',
    plain:true
  },

  /**
   * 组件的方法列表
   */
  methods: {

    // 星星颗数start
    // 提交事件
    submit_evaluate: function() {
      console.log('评价得分' + this.data.scores)
    },
    //点击星
    selectRight: function(e) {
      var score = e.currentTarget.dataset.score
      console.log(score)
      this.data.scores[e.currentTarget.dataset.idx] = score

      let evaluation = score == 1 ? '很不满意' : (score == 2 ? '不满意' : (score == 3 ? '一般' : (score == 4 ? '还不错' : (score == 5 ? '很满意' : ''))))

      this.setData({
        scores: this.data.scores,
        score: score,
        evaluation: evaluation
      })
    },
    // 星星颗数end
    // onLoad: function (options) {
    //   console.log(options.id)
    // },
    showBuyModal() {
      // 显示遮罩层
      var animation = wx.createAnimation({
        duration: 200,
        /**
         * http://cubic-bezier.com/ 
         * linear 动画一直较为均匀
         * ease 从匀速到加速在到匀速
         * ease-in 缓慢到匀速
         * ease-in-out 从缓慢到匀速再到缓慢
         * 
         * http://www.tuicool.com/articles/neqMVr
         * step-start 动画一开始就跳到 100% 直到动画持续时间结束 一闪而过
         * step-end 保持 0% 的样式直到动画持续时间结束 一闪而过
         */
        timingFunction: "ease",
        delay: 0
      })
      this.animation = animation
      animation.translateY(300).step()
      this.setData({
        animationData: animation.export(), // export 方法每次调用后会清掉之前的动画操作。
        showModalStatus: true
      })
      setTimeout(() => {
        animation.translateY(0).step()
        this.setData({
          animationData: animation.export() // export 方法每次调用后会清掉之前的动画操作。
        })
        
      }, 200)
    },

    hideBuyModal() {
      // 隐藏遮罩层
      var animation = wx.createAnimation({
        duration: 200,
        timingFunction: "ease",
        delay: 0
      })
      this.animation = animation
      animation.translateY(300).step()
      this.setData({
        animationData: animation.export(),
      })
      setTimeout(function() {
        animation.translateY(0).step()
        this.setData({
          animationData: animation.export(),
          showModalStatus: false
        })
        // console.log(this)
      }.bind(this), 200)
    }


  }
})

wxss:

/* components/star/star.wxss */

.buy{
  margin-top: 200rpx;
  text-align: center;
}
.cover_screen {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: #000;
  opacity: 0.2;
  overflow: hidden;
  z-index: 1000;
  color: #fff;
}
.buy_box {
  width: 100%;
  box-sizing: border-box;
  position: fixed;
  bottom: 0;
  left: 0;
  z-index: 2000;
  background: #fff;
  padding: 20rpx;
  overflow: hidden;
}
.buy_box .startitle {
    font-size: 28rpx;
    text-align: center;
    line-height: 40rpx;
    color: #333;
    padding: 20rpx 0;
}
/* 星星 */
.footer_end{
  display: flex
}
.footer_end button{
  width: 30%;
}
/* 插入星星 */
/*评价区域 */
.container .evaluate_contant .evaluate_item {
 font-size: 30rpx;
 color: gray;
 margin-left: 20rpx;
 margin-top: 30rpx;
}
 
/*评价标题 */
.container .evaluate_contant .evaluate_item .evaluate_title {
 display: inline-block;
}
 
/*评价盒子 */
.container .evaluate_contant .evaluate_item .evaluate_box {
 position: absolute;
 left: 220rpx;
 width: 100%;
 display: inline-block;
}
 
/*星星评价的每个图片 */
.container .evaluate_contant .evaluate_item .evaluate_box .star-image {
 position: absolute;
 width: 40rpx;
 height: 40rpx;
 src: "../../image/star_gray.png";
}

/* 评价对应内容 */
.evaluation{
  text-align: center;
  margin: 30rpx 0 40rpx;
  color: #535353;
}
 
/*星星区域 */
.container .evaluate_contant .evaluate_item .evaluate_box .star-image .staritem {
 position: absolute;
 top: 0rpx;
 left: 0rpx;
 width: 40rpx;
 height: 40rpx;
}
 
/*按钮 */
.container .evaluate_contant .sub_button {
 height: 60rpx;
 font-size: 30rpx;
 line-height: 60rpx;
 margin: 20rpx;
}


.detail-btn{
  width: 750rpx;
  display: flex;
  justify-content: space-around;
  align-items: center;
  margin-bottom: 100rpx;
}
.done-person{
  width:220rpx;
  height:72rpx;
  border-radius:36rpx;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • JS操作CSS随机改变网页背景实现思路

    JS操作CSS随机改变网页背景实现思路

    JS和CSS让页面每次刷新随机产生一张背景图,当然我的回答是可以的,下面是具体的实现思路,感兴趣的朋友可以参考下
    2014-03-03
  • 利用js的闭包原理做对象封装及调用方法

    利用js的闭包原理做对象封装及调用方法

    下面小编就为大家带来一篇利用js的闭包原理做对象封装及调用方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • canvas滤镜效果实现代码

    canvas滤镜效果实现代码

    这篇文章主要为大家详细介绍了canvas滤镜效果的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-02-02
  • JavaScript中new关键字的使用详解

    JavaScript中new关键字的使用详解

    在 JavaScript 中,new 关键字不仅是面向对象编程的关键要素,还是创建实例的重要手段,本文将深入探讨 new 关键字的使用,理解它在对象创建和构造函数调用中的作用,需要的朋友可以参考下
    2023-11-11
  • RequireJS用法简单示例

    RequireJS用法简单示例

    这篇文章主要介绍了RequireJS用法,结合简单实例形式分析了RequireJS项目文件结构、相关操作技巧与注意事项,需要的朋友可以参考下
    2018-08-08
  • BootStrap下拉框在firefox浏览器界面不友好的解决方案

    BootStrap下拉框在firefox浏览器界面不友好的解决方案

    BootStrap下拉框在firefox浏览器界面很不友好,在firefix浏览器打开链接就会发现里面有个小容器,怎么处理呢,下面看下小编给大家分享的有关这个问题的处理方案
    2016-08-08
  • 微信小程序web-view无法打开该页面不支持打开的解决方法

    微信小程序web-view无法打开该页面不支持打开的解决方法

    小程序现在日渐成熟,功能也越来越强大,我们今天来一起看看小程序跳转的问题,下面这篇文章主要给大家介绍了关于微信小程序web-view无法打开该页面不支持打开的解决方法,需要的朋友可以参考下
    2023-01-01
  • 微信小程序获取地理位置及经纬度授权代码实例

    微信小程序获取地理位置及经纬度授权代码实例

    这篇文章主要介绍了微信小程序获取地理位置及经纬度授权代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • javascript实现页面滚屏效果

    javascript实现页面滚屏效果

    本文主要介绍了javascript实现页面滚屏效果的方法,具有一定的参考价值,下面跟着小编一起来看下吧
    2017-01-01
  • 浅析JavaScript中let与const命令的区别

    浅析JavaScript中let与const命令的区别

    这篇文章主要为大家详细介绍了JavaScript中let命令与const命令的用法及区别,文中的示例代码讲解详细,具有一定的借鉴价值,需要的可以参考下
    2023-09-09

最新评论