目录
Issue
When using Yet Another Starts Rating WordPress Plugin on mobile, users can’t vote for article, there is a console error message of “Cannot read property ‘toFixed’ of undefined”.
Cause
The Yet Another Starts Rating plugin using EventListener of “mousemove” to setup the “currentrating” value, which does not work on mobile.
Solution
- Find rater-js.js file under plugin folder.
- find onStarClick function, and add these codes before callback method
//for mobile, redo it for mobile touchover if (typeof currentRating === "undefined"){ if (disabled === true || isRating === true) { return; } var xCoor = e.offsetX; var width = elem.offsetWidth; var percent = xCoor / width * 100; if (percent < 101) { if (step === 1) { currentRating = Math.ceil(percent / 100 * stars); } else { var rat = percent / 100 * stars; for (var i = 0;; i += step) { if (i >= rat) { currentRating = i; break; } } } elem.querySelector(".star-value").style.width = currentRating / stars * 100 + "%"; if (showToolTip) { var toolTip = ratingText.replace("{rating}", currentRating); toolTip = toolTip.replace("{maxRating}", stars); elem.setAttribute("data-title", toolTip); } if (typeof onHover === "function") { onHover(currentRating, rating); } } }
This can be improved by adding a touchmove listener, but current solution is good for use.