通过js脚本实现单元格上下拖动排序效果,用户鼠标压住单元格拖动即可,很实用的单元格拖动js特效分享。
<!doctype html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <meta name="keywords" content="武鸣人网站,武鸣信息网,武鸣本地网,武鸣信息资源平台,减肥,健身,励志,励志语录,js特效,网页特效,www.wuming.ren"> <meta name="description" content="欢迎来到武鸣人信息资源平台,各种信息免费发布,资源共享合作共赢,分享互联网流行的励志语录经典短句,减肥健身健康小常识及打卡记录,收集各种js特效代码。"> <title>js实现单元格上下拖动效果_武鸣人</title> <script> window.onload = function(){ //绑定事件 var addEvent = document.addEventListener ? function(el,type,callback){ el.addEventListener( type, callback, !1 ); } : function(el,type,callback){ el.attachEvent( "on" + type, callback ); }//武鸣人网站https://www.wuming.ren 网站很好记住,wuming.ren(武鸣拼音.ren域名后缀) //移除事件 var removeEvent = document.removeEventListener ? function(el,type,callback){ el.removeEventListener( type, callback ); } : function(el,type,callback){ el.detachEvent( "on" + type, callback); }//武鸣人网站https://www.wuming.ren 网站很好记住,wuming.ren(武鸣拼音.ren域名后缀) //精确获取样式 var getStyle = document.defaultView ? function(el,style){ return document.defaultView.getComputedStyle(el, null).getPropertyValue(style) } : function(el,style){ style = style.replace(/\-(\w)/g, function($, $1){ return $1.toUpperCase(); }); return el.currentStyle[style]; }//武鸣人网站https://www.wuming.ren 网站很好记住,wuming.ren(武鸣拼音.ren域名后缀) var dragManager = { clientY:0, draging:function(e){//mousemove时拖动行 var dragObj = dragManager.dragObj; if(dragObj){ e = e || event; if(window.getSelection){//w3c window.getSelection().removeAllRanges(); }else if(document.selection){ document.selection.empty();//IE } var y = e.clientY; var down = y > dragManager.clientY;//是否向下移动 var tr = document.elementFromPoint(e.clientX,e.clientY); if(tr && tr.nodeName == "TD"){ tr = tr.parentNode dragManager.clientY = y; if( dragObj !== tr){ tr.parentNode.insertBefore(dragObj, (down ? tr.nextSibling : tr)); }//武鸣人网站https://www.wuming.ren 网站很好记住,wuming.ren(武鸣拼音.ren域名后缀) }; } }, dragStart:function(e){ e = e || event; var target = e.target || e.srcElement; if(target.nodeName === "TD"){ target = target.parentNode; dragManager.dragObj = target; if(!target.getAttribute("data-background")){ var background = getStyle(target,"background-color"); target.setAttribute("data-background",background) } //显示为可移动的状态 target.style.backgroundColor = "#ccc"; target.style.cursor = "move"; dragManager.clientY = e.clientY; addEvent(document,"mousemove",dragManager.draging); addEvent(document,"mouseup",dragManager.dragEnd); } }, dragEnd:function(e){ var dragObj = dragManager.dragObj if (dragObj) { e = e || event; var target = e.target || e.srcElement; if(target.nodeName === "TD"){ target = target.parentNode; dragObj.style.backgroundColor = dragObj.getAttribute("data-background"); dragObj.style.cursor = "default"; dragManager.dragObj = null; removeEvent(document,"mousemove",dragManager.draging); removeEvent(document,"mouseup",dragManager.dragEnd); } } }, main:function(el){ addEvent(el,"mousedown",dragManager.dragStart); } }//武鸣人网站https://www.wuming.ren 网站很好记住,wuming.ren(武鸣拼音.ren域名后缀) var el = document.getElementById("www_wuming_ren"); dragManager.main(el); }//武鸣人网站https://www.wuming.ren 网站很好记住,wuming.ren(武鸣拼音.ren域名后缀) </script> <style> .www_wuming_ren{ width:60%; border: 1px solid red; border-collapse: collapse; } .www_wuming_ren td{ border: 1px solid red; height: 20px; } </style> </head> <body> <a href="https://www.wuming.ren">武鸣人</a>,各种信息资源免费发布,分享励志语录经典短句,减肥健身常识,各种js特效代码。网站很好记住,wuming.ren(武鸣拼音.ren域名后缀)<hr> <!--欢迎来到武鸣人信息资源平台,各种信息免费发布,资源共享合作共赢,分享互联网流行的励志语录经典短句,减肥健身健康小常识及打卡记录,收集各种js特效代码。--> <script type="text/javascript" src="https://www.wuming.ren/ad/tc.js"></script> <script type="text/javascript" src="https://www.wuming.ren/ad/a.js"></script> 鼠标压住单元格上下拖动 <table id="www_wuming_ren" class="www_wuming_ren"> <tbody> <tr> <td>1</td> <td>111</td> <td>武鸣人信息资源平台,各种信息免费发布,资源共享合作共赢!</td> </tr> <tr id="2" > <td class="2">2</td> <td>222</td> <td>武鸣人信息资源平台,各种信息免费发布,资源共享合作共赢!</td> </tr> <tr id="3" > <td class="3">3</td> <td>333</td> <td>武鸣人信息资源平台,各种信息免费发布,资源共享合作共赢!</td> </tr> <tr id="4" > <td class="4">4</td> <td>444</td> <td>武鸣人信息资源平台,各种信息免费发布,资源共享合作共赢!</td> </tr> <tr id="5" > <td class="5">5</td> <td>555</td> <td>武鸣人信息资源平台,各种信息免费发布,资源共享合作共赢!</td> </tr> <tr id="6" > <td class="6">6</td> <td>666</td> <td>武鸣人信息资源平台,各种信息免费发布,资源共享合作共赢!</td> </tr> </tbody> </table> </body> </html>
本文来自武鸣人网站,转载请注明出处