以前在網上看到的最簡單的拖動對象的代碼忘記作者叫什麼了原始代碼在IE下有些小問題並且聲明了文檔類型為xhtml 後在FF等非IE浏覽器下無效對其進行了改進現在已經可兼容:IEFirefoxOpera
以下代碼只是演示原理具體應用請結合你自己的實際需求進行修改
<!doctype html public
//W
C//DTD XHTML
Transitional//EN
transitional
dtd
>
<html xmlns=
lang=
gb
>
<head>
<title> 代碼實例
拖動對象 Drag Object (兼容:IE
Firefox
Opera
)</title>
<meta http
equiv=
content
type
content=
text/html; charset=gb
/>
<meta name=
editor
content=
楓巖
>
<meta name=
keywords
content=
代碼實例
拖動對象
>
<meta name=
description
content=
最簡短的拖動對象代碼實例演示
>
<style>
dragAble {position:relative;cursor:move;}
</style>
<script language=
javascript
>
<!
// Author: Unkonw
// Modify: 楓巖
var ie=document
all;
var nn
=document
getElementById&&!document
all;
var isdrag=false;
var y
x;
var oDragObj;
function moveMouse(e) {
if (isdrag) {
oDragObj
style
top = (nn
? nTY + e
clientY
y : nTY + event
clientY
y)+
px
;
oDragObj
style
left = (nn
? nTX + e
clientX
x : nTX + event
clientX
x)+
px
;
return false;
}
}
function initDrag(e) {
var oDragHandle = nn
? e
target : event
srcElement;
var topElement =
HTML
;
while (oDragHandle
tagName != topElement && oDragHandle
className !=
dragAble
) {
oDragHandle = nn
? oDragHandle
parentNode : oDragHandle
parentElement;
}
if (oDragHandle
className==
dragAble
) {
isdrag = true;
oDragObj = oDragHandle;
nTY = parseInt(oDragObj
style
top+
);
y = nn
? e
clientY : event
clientY;
nTX = parseInt(oDragObj
style
left+
);
x = nn
? e
clientX : event
clientX;
document
onmousemove=moveMouse;
return false;
}
}
document
onmousedown=initDrag;
document
onmouseup=new Function(
isdrag=false
);
//
>
</script>
</head>
<body>
<img src=
_
/
/
gif
class=
dragAble
/>
<img src=
_
/
/
gif
class=
dragAble
/>
<img src=
_
/
/
gif
class=
dragAble
/><br /><br />
<div class=
dragAble
>這些都是可拖動對象</div>
</body>
</html>
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19506.html