---
title: "drag1"
author: "ganjuelovejava"
date: "2010-11-16T17:57:36Z"
tags: []
categories: ["编程语言"]
slug: "drag1"
draft: true
lastmod: "2010-11-16T17:59:34Z"
id: 813086
url: "https://ganjuelovejava.iteye.com/blog/813086"
---

(function ($) {
	$.dragDrop=function (p) {
		var cols=$(p.accepter),//accepter集
		ground=,//坐标空间
		t,//被拖动元素
		m={
		},//被拖动元素属性集
		tip=$("<div style='position:absolute;height:4px;overflow:hidden; background:#F30'></div>"),//占位元素
		clone,
		fly,//拖动启动开关
		base,//被选中的垒
		ball,	//用于定位的accepter中对象
		mark,//最后一次插入的相对对象
		above=true,
		
		befordrag=function (v) {
			v.stopPropagation();
			v.preventDefault();
			m={
				ex:v.clientX,ey:v.clientY+$(document).scrollTop(),x:t.position().left,y:t.position().top,w:t.outerWidth(true),h:t.outerHeight(true)
			};
			$(document).mousemove(ondrag).mouseup(afterdrag);
			if(document.body.setCapture) {
				t.get(0).setCapture();
				t.get(0).onmousewheel=mousewheel
			}
		},
		ondrag=function (v) {
			v.preventDefault();
			if(!fly) {
				fly=true;
				//将可移动元素的坐标存储到ground集合中。
				makeGround();
				tip.insertBefore(t);
				mark=t.get(0);
				//拖动元素的副文本样式
				clone=t.clone().css({
					"position":"absolute","opacity":0.5,"left":m.x,"top":m.y,"width":m.w,"z-index":125058687
				}).insertAfter(t);
			};
			
			selectBase(v.clientX+$(document).scrollLeft(),v.clientY+$(document).scrollTop());
			clone.css({
				"left":v.clientX-m.ex+m.x,"top":v.clientY-m.ey+m.y+$(document).scrollTop()
			});		
			//if($(base).children().index(ball)==-1)
			{//修正除IE8、firefox、chrome下的光标与拖动中的对象的显示异常
				tip.insertBefore(ball);
				clone.insertAfter(ball);
				
			}
			
			
			if(p.ondrag || typeof p.ondrag=="function")
			{
				p.ondrag(v,t,base,ball);
			}
		},
//初始绑定事件
		cols.find(p.target).each(function (i,o) {
										   
			(p.handle?$(o).find(p.handle):$(o)).mousedown(function (v) {
				t=$(o);
				befordrag(v)
			})
		});