Fix “Cannot read property ‘toFixed’ of undefined” for Yet Another Starts Rating Plugin

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

  1. Find rater-js.js file under plugin folder.
  2. 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.

如果喜欢本文,给个好评呗!
[参与人数: 6 平均分: 4.3/5]

订阅

发表评论

电子邮件地址不会被公开。