• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • dedecms
  • ecshop
  • z-blog
  • UcHome
  • UCenter
  • drupal
  • WordPress
  • 帝国cms
  • phpcms
  • 动易cms
  • phpwind
  • discuz
  • 科汛cms
  • 风讯cms
  • 建站教程
  • 运营技巧
您的位置:首页 > CMS教程 >建站教程 > 浅谈小程序怎么实现“五星评价”功能(支持点击+滑动)

浅谈小程序怎么实现“五星评价”功能(支持点击+滑动)

作者:站长图库 字体:[增加 减小] 来源:互联网 时间:2022-04-29

站长图库向大家介绍了小程序怎么实现评价,五星评价功能,小程序点击评价,小程序滑动评价等相关知识,希望对您有所帮助

本篇文章给大家介绍一下小程序实现“五星评价”功能的方法,支持实现点击评分、滑动评分。


浅谈小程序怎么实现“五星评价”功能(支持点击+滑动)


小程序— 五星评价功能

1、已完成需求:

支持半星评价

点击评分

滑动评分


2、原理

页面布局

/*wxss 一行内展示五颗星并设置其间距为30rpx*/.scoreContent {display: inline-block;}.starLen {margin-right: 30rpx;display: inline-block;}.scoreContent .starLen:last-child {margin-right: 0;}.star {width: 80rpx;height: 80rpx;vertical-align: text-bottom;display: inline-block;}
/*wxml 每个starLen元素上绑定 touchMove touchEnd tap事件*/<view class="scoreContent" id="scoreContent">  <block wx:for='{{5}}' wx:key='item'>    <!-- 遍历评分列表 -->    <view class='starLen' catchtouchmove='changeScore' catchtouchend="changeScoreEnd" catchtap='setScore' data-index="{{index}}">      <!-- 使用三目运算符来动态变化显示的是哪张图片,score是js中的分数,index是scoreArray的下标 -->      <image class='star' src="{{score>index?(score>index+0.5?fullStarUrl:halfStarUrl):nullStarUrl}}" />    </view>  </block></view>

以上渲染中重要的是,三目运算所要展示的应该是哪种图标,index为scoreArray的元素下标,每一个item的下标index都要与score进行比较,规则如下:

//取 score与index下标做比较,默认score为0score<index    展示nullStar.png score>index+0.5  展示fullStar.png index<score<=index+0.5 展示halfStar.png

data预设值

/** * 组件的初始数据 */data: {  fullStarUrl: '/images/full.png', //满星图片  halfStarUrl: '/images/half.png', //半星图片  nullStarUrl: '/images/null.png', //空星图片  score: 0, //评价分数  rate: 2,  //设计稿宽度÷屏幕实际宽度  startX: 0, //第一颗星screenX的距离},

组件加载、数据做初始化

//设定比率rate与第一颗星screenX    组件初始化attached  或者 页面初始化onShowattached: function () { // 在组件实例进入页面节点树时执行  let { screenWidth } = wx.getSystemInfoSync();  let rate = screenWidth / 750;  this.setData({    rate: rate  })     const query = this.createSelectorQuery();  query.select('#scoreContent').boundingClientRect((res)=> {    this.setData({      startX: res.left    })  }).exec()},


点击评分

点击1次半星,点击2次整星;

例如:点击是第4颗星(index="3"),前三颗星为整星填充,当前点击星为半星与整星的切换展示

setScore(e) {  //e.currentTarget.dataset.index 为当前的item的下标  let score = e.currentTarget.dataset.index + 1;//实际的score  if (score - this.data.score == 0.5) {//当前半星与整星的切换    this.setData({      score: score    })  } else {//点击其他星设置的分数    this.setData({      score: score - 0.5    })  }},

滑动评分(关键部分)

changeScore: function (e) {  let score = 0;  let restLen = 0; //(当前触摸点距初始点距离)-整数倍*(星星宽+星星之间间距)的剩余距离  let intMult = 0; //取余后的整星数量  var touchX = e.touches[0].pageX; //获取当前触摸点X坐标  var starMinX = this.data.startX; //最左边第一颗星的X坐标  var starWidth = 80 * this.data.rate; //星星图标的宽度,假设80(已在wxss文件中设置".star")  var starLen = 30 * this.data.rate; //星星之间的距离假设为30(已在wxss文件中设置".starLen")  var starMaxX = starMinX + starWidth * 5 + starLen * 4; //最右侧星星最右侧的X坐标,需要加上5个星星的宽度和4个星星间距   if (touchX >= starMinX && touchX <= starMaxX) { //点击及触摸的初始位置在星星所在空间之内    //使用Math.ceil()方法取得当前触摸位置X坐标相对于(星星+星星间距)之比的整数,确定当前点击的是第几个星星    intMult = Math.floor((touchX - starMinX) / (starWidth + starLen));    restLen = (touchX - starMinX) - intMult * (starWidth + starLen);     if (0 <= restLen && restLen < 0.5 * starWidth) { //空星      score = intMult    } else if (0.5 * starWidth <= restLen && restLen < starWidth) { //显示半星      score = intMult + 0.5    } else if (starWidth <= restLen) { //显示整星      score = intMult + 1;    }    if (score != this.data.score) { //如果当前得分不等于刚设置的值,才赋值,因为touchmove方法刷新率很高,这样做可以节省点资源      this.setData({        score: score,      })    }  } else if (touchX < starMinX) { //如果点击或触摸位置在第一颗星星左边,则恢复默认值,否则第一颗星星会一直存在    this.setData({      score: 0,    })  } else if (touchX > starMaxX) {    this.setData({      score: 5,    })  }},


分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • 浅谈小程序怎么实现“五星评价”功能(支持点击+滑动)

相关文章

  • 2022-04-29PHP保存数组到数据库
  • 2022-04-29Photoshop制作彩色效果艺术字教程
  • 2022-04-29PHP怎么进行登入操作和注销登录(实例演示)
  • 2022-04-29Photoshop CS6制作3D文字的片头动画教程
  • 2022-04-29Photoshop制作可口的饼干文字特效
  • 2022-04-29MySQL的where查询的重新认识
  • 2022-04-29Photoshop金属质感的艺术字教程
  • 2022-04-29制作幸运星插画图
  • 2022-04-29PHP+Mysql+jQuery找回密码
  • 2022-04-29JS hasOwnProperty()方法检测一个属性是否是对象的自有属性的方法

文章分类

  • dedecms
  • ecshop
  • z-blog
  • UcHome
  • UCenter
  • drupal
  • WordPress
  • 帝国cms
  • phpcms
  • 动易cms
  • phpwind
  • discuz
  • 科汛cms
  • 风讯cms
  • 建站教程
  • 运营技巧

最近更新的内容

    • Photoshop设计电影导演工作室网站首页
    • Angular中怎么自定义视频播放器
    • 解析TP框架下mongo的基础操作及其注意点
    • 利用IF判断自定义Phpcms V9列表的图文并茂
    • PHP如何去掉所有HTML标签?
    • 那些你不知道的Photoshop冷知识技巧
    • WordPress建站教程,纯代码实现wordpress防止发布文章出现标题重复,自动检测重复标题文章
    • 外链的好坏与网站获得很好的排名无关
    • Dedecms怎么实现键盘翻页的功能
    • 解决JavaScript中数组排序sort不发生改变

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有