


A brief discussion on how mini programs implement the 'five-star evaluation' function (supports click + slide)
Aug 20, 2021 am 10:44 AMThis article will introduce to you how to implement the "five-star evaluation" function of the mini program, and support the implementation of click scoring and sliding scoring.
Mini Program—Five-star evaluation function
1. Completed requirements:
- Support half-star rating
- Click rating
- Sliding rating
[Related learning recommendations: Mini program development tutorial】
2. Principle
- Page layout
/*wxss 一行內(nèi)展示五顆星并設(shè)置其間距為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 每個(gè)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}}"> <!-- 使用三目運(yùn)算符來動(dòng)態(tài)變化顯示的是哪張圖片,score是js中的分?jǐn)?shù),index是scoreArray的下標(biāo) --> <image class='star' src="{{score>index?(score>index+0.5?fullStarUrl:halfStarUrl):nullStarUrl}}" /> </view> </block> </view>
What is important in the above rendering is what kind of icon should be displayed by the trinocular operation. The index is the element subscript of the scoreArray. The subscript index of each item must be compared with the score. The rules are as follows:
//取 score與index下標(biāo)做比較,默認(rèn)score為0 score<index 展示nullStar.png score>index+0.5 展示fullStar.png index<score<=index+0.5 展示halfStar.png
- data default value
/** * 組件的初始數(shù)據(jù) */ data: { fullStarUrl: '/images/full.png', //滿星圖片 halfStarUrl: '/images/half.png', //半星圖片 nullStarUrl: '/images/null.png', //空星圖片 score: 0, //評價(jià)分?jǐn)?shù) rate: 2, //設(shè)計(jì)稿寬度÷屏幕實(shí)際寬度 startX: 0, //第一顆星screenX的距離 },
- Component loading, data initialization
//設(shè)定比率rate與第一顆星screenX 組件初始化attached 或者 頁面初始化onShow attached: function () { // 在組件實(shí)例進(jìn)入頁面節(jié)點(diǎn)樹時(shí)執(zhí)行 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() },
Click rating
Click once for half a star, click twice for full star;
For example: click is the 4th star (index="3 "), the first three stars are filled with full stars, and the current clicked star is a switching display between half stars and full stars
setScore(e) { //e.currentTarget.dataset.index 為當(dāng)前的item的下標(biāo) let score = e.currentTarget.dataset.index + 1;//實(shí)際的score if (score - this.data.score == 0.5) {//當(dāng)前半星與整星的切換 this.setData({ score: score }) } else {//點(diǎn)擊其他星設(shè)置的分?jǐn)?shù) this.setData({ score: score - 0.5 }) } },
Sliding rating (key part)
changeScore: function (e) { let score = 0; let restLen = 0; //(當(dāng)前觸摸點(diǎn)距初始點(diǎn)距離)-整數(shù)倍*(星星寬+星星之間間距)的剩余距離 let intMult = 0; //取余后的整星數(shù)量 var touchX = e.touches[0].pageX; //獲取當(dāng)前觸摸點(diǎn)X坐標(biāo) var starMinX = this.data.startX; //最左邊第一顆星的X坐標(biāo) var starWidth = 80 * this.data.rate; //星星圖標(biāo)的寬度,假設(shè)80(已在wxss文件中設(shè)置".star") var starLen = 30 * this.data.rate; //星星之間的距離假設(shè)為30(已在wxss文件中設(shè)置".starLen") var starMaxX = starMinX + starWidth * 5 + starLen * 4; //最右側(cè)星星最右側(cè)的X坐標(biāo),需要加上5個(gè)星星的寬度和4個(gè)星星間距 if (touchX >= starMinX && touchX <= starMaxX) { //點(diǎn)擊及觸摸的初始位置在星星所在空間之內(nèi) //使用Math.ceil()方法取得當(dāng)前觸摸位置X坐標(biāo)相對于(星星+星星間距)之比的整數(shù),確定當(dāng)前點(diǎn)擊的是第幾個(gè)星星 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) { //如果當(dāng)前得分不等于剛設(shè)置的值,才賦值,因?yàn)閠ouchmove方法刷新率很高,這樣做可以節(jié)省點(diǎn)資源 this.setData({ score: score, }) } } else if (touchX < starMinX) { //如果點(diǎn)擊或觸摸位置在第一顆星星左邊,則恢復(fù)默認(rèn)值,否則第一顆星星會(huì)一直存在 this.setData({ score: 0, }) } else if (touchX > starMaxX) { this.setData({ score: 5, }) } },
For more programming related knowledge, please visit: Programming Video! !
The above is the detailed content of A brief discussion on how mini programs implement the 'five-star evaluation' function (supports click + slide). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

With the popularity of mobile Internet technology and smartphones, WeChat has become an indispensable application in people's lives. WeChat mini programs allow people to directly use mini programs to solve some simple needs without downloading and installing applications. This article will introduce how to use Python to develop WeChat applet. 1. Preparation Before using Python to develop WeChat applet, you need to install the relevant Python library. It is recommended to use the two libraries wxpy and itchat here. wxpy is a WeChat machine

Implementing card flipping effects in WeChat mini programs In WeChat mini programs, implementing card flipping effects is a common animation effect that can improve user experience and the attractiveness of interface interactions. The following will introduce in detail how to implement the special effect of card flipping in the WeChat applet and provide relevant code examples. First, you need to define two card elements in the page layout file of the mini program, one for displaying the front content and one for displaying the back content. The specific sample code is as follows: <!--index.wxml-->&l

According to news from this site on October 31, on May 27 this year, Ant Group announced the launch of the "Chinese Character Picking Project", and recently ushered in new progress: Alipay launched the "Chinese Character Picking-Uncommon Characters" mini program to collect collections from the society Rare characters supplement the rare character library and provide different input experiences for rare characters to help improve the rare character input method in Alipay. Currently, users can enter the "Uncommon Characters" applet by searching for keywords such as "Chinese character pick-up" and "rare characters". In the mini program, users can submit pictures of rare characters that have not been recognized and entered by the system. After confirmation, Alipay engineers will make additional entries into the font library. This website noticed that users can also experience the latest word-splitting input method in the mini program. This input method is designed for rare words with unclear pronunciation. User dismantling

Mini programs can use react. How to use it: 1. Implement a renderer based on "react-reconciler" and generate a DSL; 2. Create a mini program component to parse and render DSL; 3. Install npm and execute the developer Build npm in the tool; 4. Introduce the package into your own page, and then use the API to complete the development.

How uniapp can achieve rapid conversion between mini programs and H5 requires specific code examples. In recent years, with the development of the mobile Internet and the popularity of smartphones, mini programs and H5 have become indispensable application forms. As a cross-platform development framework, uniapp can quickly realize the conversion between small programs and H5 based on a set of codes, greatly improving development efficiency. This article will introduce how uniapp can achieve rapid conversion between mini programs and H5, and give specific code examples. 1. Introduction to uniapp unia

This article brings you some related issues about WeChat mini programs. It mainly introduces how to use official account template messages in mini programs. Let’s take a look at them together. I hope it will be helpful to everyone.

Implementation idea: Establishing the server side of thread, so as to process the various functions of the chat room. The establishment of the x02 client is much simpler than the server. The function of the client is only to send and receive messages, and to enter specific characters according to specific rules. To achieve the use of different functions, therefore, on the client side, you only need to use two threads, one is dedicated to receiving messages, and the other is dedicated to sending messages. As for why not use one, that is because, only

Geolocation positioning and map display of PHP and mini programs Geolocation positioning and map display have become one of the necessary functions in modern technology. With the popularity of mobile devices, people's demand for positioning and map display is also increasing. During the development process, PHP and applets are two common technology choices. This article will introduce you to the implementation method of geographical location positioning and map display in PHP and mini programs, and attach corresponding code examples. 1. Geolocation in PHP In PHP, we can use third-party geolocation
