javascript 公用拖拽类代码
D类
html, body {
margin:0;
}
!!window.__defineGetter__ && !/MSIE/.test(navigator.userAgent) && function () {
!window.opera && window.__defineGetter__('event', function () {
//兼容Event对象
var o = arguments.callee;
do {
if (o.arguments[0] instanceof Event) return o.arguments[0];
} while (o = o.caller);
return null;
});
window.attachEvent = Document.prototype.attachEvent = Element.prototype.attachEvent = function (type, listener, capture) {
//兼容attachEvent方法
return this.addEventListener(new String(type).substr(2), listener, capture || false);
};
window.detachEvent = Document.prototype.detachEvent = Element.prototype.detachEvent = function (type, listener, capture) {
//兼容detachEvent方法
return this.removeEventListener(new String(type).substr(2), listener, capture || false);
};
}();
var D = {
//拽补助类
lock : false
, dom : function () {
//document相关属性
return {
left : document.documentElement.scrollLeft
, top : document.documentElement.scrollTop
, width : document.documentElement.clientWidth
, height : document.documentElement.clientHeight
, body : document.documentElement
};
}
, mos : function (e) {
//获取鼠标绝对位置
var dom = this.dom();
return { 'x' : dom.left + e.clientX, 'y' : dom.top + e.clientY }
}
, pos : function (o) {
//获取元素绝对位置
var x = 0, y = 0;
do { x += o.offsetLeft, y += o.offsetTop; } while (o = o.offsetParent);
return { 'x' : x, 'y' : y };
}
, start : function (element, startEventHandler, moveEventHandler, stopEventHandler) {
//移动开始
if (this.lock) return;
else this.lock = true; //先上锁安全。。。:D
var pos = this.pos(element) //元素位置
, mos = this.mos(window.event) //鼠标位置
, eventHandlers = { MF : null, EF : null } //事件记录
, property = { x : mos.x - pos.x, y : mos.y - pos.y, left : pos.x, top : pos.y } //属性
, _MF = this.move(moveEventHandler, property) //移动过程事件闭包
, _EF = this.stop(element, stopEventHandler, eventHandlers); //移动停止事件闭包
try { document.selection && document.selection.empty && (document.selection.empty(), 1) || window.getSelection && window.getSelection().removeAllRanges(); } catch (exp) {}
document.attachEvent('onmousemove', _MF); //鼠标移动
document.attachEvent('onmouseup', _EF); //鼠标释放
element.setCapture
? (element.setCapture(), element.attachEvent('onlosecapture', _EF))
: window.attachEvent('onblur', _EF); //鼠标捕获丢失则释放
eventHandlers.MF = _MF, eventHandlers.EF = _EF;
startEventHandler && startEventHandler(property); //直接传过去。。。请误污染- -
}
, move : function (moveEventHandler, property) {
//移动中
var wc = this;
return function (e) {
var mos = wc.mos(e || window.event), dom = wc.dom();
window.getSelection && window.getSelection().removeAllRanges();
/MSIE/.test(navigator.userAgent) && function () {
//IE滚屏
if (mos.x > dom.left + dom.width) dom.body.scrollLeft = mos.x - dom.width;
else if (mos.x dom.top + dom.height) dom.body.scrollTop = mos.y - dom.height;
else if (mos.y
拽
拽
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]