純JS實現可拖拽表單

轉載註明出處!!!javascript

轉載註明出處!!!css

轉載註明出處!!!html

由於要用到可拖拽表單,我的要比較喜歡本身動手,不怎麼喜歡在不懂實現或者原理的狀況下用插件,因此查找資料實現了一個。java

思路:放入:用mousedown判斷鼠標點擊的位置是否在觸發控件的位置,若是是,mousemove的時候clone一個控件,修改透明度,而後放入容器內的時候remove這個控件,而且在容器內生成一個放入的控件(放入的控件和觸發的控件能夠不同)web

拖拽:一樣的, mousedown的時候判斷是哪一個控件,mousemove的時候須要放一個佔位div放在原有的位置上,並將元素修改透明度而後設置爲絕對定位方便拖動,在進行拖動的時候,佔位div也會跟着鼠標位置進行改變(判斷當前鼠標位置是不是容器內控件的左上角加上控件高度的一半),放下的時候進行判斷佔位div的位置進行插入。具體看代碼吧,這個思路加上的時間距離代碼實現的時間有點久遠了,全部可能不是很準確,可是大概的思路就是這樣了。數據庫

ps:要用到拖拽表單功能的,基本上可能都會要更改以往設計數據庫方式,這裏能夠提供給大家一個搜索關鍵詞 表的縱向存儲app

註釋基本上都已經寫的很詳細了,直接上代碼吧。ide

