Use jQuery to share star rating code and jquery star rating code
In the previous article, we used Native js to achieve star rating. The coverage may not be very wide. Now we provide a jquery-based star rating.
Copy codeThe Code is 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 codeThe Code is 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; display: block; font-style: normal ;}
. Star strong {color: # ff6600; padding-left: 10px ;}
. Hidden {display: none ;}
</Style>
Copy codeThe Code is 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>
</Head>
<Body>
<Script type = "text/javascript">
$ (Function (){
Var score = new Score ({
Callback: function (cfg ){
Console. log (cfg. starAmount );
}
});
});
</Script>
Copy codeThe Code is as follows:
/**
* JQ score
*/
Function Score (options ){
This. config = {
Selector: '. Star', // scoring container
RenderCallback: null, // callback after page rendering
Callback: null // click the scoring callback
};
This. cache = {
AMsg :[
"Not satisfied | too bad, very dissatisfied with the seller's description ",
"Not satisfied | some of them are damaged, which is inconsistent with the seller's description ",
"General | general quality, not as good as the seller described ",
"Satisfied | the quality is good, which is basically the same as that described by the Seller ",
"Very satisfied | the quality is very good, consistent with the seller's description, 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 + 'lil', parentCls). each (function (index, item ){
// Move the cursor up
$ (Item). mouseover (function (e ){
Var offsetLeft = $ ('ul ', parentCls) [0]. offsetLeft;
Ismax (index + 1 );
$ ('P', parentCls). hasClass ('den den ') & $ ('P', parentCls). removeClass ('den den ');
Detail ('p', parentcls0000.css ({'left': index * $ (this). width () + 12 + 'px '});
Var html = '<em>' +
'<B>' + index + '</B>' + _ cache. aMsg [index]. split ('|') [0] + ''+
'</Em>' + _ cache. aMsg [index]. split ('|') [1];
Parameters ('p', parentcls0000.html (html );
});
// Move the mouse out
$ (Item). mouseout (function (){
Ismax ();
! $ ('P', parentCls). hasClass ('den den ') & $ ('P', parentCls). addClass ('den den ');
});
// Click
$ (Item). click (function (e ){
Var index = $ (_ config. selector + 'lil', parentCls). index ($ (this ));
_ Cache. iStar = index + 1;
! $ ('P', parentCls). hasClass ('den den ') & $ ('P', parentCls). addClass ('den den ');
Var html = '<strong>' +
Index +
'Points </strong> '+ _ cache. aMsg [index]. split (' | ') [1];
('.Desc', parentcls0000.html (html );
_ Config. callback & $. isFunction (_ config. callback) & _ config. callback ({starAmount: _ cache. iStar });
});
});
Function ismax (iArg ){
_ Cache. iScore = iArg | _ cache. iStar;
Var lis = $ (_ config. selector + 'lil', parentCls );
For (var I = 0; I <lis. length; I ++ ){
Lis [I]. className = I <_ cache. iScore? "On ":"";
}
}
}
};
The method of use is super simple, so there is not much nonsense here. Let's take it for free.