复制代码代码如下:
<! Doctype html public "-// w3c // dtd xhtml 1.0 transitional // pt" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-bransitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv = "content-type" content = "text /html; charset = utf-8" />
<Title> 拖拽库 </title>
<style type = "text/css">
div, h2, p {margem: 0; preenchimento: 0;}
corpo {font: 14px/1.5 Arial;}
#Box {Width: 100px; altura: 100px; fundo: #fef4eb; preenchimento: 5px; margem: 50px; borda: 1px sólido #f60;}
#box .title {altura: 25px; fundo:#f60;}
#Tool {Margin-Bottom: 10px;}
</style>
<script type = "text/javascript">
função arrastar ()
{
// 初始化
this.initialize.apply (isto, argumentos)
}
Drag.prototype = {
// 初始化
Inicializar: função (arrastar, opções)
{
this.drag = this. $ (arrasto);
this._x = this._y = 0;
this._MovedRag = this.bind (this, this.MovedRag);
this._stopdrag = this.bind (this, this.stopdrag);
this.setOptions (opções);
this.Handle = this. $ (this.options.handle);
this.maxContainer = this. $ (this.options.maxContainer);
this.maxtop = math.max (this.maxContainer.clientHeight, this.maxContainer.scrolHeight) - this.drag.offsetHeight;
this.maxleft = math.max (this.maxContainer.clientWidth, this.maxContainer.scrollwidth) - this.drag.offsetWidth;
this.limit = this.options.limit;
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.style.cursor = "move";
this.Changelayout ();
this.addhandler (this.handle, "mousedown", this.bind (this, this.startdrag))
},
Changelayout: function ()
{
this.drag.style.top = this.drag.offsettop + "px";
this.drag.style.left = this.drag.offsetleft + "px";
this.drag.style.Position = "Absolute";
this.drag.style.margin = "0"
},
StartDrag: função (evento)
{
Var Evento = Evento || Window.Event;
this._x = event.clientX - this.drag.offsetleft;
this._y = event.clienty - this.drag.offsettop;
this.addhandler (documento, "mousemove", this._movedRag);
this.addhandler (documento, "mouseup", this._stopdrag);
event.preventDefault && event.preventDefault ();
this.handle.setCapture && this.handle.setcapture ();
this.onstart ()
},
Moverag: function (evento)
{
Var Evento = Evento || Window.Event;
var itop = event.clienty - this._y;
var iLeft = event.clientX - this._x;
if (this.lock) retornar;
this.limit && (itop <0 && (itop = 0), iLeft <0 && (ileft = 0), itop> this.maxtop && (itop = this.maxtop), iLeft> this.maxleft && (iLeft = this. maxleft));
this.locky || (this.drag.style.top = itop + "px");
this.lockx || (this.drag.style.left = iLeft + "px");
event.preventDefault && event.preventDefault ();
this.onmove ()
},
StopDrag: function ()
{
this.RemoveHandler (documento, "mousemove", this._movedRag);
this.RemoveHandler (documento, "mouseup", this._stopdrag);
this.Handle.ReleasecApture && this.handle.releaseCapture ();
this.onstop ()
},
// 参数设置
Setopions: function (opções)
{
this.options =
{
Handle: this.drag, // 事件对象
Limite: Verdadeiro, // 锁定范围
Bloqueio: Falso, // 锁定位置
Lockx: false, // 锁定水平位置
Locky: Falso, // 锁定垂直位置
maxContainer: document.documentElement || document.body, // 指定限制容器
OnStart: function () {}, // 开始时回调函数
OnMove: function () {}, // 拖拽时回调函数
ONSTOP: function () {} // 停止时回调函数
};
para (var p nas opções) this.options [p] = opções [p]
},
// 获取 id
$: function (id)
{
Retornar o tipoof id === "String"? Document.getElementById (ID): ID
},
// 添加绑定事件
AddHandler: function (oElement, SeventType, FNHandler)
{
retornar oElement.AddeventListener? OELEMENT.ADDeventListener (SeventType, FNHandler, False): OELEMENT.ATCACHEVENT ("ON" + SeventType, FNHandler)
},
// 删除绑定事件
Remonendler: function (oelement, seventtype, fnhandler)
{
retornar oElement.RemoveEventListener? OELEMENT.RemoveEventListener (SeventType, FNHandler, False): OELEMENT.DETACHEVENT ("ON" + SeventType, FNHandler)
},
// 绑定事件到对象
Bind: function (objeto, fnhandler)
{
função de retorno ()
{
Retorne fnhandler.apply (objeto, argumentos)
}
}
};
// 应用
window.onload = function ()
{
var obox = document.getElementById ("caixa");
var otitle = obox.getElementsByTagName ("h2") [0];
var ospan = document.getElementsByTagName ("span") [0];
var odrag = novo arrasto (obox, {handle: otitle, limite: false});
var ainput = document.getElementsByTagName ("input");
// 锁定范围接口
Ainput [0] .OnClick = function ()
{
odrag.limit =! odrag.limit;
this.value = odrag.limit? "取消锁定范围": "锁定范围"
};
// 水平锁定接口
Ainput [1] .OnClick = function ()
{
odrag.lockx =! odrag.lockx;
this.value = odrag.lockx? "取消水平锁定": "水平锁定"
};
// 垂直锁定接口
Ainput [2] .OnClick = function ()
{
odrag.locky =! odrag.locky;
this.value = odrag.locky? "取消垂直锁定": "垂直锁定"
};
// 锁定位置接口
Ainput [3] .OnClick = function ()
{
odrag.lock =! odrag.lock;
this.value = odrag.lock? "取消锁定位置": "锁定位置"
};
// 开始拖拽时方法
odrag.onstart = function ()
{
ospan.innerhtml = "开始拖拽"
};
// 开始拖拽时方法
odrag.onmove = function ()
{
ospan.innerhtml = "esquerda:" + this.drag.offsetleft + ", top:" + this.drag.offsettop
};
// 开始拖拽时方法
odrag.onstop = function ()
{
ospan.innerhtml = "结束拖拽"
};
};
</script>
</head>
<Body>
<div id = "ferramenta">
<input type = "button" value = "锁定范围" />
<input type = "button" value = "水平锁定" />
<input type = "button" value = "垂直锁定" />
<input type = "button" value = "锁定位置" />
</div>
<p> 拖放状态 : <span> 未开始 </span> </p>
<div id = "Box">
<H2> </h2>
</div>
</body>
</html>
</td>
</tr>
</tabela>