经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » 微信小程序 » 查看文章
微信小程序实现星星评价效果
来源:jb51  时间:2018/11/3 15:36:00  对本文有异议

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

代码实现

wxml文件

  1. <!--pages/evaluatepage/evaluatepage.wxml-->
  2.  
  3. <view class='container'>
  4. <view class='evaluate_contant'>
  5. <!--外层循环控制有几个评价条目 -->
  6. <block wx:for='{{evaluate_contant}}' wx:key='' wx:for-index='idx'>
  7. <view class='evaluate_item'>
  8. <view class='evaluate_title'>{{item}}</view>
  9.  
  10. <!--星星评价 -->
  11. <view class='evaluate_box'>
  12. <!--内层循环展示每个评价条目的星星 -->
  13. <block wx:for="{{stars}}" wx:key=''>
  14. <image class="star-image" style="left: {{item*80}}rpx" src="{{scores[idx] > item ?(scores[idx]-item == 0.5?halfSrc:selectedSrc) : normalSrc}}">
  15. <view class="item" style="left:0rpx" data-score="{{item + 0.5}}" data-idx='{{idx}}' bindtap="selectLeft"></view>
  16. <view class="item" style="left:20rpx" data-score="{{item + 1}}" data-idx='{{idx}}' bindtap="selectRight"></view>
  17. </image>
  18. </block>
  19. </view>
  20.  
  21. </view>
  22. </block>
  23. <button class='submit_button' bindtap='submit_evaluate' type='primary'>提交</button>
  24. </view>
  25. </view>

js文件

  1. Page({
  2.  
  3. data: {
  4. evaluate_contant: ['评价条目一', '评价条目二', '评价条目三',],
  5. stars: [0, 1, 2, 3, 4],
  6. normalSrc: '../../images/no-star.png',
  7. selectedSrc: '../../images/full-star.png',
  8. halfSrc: '../../images/half-star.png',
  9. score: 0,
  10. scores: [0, 0, 0],
  11. },
  12.  
  13. // 提交事件
  14. submit_evaluate: function () {
  15. console.log('评价得分' + this.data.scores)
  16. },
  17.  
  18. //点击左边,半颗星
  19. selectLeft: function (e) {
  20. var score = e.currentTarget.dataset.score
  21. if (this.data.score == 0.5 && e.currentTarget.dataset.score == 0.5) {
  22. score = 0;
  23. }
  24.  
  25. this.data.scores[e.currentTarget.dataset.idx] = score,
  26. this.setData({
  27. scores: this.data.scores,
  28. score: score
  29. })
  30.  
  31. },
  32.  
  33. //点击右边,整颗星
  34. selectRight: function (e) {
  35. var score = e.currentTarget.dataset.score
  36.  
  37. this.data.scores[e.currentTarget.dataset.idx] = score,
  38. this.setData({
  39. scores: this.data.scores,
  40. score: score
  41. })
  42. }
  43. })
  44.  

wxss

  1. /*评价区域 */
  2. .container .evaluate_contant .evaluate_item {
  3. font-size: 30rpx;
  4. color: gray;
  5. margin-left: 20rpx;
  6. margin-top: 30rpx;
  7. }
  8.  
  9. /*评价标题 */
  10. .container .evaluate_contant .evaluate_item .evaluate_title {
  11. display: inline-block;
  12. }
  13.  
  14. /*评价盒子 */
  15. .container .evaluate_contant .evaluate_item .evaluate_box {
  16. position: absolute;
  17. left: 220rpx;
  18. width: 100%;
  19. display: inline-block;
  20. }
  21.  
  22. /*星星评价的每个图片 */
  23. .container .evaluate_contant .evaluate_item .evaluate_box .star-image {
  24. position: absolute;
  25. width: 40rpx;
  26. height: 40rpx;
  27. src: "../../images/no-star.png";
  28. }
  29.  
  30. /*星星的左边和右边区域<点击左边半个星星,点击右边整个星星> */
  31. .container .evaluate_contant .evaluate_item .evaluate_box .star-image .item {
  32. position: absolute;
  33. top: 0rpx;
  34. width: 20rpx;
  35. height: 40rpx;
  36. }
  37.  
  38. /*按钮 */
  39. .container .evaluate_contant .submit_button {
  40. height: 60rpx;
  41. font-size: 30rpx;
  42. line-height: 60rpx;
  43. margin: 20rpx;
  44. }

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号