js控制div缩放移动

*{margin:0;padding:0;}
#content{
	width:200px;
	height:200px;
	position:relative;
	background:#EEEEEE;
	border:1px solid #f00;
}
#drapdiv{
	width:15px;
	height:15px;
	background:#99CC00;
	position:absolute;
	right:0px;
	bottom:0px;
	overflow:hidden;
	font-size:12px;
	text-align:center;
	line-height:15px;
	color:#FFFFFF;
	float:right;
	z-index:3;
}
#right{
	width:15px;
	height:100%;
	background:#f00;
	float:right;
	position:absolute;
	right:0;
	top:0;
	cursor:e-resize;
	overflow:hidden;
	filter:alpha(opacity:0);
	opacity:0;
	z-index:1;
}

  

window.onload=function()
{
	var oDiv=document.getElementById("drapdiv");
	var oDiv2=document.getElementById("content");
	var right=document.getElementById("right");
	var mouseStart={};
	var rightStart={};
	//往右拽
	right.onmousedown=function(ev)
	{
		var oEvent=ev||event;
		mouseStart.x=oEvent.clientX;
		mouseStart.y=oEvent.clientY;
		rightStart.x=this.offsetLeft;
		if(right.setCapture)
		{
			right.onmousemove=doDrag1;
			right.onmouseup=stopDrag1;
			right.setCapture();
		}
		else
		{
			document.addEventListener("mousemove",doDrag1,true);
			document.addEventListener("mouseup",stopDrag1,true);
		}
	};
	function doDrag1(ev)
	{
		var oEvent=ev||event;
		var l=oEvent.clientX-mouseStart.x+rightStart.x;
		var w=l+oDiv.offsetWidth;
		if(w<oDiv.offsetWidth)
		{
			w=oDiv.offsetWidth;
		}
		else if(w>document.documentElement.clientWidth-oDiv2.offsetLeft)
		{
			w=document.documentElement.clientWidth-oDiv2.offsetLeft-2;
		}
		oDiv2.style.width=w+"px";
	};
	function stopDrag1()
	{
		if(right.releaseCapture)
		{
			right.onmousemove=null;
			right.onmouseup=null;
			right.releaseCapture();
		}
		else
		{
			document.removeEventListener("mousemove",doDrag1,true);
			document.removeEventListener("mouseup",stopDrag1,true);
		}
	};
}

  

<div id="content">
	<div style="width:100%;height:100%;overflow:hidden;">
		<h2>我是标题行</h2>
		<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
		tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
		quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
		consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
		cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
		proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
		<div id="right"></div>
		<div id="drapdiv">拖</div>
	</div>
</div>

  

posted @ 2019-03-08 16:59  寻非语  阅读(1299)  评论(0)    收藏  举报