日期:2014-05-16  浏览次数:20361 次

JS拖动容器工具类
var isIE = (document.all) ? true : false;
var $ = function(id) {
	return "string" == typeof id ? document.getElementById(id) : id;
};
var Class = {
	create : function() {
		return function() {
			this.initialize.apply(this, arguments);
		}
	}
}
var Extend = function(destination, source) {
	for (var property in source) {
		destination[property] = source[property];
	}
}
var Bind = function(object, fun) {
	return function() {
		return fun.apply(object, arguments);
	}
}
var BindAsEventListener = function(object, fun) {
	var args = Array.prototype.slice.call(arguments).slice(2);
	return function(event) {
		return fun.apply(object, [event || window.event].concat(args));
	}
}
var CurrentStyle = function(element) {
	return element.currentStyle
			|| document.defaultView.getComputedStyle(element, null);
}
function addEventHandler(oTarget, sEventType, fnHandler) {
	if (oTarget.addEventListener) {
		oTarget.addEventListener(sEventType, fnHandler, false);
	} else if (oTarget.attachEvent) {
		oTarget.attachEvent("on" + sEventType, fnHandler);
	} else {
		oTarget["on" + sEventType] = fnHandler;
	}
};
function removeEventHandler(oTarget, sEventType, fnHandler) {
	if (oTarget.removeEventListener) {
		oTarget.removeEventListener(sEventType, fnHandler, false);
	} else if (oTarget.detachEvent) {
		oTarget.detachEvent("on" + sEventType, fnHandler);
	} else {
		oTarget["on" + sEventType] = null;
	}
}; // 拖放程序
var Drag = Class.create();
Drag.prototype = { // 拖放对象
	initialize : function(drag, options) {
		this.Drag = $(drag);// 拖放对象
		this._x = this._y = 0;// 记录鼠标相对拖放对象的位置
		this._marginLeft = this._marginTop = 0;// 记录margin //事件对象(用于绑定移除事件)
		this._fM = BindAsEventListener(this, this.Move);
		this._fS = Bind(this, this.Stop);
		this.SetOptions(options);
		this.Limit = !!this.options.Limit;
		this.mxLeft = parseInt(this.options.mxLeft);
		this.mxRight = parseInt(this.options.mxRight);
		this.mxTop = parseInt(this.options.mxTop);
		this.mxBottom = parseInt(this.options.mxBottom);
		this.LockX = !!this.options.LockX;
		this.LockY = !!this.options.LockY;
		this.Lock = !!this.options.Lock;
		this.onStart = this.options.onStart;
		this.onMove = this.options.onMove;
		this.onStop = this.options.onStop;
		this._Handle = $(this.options.Handle) || this.Drag;
		this._mxContainer = $(this.options.mxContainer) || null; // setZB("bgDiv","div");
		this.Drag.style.position = "absolute"; // 透明
		if (isIE && !!this.options.Transparent) { // 填充拖放对象
			with (this._Handle.appendChild(document.createElement("div")).style) {
				width = height = "100%";
				backgroundColor = "#fff";
				filter = "alpha(opacity:0)";
			}
		} // 修正范围
		this.Repair();
		addEventHandler(this._Handle, "mousedown", BindAsEventListener(this,
				this.Start));
	}, // 设置默认属性
	SetOptions : function(options) {
		this.options = {// 默认值
			Handle : "",// 设置触发对象(不设置则使用拖放对象)
			Limit : false,// 是否设置范围限制(为true时下面参数有用,可以是负数)
			mxLeft : 0,// 左边限制
			mxRight : 9999,// 右边限制
			mxTop : 0,// 上边限制
			mxBottom : 9999,// 下边限制
			mxContainer : "",// 指定限制在容器内
			LockX : false,// 是否锁定水平方向拖放
			LockY : false,// 是否锁定垂直方向拖放
			Lock : false,// 是否锁定
			Transparent : false,// 是否透明
			onStart : function() {
			},// 开始移动时执行
			onMove : function() {
			},// 移动时执行
			onStop : function() {
			}// 结束移动时执行
		};
		Extend(this.options, options || {});
	}, // 准备拖动
	Start : function(oEvent) {
		if (this.Lock) {
			return;
		}
		this.Repair(); // 记录鼠标相对拖放对象的位置
		this._x = oEvent.clientX - this.Drag.offsetLeft;
		this._y = oEvent.clientY - this.Drag.offsetTop; // 记录margin
		this._marginLeft = parseInt(CurrentStyle(this.Drag).marginLeft) || 0;
		this._marginTop = parseInt(CurrentStyle(this.Drag).marginTop) || 0; // mousemove时移动
		// mouseup时停止
		addEventHandler(document, "mousemove", this._fM);
		addEventHandler(document, "mouseup", this._fS);
		if (isIE) { // 焦点丢失
			addEventHandler(this._Handle, "losecapture", this._fS); // 设置鼠标捕获
			this._Handle.setCapture();
		} else { // 焦点丢