一个纯JS的微调按钮类

做人要厚道,转载请注明
就是那种点上下箭头加减数字的东东 , 费话少说,上代码

 

//itsuki@20110405
function spinner( _target ){
	var _this		= this;
	this.target		= {};
	this.spin		= null;
	this.step		= 1;
	
	this.bind		= function(){
		if (!this.target) return false;
		this.spin	= document.createElement( "label" );
		this.spin.style.cssText = "display:inline-block;width:20px;height:24px;overflow:auto;overflow-y:scroll;vertical-align:middle;";
		this.spin.innerHTML = "<br/><br/><br/><br/>";
		this.spin.onmouseover = function(){
			_this.spin.scrollTop = 1;
		}
		
		this.spin.onscroll = function(){
			if ( _this.spin.scrollTop == _this.spin.getAttribute("orgscrolltop") ) return;

			if (_this.spin.scrollTop!=1){
				if (!_this.target.value || isNaN(_this.target.value)) _this.target.value=0;
				if (_this.spin.scrollTop==0){
					_this.target.value = parseFloat(_this.target.value,10)+_this.step;
				}else{
					_this.target.value = parseFloat(_this.target.value,10)-1;
					if (parseFloat(_this.target.value,10)<0) _this.target.value=0;
				}
			}

			_this.spin.setAttribute("orgscrolltop",_this.spin.scrollTop);
			_this.spin.scrollTop = 1;
		}

		if (this.target.nextSibling){
			this.target.parentNode.insertBefore( this.spin , this.target.nextSibling );
		}else{
			this.target.parentNode.appendChild( this.spin );
		}
	}

	if (_target){
		this.target = document.getElementById( _target );
		this.bind();
	}
}

 

HOW TO USE

 

<!DOCTYPE html  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>-</title>
	<script type="text/javascript" src="/scripts/class.spinner.js"></script>
</head>
<body>
	<div >
		<input id="input1" />
		<input id="input2" />
		<script type="text/javascript">
			new spinner("input1");
			new spinner("input2");
		</script>
	</div>
</body>
</html>
 

兼容性:

测试了IE和chrome木有问题,FF下必须要设置高度为34以上才有滚动条,囧...反正FF恶心也不是一天两天了...

 

 

 

 

 

你可能感兴趣的:(JavaScript,html,XHTML,IE,chrome)