欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

js封装拖拽事件

程序员文章站 2022-06-30 19:13:24
...
 	function drag(elem){
		 		var disX,disY;
		 		addEvent(elem,"mousedown",function(){
		 			var event=e||window.event;
		 			disX=event.clientX-parseInt(elem.getStyle("left"));
				    disY=event.clientY-parseInt(elem.getStyle("top"));
		 			addEvent(document,"mouseMove",mouseMove);
		 			addEvent(document,"mouseUp",mouseUp);
		 			stopBubble(event);
		 			cancelHandler(event);
		 		});
		 		function mouseMove(e){
		 			var event=e||window.event;
		 			elem.sttyle.left=event.clientX-disX+"px";
		 			elem.sttyle.top=event.clientY-disY+"px";

		 		}
		 		function mouseUp(e){
		 			var event=e||window.event;
		 			removeEvent(document,"mouseMove",mouseMove);
		 			removeEvent(document,"mouseUp",mouseUp);
		 		}
		 	}