有什麼問題請大神們多多指教函數

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <title>Test</title>
 5         <style type="text/css" >
 6             html,body
 7             {
 8                 height:100%;
 9                 width:100%;
10                 padding:0;
11                 margin:0;
12             }
13             .dialog
14             {
15              /*   width:250px;
16                 height:250px;*/
17                 position:absolute;
18                 background-color:#ccc;
19                 -webkit-box-shadow:1px 1px 3px #292929;
20                 -moz-box-shadow:1px 1px 3px #292929;
21                 box-shadow:1px 1px 3px #292929;
22                 /*margin:10px;*/
23                 top:50px;
24                 left: 50px;
25                 opacity:1;
26             }
27             .dialog-title
28             {
29                 color:#fff;
30                 background-color:#404040;
31                 font-size:12pt;
32                 font-weight:bold;
33                 padding:4px 6px;
34                 cursor:move;
35                 position:absolute;
36                 top:50px;
37                 left: 50px;
38             }
39             
40             .dialog-content
41             {
42                 padding:4px;
43                 cursor:move;
44             }
45             .none{
46                 display: none;
47             }
48             .box{
49                 width: 200px;
50                 height: 500px;
51                 background-color: gray;
52                 line-height: 30px;
53                 margin: 100px;
54             }
55             .place{
56                 width: 200px;
57                 height: 50px;
58                 border: 1px dashed red;
59             }
60         </style>
61         <script type="text/javascript" src="js/devWin.js"></script>
62     </head>
63     <body>
64         <!-- <div id="dlgTest" class="dialog"> -->
65             <div id = "title" class="dialog-title">Dialog</div>
66             <div id = "title2" class="dialog-title" style ="top:10px">Dialog</div>
67         <!-- </div> -->
68         <a id = "a" href="#">123</a>
69         <input id = "text" type="text" class = "none">
70         <div id = "box" class = "box">
71        <!-- <input type="" name="" placeholder=""> -->
72         </div>
73         <div class = "place"></div>
74     </body>
75     <script type="text/javascript">
76         //要傳入的變量
77         //點擊觸發的對象 
78         var click = document.getElementById("title");
79         var click2 = document.getElementById("title2");
80         //放入的容器
81         var box = document.getElementById("box");
82         //容器內佔位的DIV
83         var place = document.createElement("div");
84         place.className = "place";
85         //往容器內添加的對象
86         var create = document.createElement("input");
87         create.type = "text";
88         var create2 = document.createElement("input");
89         create2.type = "password";
90         //是否能夠添加多個
91         var isMany = true;
92         createWin(click,create,isMany,place,box);
93         createWin(click2,create2,isMany,place,box);
94     </script>
95 </html>
HTML代碼
  1 /**
  2 * author:lzh 
  3 * 做用:可拖拽排序表單實現
  4 * 參數:oclick     點擊觸發事件的對象
  5 *      ocreate    出發後在表單中傳入的對象
  6 *      bisMany    單個oclick對象是否可拖入多個ocreate對象
  7 *      oplace     拖入時佔位div對象
  8 *      obox       拖入的容器,不填則默認爲body
  9 */
 10 function createWin(oclick,ocreate,bisMany,oplace,obox=document.body)
 11 {
 12     //是否點擊了觸發對象
 13     var isClick = false;
 14     //觸發對象是否爲容器內的元素
 15     var isIncludeBox = false;
 16     oplace.style.width = obox.offsetWidth-5 + "px";
 17     oplace.style.height = oclick.offsetHeight-5 + "px";
 18     //移動效果的臨時元素
 19     var oclickClone;
 20     var oclickDown;
 21     //計算偏移量
 22     var diffObj;
 23     var diffX;
 24     var diffY;
 25     var tmp;
 26     var omove_position;
 27     //點是否包含在容器內
 28     function isInclude(x,y,includeBox=obox)
 29     {
 30         if(x  > includeBox.offsetLeft 
 31         && y > includeBox.offsetTop 
 32         && x < includeBox.offsetLeft + includeBox.offsetWidth
 33         && y < includeBox.offsetTop + includeBox.offsetHeight)
 34             return true;
 35         return false;
 36     }
 37     //移動效果函數
 38     function moveMagic(omove,e)
 39     {
 40         // omove_position = window.getComputedStyle(omove).getPropertyValue('position');
 41         omove.style.opacity = 0.4;
 42         omove.style.position = "absolute";
 43         document.body.appendChild(omove);
 44         omove.style.left = e.clientX-diffX+"px";
 45         omove.style.top = e.clientY-diffY+"px";
 46     }
 47     function getdiff(e)
 48     {
 49         diffObj = e.target;
 50         diffX = e.clientX-diffObj.offsetLeft;
 51         diffY = e.clientY-diffObj.offsetTop;
 52     }
 53     //鼠標按下事件
 54     function down(e)
 55     {
 56         if(isInclude(e.clientX,e.clientY,oclick))
 57         {
 58             isClick = true;
 59             //克隆點擊的觸發節點
 60             oclickClone=oclick.cloneNode(true);
 61             //計算鼠標的偏移量(若是有margin的話會有偏移現象)
 62             getdiff(e);
 63         }
 64         else
 65         {
 66             getdiff(e);
 67             var child = obox.childNodes;
 68             for(var i=0; i < child.length; i++)
 69             {
 70                 //判斷鼠標點擊是不是容器內的元素,而且不能是佔位div(若是不加這個佔位div判斷會有bug,具體緣由不知道)
 71                 if(isInclude(e.clientX,e.clientY,child[i])&& child[i] != oplace)
 72                 {
 73                     isClick = true;
 74                     isIncludeBox = true;
 75                     //克隆元素用來拖動時的效果
 76                     oclickClone = child[i].cloneNode(true);
 77                     //克隆元素用來放下
 78                     oclickDown = child[i].cloneNode(true);
 79                     //按下以後刪除元素,並使用移動效果來展現元素
 80                     obox.removeChild(child[i]);
 81                     moveMagic(oclickClone,e);
 82                     //插入佔位div來弄
 83                     obox.insertBefore(oplace,child[i]);
 84                     // flag = true;
 85                     break;
 86                 }
 87             }
 88         }
 89     }
 90     //鼠標移動事件
 91     function move(e)
 92     {
 93         if(isClick)
 94         {
 95             moveMagic(oclickClone,e);
 96             //判斷鼠標是否移動到了容器內部
 97             if(isInclude(e.clientX,e.clientY,obox))
 98             {
 99                 //計算容器內的子節點
100                 var child = obox.childNodes;
101                 //一旦進入就能夠在首位置插入佔位DIV
102                 obox.insertBefore(oplace,child[0]);
103                 //根據鼠標位置放置佔位DIV
104                 for(var i = 0; i < child.length; i++)
105                 {
106                     //由於佔位DIV是惟一的,因此只須要這樣判斷便可
107                     if(e.clientY > child[i].offsetTop+child[i].offsetHeight/2)
108                     {
109                         //判斷是否拖動到告終尾
110                         if(i != child.length-1)
111                             obox.insertBefore(oplace,child[i+1]);
112                         else
113                             obox.appendChild(oplace);
114                     }
115                 }
116             }
117         }
118     }
119     //鼠標擡起事件
120     function up(e)
121     {
122         isClick = false;
123         //鼠標擡起則能夠刪除臨時的拖動效果元素
124         document.body.removeChild(oclickClone);
125         //若是將元素放置到了容器內
126         if(isInclude(e.clientX,e.clientY))
127         {
128             var child = obox.childNodes;
129             //佔位div的位置
130             var insertPlace;
131             for(var i=0; i<child.length;i++)
132             {
133                 //肯定佔位div的位置
134                 if(oplace === child[i])
135                 {
136                     obox.removeChild(child[i]);
137                     insertPlace = i;
138                     break;
139                 }
140             }
141             //判斷是否能夠放置多個
142             if(!bisMany)
143             {
144                 if(isIncludeBox)
145                     ocreate = oclickDown;
146                 if(insertPlace==child.length)
147                     obox.appendChild(ocreate);
148                 else
149                     obox.insertBefore(ocreate,child[insertPlace]);
150             }
151             else
152             {
153                     //能夠放置多個則須要每一個都克隆一下
154                 if(isIncludeBox)
155                     var ocreateClone = oclickDown;
156                 else
157                     var ocreateClone = ocreate.cloneNode(true);
158                 if(insertPlace==child.length)
159                     obox.appendChild(ocreateClone);
160                 else
161                 {
162                     obox.insertBefore(ocreateClone,child[insertPlace]);
163                 }
164             }
165         }
166         else
167         {
168             if(isIncludeBox)
169             {
170                 var child  = obox.childNodes;
171                 for(var i=0; i<child.length; i++)
172                 {
173                     if(child[i] === oplace)
174                     {
175                         obox.removeChild(child[i]);
176                         obox.insertBefore(oclickDown,child[i]);
177                     }1
178                 }
179             }
180         }
181         isIncludeBox = false;
182     }
183     document.addEventListener('mousemove',move);
184     document.addEventListener('mousedown',down);
185     document.addEventListener('mouseup',up);
186 }
JavaScript代碼
相關文章
相關標籤/搜索