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.

看完了?留个评分呗?
[8人评了分,平均: 4.3/5]

本站原创文章皆遵循“署名-非商业性使用-相同方式共享 3.0 (CC BY-NC-SA 3.0)”。转载请保留以下标注:

原文来源:《Fix “Cannot read property ‘toFixed’ of undefined” for Yet Another Starts Rating Plugin》

发表评论

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

返回顶部