复制代码代码如下:
<! Doctype html public "-// w3c // dtd xhtml 1.0 transisi // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv = "konten tipe" content = "text /html; charset = utf-8" />
<title> 拖拽库 </iteme>
<type style = "text/css">
Div, h2, p {margin: 0; padding: 0;}
tubuh {font: 14px/1.5 Arial;}
#box {width: 100px; tinggi: 100px; latar belakang: #fef4eb; padding: 5px; margin: 50px; Border: 1px solid #f60;}
#box .title {height: 25px; latar belakang:#f60;}
#tool {margin-bottom: 10px;}
</tyle>
<type skrip = "Teks/JavaScript">
function drag ()
{
// 初始化
this.initialize.apply (ini, argumen)
}
Drag.prototype = {
// 初始化
Inisialisasi: Fungsi (seret, opsi)
{
this.drag = this. $ (seret);
this._x = this._y = 0;
this._movedrag = this.bind (this, this.movedrag);
this._stopdrag = this.bind (this, this.stopdrag);
this.setOptions (opsi);
this.handle = this. $ (this.options.handle);
this.maxcontainer = this. $ (this.options.maxcontainer);
this.maxtop = math.max (this.maxcontainer.clientHeight, this.maxcontainer.scrollHeight) - 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: Fungsi (acara)
{
var event = event || window.event;
this._x = event.clientx - this.drag.offsetleft;
this._y = event.clienty - this.drag.offsettop;
this.addhandler (dokumen, "mousemove", this._movedrag);
this.addhandler (dokumen, "mouseup", this._stopdrag);
event.preventdefault && event.preventdefault ();
this.handle.setCapture && this.handle.setCapture ();
this.onsart ()
},
MovingRag: Function (acara)
{
var event = event || window.event;
var itop = event.clienty - this._y;
var ileft = event.clientx - this._x;
if (this.lock) kembali;
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 (dokumen, "mousemove", this._movedrag);
this.removeHandler (dokumen, "mouseup", this._stopdrag);
this.handle.releasecapture && this.handle.releasecapture ();
this.onstop ()
},
// 参数设置
setOptions: fungsi (opsi)
{
this.options =
{
Tangani: this.drag, // 事件对象
Batas: Benar, // 锁定范围
Kunci: Salah, // 锁定位置
LOCKX: Salah, // 锁定水平位置
Locky: Salah, // 锁定垂直位置
MaxContainer: Document.DocumentElement || document.body, // 指定限制容器
onStart: function () {}, // 开始时回调函数
onmove: function () {}, // 拖拽时回调函数
Onstop: function () {} // 停止时回调函数
};
untuk (var p dalam opsi) this.options [p] = opsi [p]
},
// 获取 id
$: function (id)
{
return typeof id === "string"? document.geteLementById (id): id
},
// 添加绑定事件
AddHandler: Fungsi (OElement, SeventType, FNHandler)
{
return oelement.addeventlistener? oelement.addeventListener (SeventType, fnhandler, false): oelement.attachevent ("on" + SeventType, fnhandler)
},
// 删除绑定事件
Hapushandler: Fungsi (OElement, SeventType, FNHandler)
{
return oelement.removeeventlistener? oelement.removeeventListener (SeventType, fnhandler, false): oelement.detachevent ("on" + SeventType, fnhandler)
},
// 绑定事件到对象
Bind: Fungsi (objek, fnhandler)
{
fungsi kembali ()
{
return fnhandler.Apply (objek, argumen)
}
}
};
// 应用
window.onload = function ()
{
var obox = document.getElementById ("box");
var otitle = obox.getElementsbyTagname ("h2") [0];
var ospan = document.getElementsbyTagname ("span") [0];
var odrag = drag baru (obox, {handle: otitle, limit: false});
var input = document.geteLementsByTagname ("input");
// 锁定范围接口
input [0] .onClick = function ()
{
odrag.limit =! Odrag.limit;
this.value = odrag.limit? "取消锁定范围": "锁定范围"
};
// 水平锁定接口
input [1] .onClick = function ()
{
odrag.lockx =! odrag.lockx;
this.value = odrag.lockx? "取消水平锁定": "水平锁定"
};
// 垂直锁定接口
input [2] .onClick = function ()
{
odrag.locky =! odrag.locky;
this.value = odrag.locky? "取消垂直锁定": "垂直锁定"
};
// 锁定位置接口
input [3] .onClick = function ()
{
odrag.lock =! odrag.lock;
this.value = odrag.lock? "取消锁定位置": "锁定位置"
};
// 开始拖拽时方法
odrag.onstart = function ()
{
ospan.innerhtml = "开始拖拽"
};
// 开始拖拽时方法
odrag.onmove = function ()
{
ospan.innerhtml = "kiri:" + this.drag.offsetleft + ", atas:" + this.drag.offsettop
};
// 开始拖拽时方法
odrag.onstop = function ()
{
ospan.innerhtml = "结束拖拽"
};
};
</script>
</head>
<body>
<Div id = "Tool">
<input type = "tombol" value = "锁定范围" />
<input type = "tombol" value = "水平锁定" />
<input type = "tombol" value = "垂直锁定" />
<input type = "tombol" value = "锁定位置" />
</div>
<p> 拖放状态 : <span> 未开始 </span> </p>
<Div id = "box">
<h2> </h2>
</div>
</body>
</html>
</td>
</tr>
</boable>