复制代码代码如下:
<! Doctype html public "-// w3c // dtd xhtml 1.0 transitional // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<голова>
<meta http-equiv = "content-type" content = "text /html; charset = utf-8" />
<title> 拖拽库 </title>
<стиль типа = "text/css">
div, h2, p {margin: 0; падки: 0;}
Body {Font: 14px/1,5 Arial;}
#box {ширина: 100px; высота: 100px; фон: #fef4eb; Padding: 5px; маржа: 50px; граница: 1px sold #f60;}
#box .title {eight: 25px; фон:#f60;}
#tool {margin-bothom: 10px;}
</style>
<script type = "text/javascript">
функция Drag ()
{
// 初始化
this.initialize.apply (это, аргументы)
}
Drag.prototype = {
// 初始化
Инициализировать: функция (перетаскивание, параметры)
{
this.drag = this. $ (drag);
this._x = this._y = 0;
this._movedrag = this.bind (this, this.movedrag);
this._stopdrag = this.bind (this, this.stopdrag);
this.setoptions (опции);
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: функция (событие)
{
var Event = Event || window.event;
this._x = event.clientx - this.drag.offsetleft;
this._y = event.clienty - this.drag.offsettop;
это.
this.addhandler (документ, "Mouseup", this._stopdrag);
event.preventdefault && event.preventDefault ();
this.handle.setCapture && this.handle.setCapture ();
this.onstart ()
},
MovelRag: функция (событие)
{
var Event = Event || window.event;
var itop = event.clienty - this._y;
var Ileft = event.clientx - this._x;
if (this.lock) return;
this.limit && (itop <0 && (itop = 0), ileft <0 && (ileft = 0), itop> this.maxtop && (itop = this.maxtop), ileft> this.maxleft && (ilft = this. maxleft));
this.locky || (this.drag.style.top = itop + "px");
this.lockx || (this.drag.style.left = ilft + "px");
event.preventdefault && event.preventDefault ();
this.onmove ()
},
stopdrag: function ()
{
this.removehandler (документ, "MouseMove", this._movedrag);
this.removehandler (документ, "MouseUp", this._stopdrag);
this.handle.releasecapture && this.handle.releasecapture ();
this.onstop ()
},
// 参数设置
setoptions: function (options)
{
this.options =
{
Ручка: this.drag, // 事件对象
Предел: верно, // 锁定范围
Замок: ложь, // 锁定位置
Lockx: false, // 锁定水平位置
Локки: Неверно, // 锁定垂直位置
maxcontainer: document.documentelement || Document.body, // 指定限制容器
OnStart: function () {}, // 开始时回调函数
onmove: function () {}, // 拖拽时回调函数
Onstop: function () {} // 停止时回调函数
};
для (var p в параметрах) this.options [p] = параметры [p]
},
// 获取 ID
$: функция (ID)
{
return typeof id === "String"? Document.GetElementByID (ID): ID
},
// 添加绑定事件
addHandler: функция (oelement, Seventype, Fnhandler)
{
return oelement.addeventlistener? oelement.addeventlistener (Seventype, Fnhandler, false): oelement.attachevent ("on" + Seventtype, fnhandler)
},
// 删除绑定事件
Удалить handherler: function (oelement, Seventype, fnhandler)
{
return oelement.removeeventlistener? oelement.removeeventListener (SeventType, Fnhandler, false): oelement.detachevent ("on" + Seventype, fnhandler)
},
// 绑定事件到对象
Bind: function (Object, fnhandler)
{
return function ()
{
вернуть fnhandler.apply (объект, аргументы)
}
}
};
// 应用
window.onload = function ()
{
var obox = document.getElementbyId ("box");
var otitle = obox.getElementsbytagname ("h2") [0];
var ospan = document.getelementsbytagname ("span") [0];
var odrag = new Drag (obox, {handle: Otitle, предел: 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 = "слева:" + this.drag.offsetleft + ", top:" + this.drag.offsettop
};
// 开始拖拽时方法
odrag.onstop = function ()
{
ospan.innerhtml = "结束拖拽"
};
};
</script>
</head>
<тело>
<div id = "Tool">
<input type = "button" value = "锁定范围" />
<input type = "button" value = "水平锁定" />
<input type = "button" value = "垂直锁定" />
<input type = "button" value = "锁定位置" />
</div>
<p> 拖放状态 : <pan> 未开始 </span> </p>
<div id = "box">
<h2> </h2>
</div>
</body>
</html>
</td>
</tr>
</table>