原生JS DIV拖拽效果

效果图

html

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<meta charset="utf-8">
	<style type="text/css">
	*{
		margin: 0;
		padding: 0;
	}
	.box-hd{
		width: 300px;
		height: 230px;
		position: absolute;
		left: 0;
		top: 0;
		background: #000;
		-webkit-box-shadow: 0 0 10px rgba(125,125,125,0.8);
	    -moz-box-shadow: 0 0 10px rgba(125,125,125,0.8);
	    box-shadow: 0 0 10px rgba(125,125,125,0.8);
	    border-radius: 4px;
	}
	.box-hd  h2{
		border-top-left-radius: 4px;
		border-top-right-radius: 4px;
		padding: 0 15px;
		color: #fff;
		text-align: left;
		line-height: 30px;
		height: 30px;
		font-size: 14px;
		font-family: '\5FAE\8F6F\96C5\9ED1';
		background: #3498db;
		position: relative;
		cursor: move;
	}
	.box-hd  .btn-closed{
		position: absolute;
		right: 10px;
		top: 0;
		color: #fff;
		text-decoration: none;
	}
	.box-hd .box-inner{
		width: 300px;
		height: 200px;
		background: #fff;
	}

	</style>
</head>
<body>
<div class="box-hd" id="drag-box">
	<h2>这里是标题<a class="btn-closed" href="javascript:;">关闭</a></h2>
	<div class="box-inner">		
	</div>
</div>
<script type="text/javascript">
window.onload=function(){
	var oBox = document.getElementById('drag-box');
	var l = (document.documentElement.clientWidth-oBox.offsetWidth)/2;
	var t = (document.documentElement.clientHeight-oBox.offsetHeight)/2;
	oBox.style.left = l + 'px';
	oBox.style.top = t + 'px';
	drag('drag-box');
};


/*--------------拖曳效果----------------
*原理:标记拖曳状态dragging ,坐标位置X, Y
*/
function drag(name){
	var oBox = document.getElementById(name);
	var disX=0;
	var disY=0;
	oBox.onmousedown=function(ev){
		var oEvent = ev||event;
		disX=oEvent.clientX-oBox.offsetLeft;
		disY=oEvent.clientY-oBox.offsetTop;
		document.onmousemove=function(ev){
			var oEvent = ev||event;
			var l = oEvent.clientX-disX;
			var t = oEvent.clientY-disY;
			if(l<100){
				l=0;	
			}else if(l>document.documentElement.clientWidth-oBox.offsetWidth-100){
				l=document.documentElement.clientWidth-oBox.offsetWidth;
			}
			if(t<100){
				t=0;	
			}else if(t>document.documentElement.clientHeight-oBox.offsetHeight-100){
				t=document.documentElement.clientHeight-oBox.offsetHeight;
			}
			oBox.style.left=l+"px";
			oBox.style.top=t+"px";
		};
		document.onmouseup=function(){
			document.onmousemove=null;
			document.onmouseup=null;
			oBox.releaseCapture&&oBox.releaseCapture();  //释放鼠标捕获
		};
		oBox.setCapture&&oBox.setCapture();  //捕获
		return false;  //防止冒泡
	};
	
};

</script>
</body>
</html>

纯JS制作自动播放选项卡

先预览无一例外的简陋版样子

下面看源码:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<style type="text/css">
*{
	margin: 0;
	padding: 0;	
}
input.cur{
	background: yellow;
}
#box{
	margin: 50px auto;
	width: 402px;
}
#box div{
	width: 400px;
	height: 400px;
	border: #ccc solid 1px;
	display: none
}
#box div.cur{
	display: block;
}
</style>
<script type="text/javascript">
window.onload = function(){
	var aInput = document.getElementsByTagName('input');
	var oBox = document.getElementById('box');
	var aDiv = oBox.getElementsByTagName('div');
	var oPrev = document.getElementById('prev');
	var oNext = document.getElementById('next');
	var now = 0;

	for(var i=0; i<aInput.length; i++)
	{
		aInput[i].index = i;
		aInput[i].onclick = function()
		{
			now=this.index;
			for(var i=0; i<aInput.length; i++)
			{
				aDiv[i].className = '';
				aInput[i].className = '';
			}
			aDiv[this.index].className = 'cur';
			this.className="cur";
		};
	}

	//上一页
	oPrev.onclick = function()
	{
		if(now>0)
		{
			now--;
		}
		else
		{
			now=aInput.length-1;
		}
		for(var i=0; i<aInput.length; i++)
		{
			aInput[i].className = '';
			aDiv[i].className = '';
		}
		aInput[now].className = 'cur';
		aDiv[now].className = 'cur';
	};

	//下一页
	oNext.onclick = next;

	function next()
	{
		if(now<aInput.length-1)
		{
			now++;
		}
		else
		{
			now=0;
		}
		for(var i=0; i<aInput.length; i++)
		{
			aInput[i].className = '';
			aDiv[i].className = '';
		}
		aInput[now].className = 'cur';
		aDiv[now].className = 'cur';
	};

	//自动播放
	var timer = setInterval(next,1000);

	oBox.onmouseover = function()
	{
		clearInterval(timer);
	};
	oBox.onmouseout = function()
	{
		timer = setInterval(next,1000);
	};
};
</script>
</head>

<body>
<div id="box">
	<a id="prev" href="javascript:;">上一页</a>
	<input class="cur" type="button" value="aaaaa">
	<input type="button" value="bbbbb">
	<input type="button" value="ccccc">
	<a id="next" href="javascript:;">下一页</a>
	<hr>
	<div class="cur">1111111111111</div>
	<div>22222222222222222222</div>
	<div>3333333333333333333333333333</div>
</div>
</body>
</html>

JS制作tab选项卡

先看效果图,比较简陋,意思下的哈

下面是代码

<!DOCTYPE>
<html>
<meta charset="UTF-8">
<title></title>
<head>
<style type="text/css">
#div1 a{
	padding: 0 5px;
	color: black;
	text-decoration: none;
	border: #ccc solid 1px;
}
#div1 .active{
	color: white;
	background: red;
}
#div1 div{

	width: 200px;
	height: 200px;
	background: #ccc;
	border: black solid 1px;
	display: none;
}
</style>
<script type="text/javascript">
window.onload=function ()
{
	var oDiv=document.getElementById("div1");
	var aBtn=oDiv.getElementsByTagName("a");
	var aDiv=oDiv.getElementsByTagName("div");
	
	for (var i=0;i<aBtn.length;i++)
	{
		aBtn[i].index=i;
		aBtn[i].onmouseover=function ()
		{
			for (var i=0;i<aBtn.length;i++)
			{
				aBtn[i].className="";
				aDiv[i].style.display="none";
			};
			this.className="active";
			aDiv[this.index].style.display="block";
		};
	};	
};
</script>
</head>
<body>

<div id="div1">
	<a class="active" href="javasript:;">教育</a>
	<a href="javasript:;">培育</a>
	<a href="javasript:;">招生</a>
	<a href="javasript:;">出国</a>
	<div style="display:block;">
		教育教育教育教育教育教育教育
	</div>
	<div>
		培育培育培育培育培育培育培育培育培育
	</div>
	<div>
		招生招生招生招生
	</div>
	<div>
		出国出国出国出国出国出国出国出国出国出国出国出国
	</div>
</div>
</body>

</html>