zoukankan      html  css  js  c++  java
  • js css div 点亮半颗星星(二)

    上回说到js css点亮星星 换种方式来点亮

    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="UTF-8">
    	<title>rating</title>
    </head>
    <style >
    	*{margin:0;padding: 0}
    	div{500px; padding:100px;padding:0 auto;}
    
    </style>
    
    <body>				
    	<div class="rating" id="rating">
    		<img class="rating-item" src="http://images.cnblogs.com/cnblogs_com/tonnytong/769928/o_off.png" alt="" title="很差">
    		<img class="rating-item" src="http://images.cnblogs.com/cnblogs_com/tonnytong/769928/o_off.png" alt="" title="差">
    		<img class="rating-item" src="http://images.cnblogs.com/cnblogs_com/tonnytong/769928/o_off.png" alt="" title="一般">
    		<img class="rating-item" src="http://images.cnblogs.com/cnblogs_com/tonnytong/769928/o_off.png" alt="" title="好">
    		<img class="rating-item" src="http://images.cnblogs.com/cnblogs_com/tonnytong/769928/o_off.png" alt="" title="极好">
    	</div>
    </body>
    </html>
    <script type="text/javascript" src="https://files.cnblogs.com/files/tonnytong/___jquery-1.12.0.min.js"></script>
    <script type="text/javascript">
    	(function($){
    		var ratingFunc = function(elWrap,options){
    			this._opts = {
    				num:1
    			};
    			this._opts = $.extend(this._opts,options);
    			this._el = $(elWrap);
    			this._ratingItems = this._el.find(".rating-item");
    
    			this._lightOn(this._opts.num);
    			this._bindEvent();
    		};
    
    		$.extend(ratingFunc.prototype,{
    			_lightOn: function(num){
                    var that = this;
    				that._ratingItems.each(function(index){
    					var $this = $(this)
    						, intNum = parseInt(num);
    					$this.attr("src","http://images.cnblogs.com/cnblogs_com/tonnytong/769928/o_off.png");
    					if((index +1) <=num)
    					{
    						$this.attr("src","http://images.cnblogs.com/cnblogs_com/tonnytong/769928/o_on.png");
    						if(intNum != num){
    							if(index +1 == intNum){
    								$this.attr("src","http://images.cnblogs.com/cnblogs_com/tonnytong/769928/o_half-on.png");
    							}
    						}
    					}
    				})
    			},
    			_calculateNum: function(obj,eventObj)
    			{
                    var that = this;
    
    				var imgWith = obj.width()
    		 			, offset = obj.offset();
    				if((eventObj.pageX-offset.left) < (0.5*imgWith)){
    					that._opts.num = obj.index()+1+0.5;
    				}else{
    					that._opts.num = obj.index()+1;
    				}				
    			},
    			_bindEvent: function(){
                    var that = this;
    				that._ratingItems.on("click",function(e){
    					that._calculateNum($(this),e);		
    					that._lightOn(that._opts.num);
    				}).on("mousemove",function(e){
    					that._calculateNum($(this),e);							
    					that._lightOn(that._opts.num);
    				})
    			}
    		})
    
    	    $.ratingFunc = function(el,options){
    	        return new ratingFunc(el,options);
    	    };
    	})(jQuery);
    
    	$.ratingFunc("#rating",{num:3.5});
    
    </script>
    

      

  • 相关阅读:
    有趣的linux指令
    Linux——文件打包与压缩
    linux点滴记录
    不归零法编码、曼彻斯特编码和差分曼彻斯特编码
    MySQL点滴记录
    hdu 1200 To and Fro(简单模拟或DP)
    hdu 1081 To The Max(dp+化二维为一维)
    抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)
    抓其根本(一)(hdu2710 Max Factor 素数 最大公约数 最小公倍数.....)
    hdu 1159 Common Subsequence(最长公共子序列 DP)
  • 原文地址:https://www.cnblogs.com/tonnytong/p/7878814.html
Copyright © 2011-2022 走看看