JS實現拖動對象的代碼

來源:互聯網
上載者:User
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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.