`

jquery星级评分

阅读更多

前面有一篇原生js实现星级评分 。可能覆盖面不是很广,现在给出一个jquery实现的星级评分

Html代码 复制代码 收藏代码
  1. <divclass="star">
  2. <span>jQuery星级评论打分</span>
  3. <ul>
  4. <li><ahref="javascript:;">1</a></li>
  5. <li><ahref="javascript:;">2</a></li>
  6. <li><ahref="javascript:;">3</a></li>
  7. <li><ahref="javascript:;">4</a></li>
  8. <li><ahref="javascript:;">5</a></li>
  9. </ul>
  10. </div>
<div class="star">
<span>jQuery星级评论打分</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>

 

Css代码 复制代码 收藏代码
  1. <style>
  2. *{margin:0;padding:0;font-size:13px;}
  3. ul,li{list-style:none;}
  4. .star {position:relative;width:600px;height:24px; margin:20px auto 0;}
  5. .star span {float:left;height:19px;line-height:19px;}
  6. .star ul{margin:0 10px;}
  7. .star li{float:left;width:24px;height:22px;text-indent:-9999px;background:url('star.png') no-repeat;cursor:pointer;}
  8. .star li.on{background-position:0 -28px;}
  9. .star p {padding:10px 10px 0;position:absolute;top:20px;width:159px;height:60px;z-index:100;}
  10. .star p em {color: #FF6600;display: block;font-style: normal;}
  11. .star strong {color:#ff6600;padding-left:10px;}
  12. .hidden{display:none;}
  13. </style>
<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>

 

Js代码 复制代码 收藏代码
  1. <script type="text/javascript" src="http://s.thsi.cn/js/jquery-1.7.2.min.js"></script>
  2. <script type="text/javascript" src="score.js"></script>
  3. </head>
  4. <body>
  5. <script type="text/javascript">
  6. $(function(){
  7. var score = new Score({
  8. callback: function(cfg) {
  9. console.log(cfg.starAmount);
  10. }
  11. });
  12. });
  13. </script>
<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>

 

Js代码 复制代码 收藏代码
  1. /**
  2. * JQ评分效果
  3. */
  4. function Score(options) {
  5. this.config = {
  6. selector : '.star', // 评分容器
  7. renderCallback : null, // 渲染页面后回调
  8. callback : null// 点击评分回调
  9. };
  10. this.cache = {
  11. aMsg : [
  12. "很不满意|差得太离谱,与卖家描述的严重不符,非常不满",
  13. "不满意|部分有破损,与卖家描述的不符,不满意",
  14. "一般|质量一般,没有卖家描述的那么好",
  15. "满意|质量不错,与卖家描述的基本一致,还是挺满意的",
  16. "非常满意|质量非常好,与卖家描述的完全一致,非常满意"
  17. ],
  18. iStar : 0,
  19. iScore : 0
  20. };
  21. this.init(options);
  22. }
  23. Score.prototype = {
  24. constructor: Score,
  25. init: function(options){
  26. this.config = $.extend(this.config,options || {});
  27. var self = this,
  28. _config = self.config,
  29. _cache = self.cache;
  30. self._renderHTML();
  31. },
  32. _renderHTML: function(){
  33. var self = this,
  34. _config = self.config;
  35. var html = '<span class="desc"></span>' +
  36. '<p class="star-p hidden"></p>';
  37. $(_config.selector).each(function(index,item){
  38. $(item).append(html);
  39. $(item).wrap($('<div class="parentCls" style="position:relative"></div>'));
  40. var parentCls = $(item).closest('.parentCls');
  41. self._bindEnv(parentCls);
  42. _config.renderCallback && $.isFunction(_config.renderCallback) && _config.renderCallback();
  43. });
  44. },
  45. _bindEnv: function(parentCls){
  46. var self = this,
  47. _config = self.config,
  48. _cache = self.cache;
  49. $(_config.selector + ' li',parentCls).each(function(index,item){
  50. // 鼠标移上
  51. $(item).mouseover(function(e){
  52. var offsetLeft = $('ul',parentCls)[0].offsetLeft;
  53. ismax(index + 1);
  54. $('p',parentCls).hasClass('hidden') && $('p',parentCls).removeClass('hidden');
  55. $('p',parentCls).css({'left':index*$(this).width() + 12 + 'px'});
  56. var html = '<em>' +
  57. '<b>'+index+'</b>分 '+_cache.aMsg[index].split('|')[0]+'' +
  58. '</em>' + _cache.aMsg[index].split('|')[1];
  59. $('p',parentCls).html(html);
  60. });
  61. // 鼠标移出
  62. $(item).mouseout(function(){
  63. ismax();
  64. !$('p',parentCls).hasClass('hidden') && $('p',parentCls).addClass('hidden');
  65. });
  66. // 鼠标点击
  67. $(item).click(function(e){
  68. var index = $(_config.selector + ' li',parentCls).index($(this));
  69. _cache.iStar = index + 1;
  70. !$('p',parentCls).hasClass('hidden') && $('p',parentCls).addClass('hidden');
  71. var html = '<strong>' +
  72. index +
  73. '分</strong>' +_cache.aMsg[index].split('|')[1];
  74. $('.desc',parentCls).html(html);
  75. _config.callback && $.isFunction(_config.callback) && _config.callback({starAmount:_cache.iStar});
  76. });
  77. });
  78. function ismax(iArg) {
  79. _cache.iScore = iArg || _cache.iStar;
  80. var lis = $(_config.selector + ' li',parentCls);
  81. for(var i = 0; i < lis.length; i++) {
  82. lis[i].className = i < _cache.iScore ? "on" : "";
  83. }
  84. }
  85. }
  86. };
/**
 * JQ评分效果
 */
 function Score(options) {
	this.config = {
		selector                  :   '.star',     // 评分容器
		renderCallback            :   null,        // 渲染页面后回调
		callback                  :   null         // 点击评分回调                         
	};

	this.cache = {
		aMsg : [
				"很不满意|差得太离谱,与卖家描述的严重不符,非常不满",
				"不满意|部分有破损,与卖家描述的不符,不满意",
				"一般|质量一般,没有卖家描述的那么好",
				"满意|质量不错,与卖家描述的基本一致,还是挺满意的",
				"非常满意|质量非常好,与卖家描述的完全一致,非常满意"
				],
		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){
			
			// 鼠标移上
			$(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() + 12 + 'px'});
				

				var html = '<em>' + 
							  '<b>'+index+'</b>分 '+_cache.aMsg[index].split('|')[0]+'' + 
						   '</em>' + _cache.aMsg[index].split('|')[1];
				$('p',parentCls).html(html);
			});

			// 鼠标移出
			$(item).mouseout(function(){
				ismax();
 				!$('p',parentCls).hasClass('hidden') && $('p',parentCls).addClass('hidden');
			});
			
			// 鼠标点击
			$(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 +
						   '分</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" : "";
			}
		}
	}
 };

 

这个也是在网上找的一个相对自己来说更实用一点的。做储备。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics