JS實現拖動對象的代碼,珍藏已久的精典代碼,可以隨意把定義的表格拖動
1<SCRIPT LANGUAGE="JavaScript">...
2<!--
3var currentMoveObj = null; //當前拖動對象
4var relLeft; //滑鼠按下位置相對對象位置
5var relTop;
6function f_mdown(obj)
7...{
8 currentMoveObj = obj; //當對象被按下時,記錄該對象
9 currentMoveObj.style.position = "absolute";
10 relLeft = event.x - currentMoveObj.style.pixelLeft;
11 relTop = event.y - currentMoveObj.style.pixelTop;
12}
13window.document.onmouseup = function()
14...{
15 currentMoveObj = null; //當滑鼠釋放時同時釋放拖動對象
16}
17function f_move(obj)
18...{
19if(currentMoveObj != null)
20...{
21 currentMoveObj.style.pixelLeft=event.x-relLeft;
22 currentMoveObj.style.pixelTop=event.y-relTop;
23 }
24}
25
26//-->
27</SCRIPT>
28<BODY>
29<TABLE width="150" border=1 onselectstart="return false"
30 style="position:absolute;left:50;top:50" onmousedown="f_mdown(this)" onmousemove="f_move(this)">
31<TR>
32<TD bgcolor="#CCCCCC" align="center" style="cursor:move">www.web3.cn標題1</TD>
33</TR>
34<TR>
35<TD align="center" height="60">內容1</TD>
36</TR>
37</TABLE>
38<TABLE width="150" border=1 onselectstart="return false" style="position:absolute;left:350;top:250"
39 onmousedown="f_mdown(this)" onmousemove="f_move(this)">
40<TR>
41<TD bgcolor="#CCCCCC" align="center" style="cursor:move">www.web3.cn標題2</TD>
42</TR>
43<TR>
44<TD align="center" height="60">內容2</TD>
45</TR>
46</TABLE>
47</BODY>