码迷,mamicode.com
首页 > Web开发 > 详细

js基础拖拽二

时间:2016-04-26 01:51:40      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:

	var oDiv= document.getElementById("div");
	oDiv.onmousedown= function(e){
		var _this = this;
		var e= e || window.event;
		var diffX= e.clientX - _this.offsetLeft;
		var diffY= e.clientY - _this.offsetTop;
		if(_this.setCapture)  _this.setCapture();
	     document.onmousemove= function(e){
			 var e= e || window.event;
			 var left = e.clientX -diffX;
			 var top= e.clientY -diffY;
			 left= left < 0 ? 0 : left;
			 left= left >  document.documentElement.clientWidth - _this.offsetWidth ? document.documentElement.clientWidth - _this.offsetWidth : left;
			 top= top < 0 ? 0 : top;
			 top= top >  document.documentElement.clientHeight - _this.offsetHeight ? document.documentElement.clientHeight - _this.offsetHeight : top;

			 _this.style.left= left + "px";
			 _this.style.top= top + "px";
		 
		 }
		 document.onmouseup= function(e){
			 this.onmousemove= null;
			 this.onmouseup= null;
			 if(_this.releaseCapture)  _this.releaseCapture();
		 }
	}

 

js基础拖拽二

标签:

原文地址:http://www.cnblogs.com/fangxuele/p/5433413.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!