Using jquery to achieve star rating code sharing _jquery

Source: Internet
Author: User

The front has a native JS to achieve the star rating. Perhaps the coverage is not very broad, now give a jquery implementation of the star rating.

Copy Code code as follows:

<div class= "Star" >
<span>jquery star rating </span>
<ul>
<li><a href= "javascript:;" >1</a></li>
<li><a href= "javascript:;" >2</a></li>
<li><a href= "javascript:;" >3</a></li>
<li><a href= "javascript:;" >4</a></li>
<li><a href= "javascript:;" >5</a></li>
</ul>
</div>

Copy Code code as follows:

<style>
*{margin:0;padding:0;font-size:13px;}
Ul,li{list-style:none;}
. star {position:relative;width:600px;height:24px; margin:20px auto 0;}
. star span {float:left;height:19px;line-height:19px;}
. Star ul{margin:0 10px;}
. Star Li{float:left;width:24px;height:22px;text-indent:-9999px;background:url (' Star.png ') No-repeat;cursor: pointer;}
. Star li.on{background-position:0 -28px;
. Star P {padding:10px 10px 0;position:absolute;top:20px;width:159px;height:60px;z-index:100;}
. Star p EM {color: #FF6600;d Isplay:block;font-style:normal}
. Star Strong {color: #ff6600;p adding-left:10px}
. Hidden{display:none;}
</style>

Copy Code code as follows:

<script type= "Text/javascript" src= "Http://s.thsi.cn/js/jquery-1.7.2.min.js" ></script>
<script type= "Text/javascript" src= "Score.js" ></script>

<body>
<script type= "Text/javascript" >
$ (function () {
var score = new Score ({
Callback:function (CFG) {
Console.log (Cfg.staramount);
}
});
});
</script>


Copy Code code as follows:

/**
* JQ Scoring effect
*/
function Score (options) {
This.config = {
Selector: '. Star ',//grading container
Rendercallback:null,//Render page after callback
Callback:null//Click Score Callback
};

This.cache = {
AMSG: [
"Very dissatisfied | Too far, and the seller described the serious discrepancy, very unhappy",
"Dissatisfied with | partial breakage, inconsistent with seller's description, dissatisfied",
"General | Quality general, no seller described so well",
"Satisfied | The quality is good, and the seller described the Basic Agreement, or quite satisfied",
"Very satisfied | The quality is very good, and the seller described the exact same, very satisfied"
],
istar:0,
iscore:0
};

This.init (options);
}

Score.prototype = {

Constructor:score,

Init:function (options) {
This.config = $.extend (This.config,options | | {});
var self = this,
_config = Self.config,
_cache = Self.cache;

Self._renderhtml ();
},
_renderhtml:function () {
var self = this,
_config = Self.config;
var html = ' <span class= ' desc ' ></span> ' +
' <p class= ' star-p hidden ' ></p> ';
$ (_config.selector). each (function (Index,item) {
$ (item). append (HTML);
$ (item). Wrap ($ (' <div class= "parentcls" style= "position:relative" ></div> "));
var parentcls = $ (item). closest ('. Parentcls ');
Self._bindenv (PARENTCLS);
_config.rendercallback && $.isfunction (_config.rendercallback) && _config.rendercallback ();
});

},
_bindenv:function (PARENTCLS) {
var self = this,
_config = Self.config,
_cache = Self.cache;

$ (_config.selector + ' Li ', parentcls). Each (function (Index,item) {

Move mouse over
$ (item). mouseover (function (e) {
var offsetleft = $ (' ul ', parentcls) [0].offsetleft;
Ismax (index + 1);

$ (' P ', parentcls). Hasclass (' hidden ') && $ (' P ', parentcls). Removeclass (' hidden ');
$ (' P ', parentcls). css ({' Left ': index*$ (This). Width () + + ' px '});


var html = ' <em> ' +
' <b> ' +index+ ' </b> min ' +_cache.amsg[index].split (' | ') [0]+ ' +
' </em> ' + _cache.amsg[index].split (' | ') [1];
$ (' P ', parentcls). HTML (HTML);
});

Mouse move out
$ (item). mouseout (function () {
Ismax ();
!$ (' P ', parentcls). Hasclass (' hidden ') && $ (' P ', parentcls). addclass (' hidden ');
});

Mouse click
$ (item). Click (function (e) {
var index = $ (_config.selector + ' Li ', parentcls). Index ($ (this));
_cache.istar = index + 1;

!$ (' P ', parentcls). Hasclass (' hidden ') && $ (' P ', parentcls). addclass (' hidden ');
var html = ' <strong> ' +
Index +
' Min </strong> ' +_cache.amsg[index].split (' | ') [1];

$ ('. Desc ', parentcls). HTML (HTML);
_config.callback && $.isfunction (_config.callback) && _config.callback ({staramount:_cache.istar});
});

});

function Ismax (iarg) {
_cache.iscore = Iarg | | _cache.istar;
var lis = $ (_config.selector + ' Li ', parentcls);

for (var i = 0; i < lis.length; i++) {
Lis[i].classname = i < _cache.iscore? "On": "";
}
}
}
};

The use of the method is super simple, here is no more nonsense, small partners take free play it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.