




已阅读5页,还剩37页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
怎么用javascript进行拖拽本文译自:/programming/javascript/mk/column2/index.html所有版权归原文所有Javascript的特点是dom的处理与网页效果,大多数情况我们只用到了这个语言的最简单的功能,比如制作图片轮播/网页的tab等等,这篇文章将向你展示如何在自己的网页上制作拖拽有很多理由让你的网站加入拖拽功能,最简单的一个是数据重组例如:你有一个序列的内容让用户排序,用户需要给每个条目进行输入或者用select选择,替代前面这个方法的就是拖拽或许你的网站也需要一个用户可以拖动的导航窗口!那么这些效果都是很简单:因为你可以很容易的实现!网页上实现拖拽其实也不是很复杂第一你需要知道鼠标坐标,第二你需要知道用户鼠标点击一个网页元素并实现拖拽,最后我们要实现移动这个元素先看一个例子HTML代码:LI MARGIN-BOTTOM: 10pxOL MARGIN-TOP: 5px.DragContainer BORDER-RIGHT: #669999 2px solid; PADDING-RIGHT: 5px; BORDER-TOP: #669999 2px solid; PADDING-LEFT: 5px; FLOAT: left; PADDING-BOTTOM: 0px; MARGIN: 3px; BORDER-LEFT: #669999 2px solid; WIDTH: 100px; PADDING-TOP: 5px; BORDER-BOTTOM: #669999 2px solid.OverDragContainer BORDER-RIGHT: #669999 2px solid; PADDING-RIGHT: 5px; BORDER-TOP: #669999 2px solid; PADDING-LEFT: 5px; FLOAT: left; PADDING-BOTTOM: 0px; MARGIN: 3px; BORDER-LEFT: #669999 2px solid; WIDTH: 100px; PADDING-TOP: 5px; BORDER-BOTTOM: #669999 2px solid.OverDragContainer BACKGROUND-COLOR: #eee.DragBox BORDER-RIGHT: #000 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #000 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10px; MARGIN-BOTTOM: 5px; PADDING-BOTTOM: 2px; BORDER-LEFT: #000 1px solid; WIDTH: 94px; CURSOR: pointer; PADDING-TOP: 2px; BORDER-BOTTOM: #000 1px solid; FONT-FAMILY: verdana, tahoma, arial; BACKGROUND-COLOR: #eee.OverDragBox BORDER-RIGHT: #000 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #000 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10px; MARGIN-BOTTOM: 5px; PADDING-BOTTOM: 2px; BORDER-LEFT: #000 1px solid; WIDTH: 94px; CURSOR: pointer; PADDING-TOP: 2px; BORDER-BOTTOM: #000 1px solid; FONT-FAMILY: verdana, tahoma, arial; BACKGROUND-COLOR: #eee.DragDragBox BORDER-RIGHT: #000 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #000 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10px; MARGIN-BOTTOM: 5px; PADDING-BOTTOM: 2px; BORDER-LEFT: #000 1px solid; WIDTH: 94px; CURSOR: pointer; PADDING-TOP: 2px; BORDER-BOTTOM: #000 1px solid; FONT-FAMILY: verdana, tahoma, arial; BACKGROUND-COLOR: #eee.miniDragBox BORDER-RIGHT: #000 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #000 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10px; MARGIN-BOTTOM: 5px; PADDING-BOTTOM: 2px; BORDER-LEFT: #000 1px solid; WIDTH: 94px; CURSOR: pointer; PADDING-TOP: 2px; BORDER-BOTTOM: #000 1px solid; FONT-FAMILY: verdana, tahoma, arial; BACKGROUND-COLOR: #eee.OverDragBox BACKGROUND-COLOR: #ffff99.DragDragBox BACKGROUND-COLOR: #ffff99.DragDragBox FILTER: alpha(opacity=50); BACKGROUND-COLOR: #ff99ccLEGEND FONT-WEIGHT: bold; FONT-SIZE: 12px; COLOR: #666699; FONT-FAMILY: verdana, tahoma, arialFIELDSET PADDING-RIGHT: 3px; PADDING-LEFT: 3px; PADDING-BOTTOM: 3px; PADDING-TOP: 3px.History FONT-SIZE: 10px; OVERFLOW: auto; WIDTH: 100%; FONT-FAMILY: verdana, tahoma, arial; HEIGHT: 82px#DragContainer8 BORDER-RIGHT: #669999 1px solid; PADDING-RIGHT: 0px; BORDER-TOP: #669999 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 0px; BORDER-LEFT: #669999 1px solid; WIDTH: 110px; PADDING-TOP: 5px; BORDER-BOTTOM: #669999 1px solid; HEIGHT: 110px.miniDragBox FLOAT: left; MARGIN: 0px 5px 5px 0px; WIDTH: 20px; HEIGHT: 20pxPRE BORDER-RIGHT: #ccc 1px solid; PADDING-RIGHT: 10px; BORDER-TOP: #ccc 1px solid; PADDING-LEFT: 10px; PADDING-BOTTOM: 10px; BORDER-LEFT: #ccc 1px solid; PADDING-TOP: 10px; BORDER-BOTTOM: #ccc 1px solid; BACKGROUND-COLOR: #f8f8f0样例- 拖拽页面元素 Item #1 Item #2 Item #3 Item #4 Item #5 Item #6 Item #7 Item #8 Item #9 Item #10 Item #11 Item #12/articleimg/2006/07/3791/drag_drop.js代码:var Demos = ;var nDemos = 8;/ Demo variables/ iMouseDown represents the current mouse button state: up or down/*lMouseState represents the previous mouse button state so that we cancheck for button clicks and button releases:if(iMouseDown & !lMouseState) / button just clicked!if(!iMouseDown & lMouseState) / button just released!*/var mouseOffset = null;var iMouseDown = false;var lMouseState = false;var dragObject = null;/ Demo 0 variablesvar DragDrops = ;var curTarget = null;var lastTarget = null;var dragHelper = null;var tempDiv = null;var rootParent = null;var rootSibling = null;var nImg = new Image();nImg.src = images/drag_drop_poof.gif;/ Demo1 variablesvar D1Target = null;Ntotype.NaN0=function()return isNaN(this)?0:this;function CreateDragContainer()/*Create a new Container Instance so that items from one Set can notbe dragged into items from another Set*/var cDrag = DragDrops.length;DragDropscDrag = ;/*Each item passed to this function should be a container. Store eachof these items in our current container*/for(var i=0; iarguments.length; i+)var cObj = argumentsi;DragDropscDrag.push(cObj);cObj.setAttribute(DropObj, cDrag);/*Every top level item in these containers should be draggable. Do thisby setting the DragObj attribute on each item and then later checkingthis attribute in the mouseMove function*/for(var j=0; jcObj.childNodes.length; j+)/ Firefox puts in lots of #text nodes.skip theseif(cObj.childNodesj.nodeName=#text) continue;cObj.childNodesj.setAttribute(DragObj, cDrag);function getPosition(e)var left = 0;var top = 0;while (e.offsetParent)left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth).NaN0():0);top += e.offsetTop + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth).NaN0():0);e = e.offsetParent;left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth).NaN0():0);top += e.offsetTop + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth).NaN0():0);return x:left, y:top;function mouseCoords(ev)if(ev.pageX | ev.pageY)return x:ev.pageX, y:ev.pageY;return x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,y:ev.clientY + document.body.scrollTop - document.body.clientTop;function writeHistory(object, message)if(!object | !object.parentNode | !object.parentNode.getAttribute) return;var historyDiv = object.parentNode.getAttribute(history);if(historyDiv)historyDiv = document.getElementById(historyDiv);historyDiv.appendChild(document.createTextNode(object.id+: +message);historyDiv.appendChild(document.createElement(BR);historyDiv.scrollTop += 50;function getMouseOffset(target, ev)ev = ev | window.event;var docPos = getPosition(target);var mousePos = mouseCoords(ev);return x:mousePos.x - docPos.x, y:mousePos.y - docPos.y;function mouseMove(ev)ev = ev | window.event;/*We are setting target to whatever item the mouse is currently onFirefox uses event.target here, MSIE uses event.srcElement*/var target = ev.target | ev.srcElement;var mousePos = mouseCoords(ev);if(Demos0 | Demos4)/ mouseOut event - fires if the item the mouse is on has changedif(lastTarget & (target!=lastTarget)writeHistory(lastTarget, Mouse Out Fired);/ reset the classname for the target elementvar origClass = lastTarget.getAttribute(origClass);if(origClass) lastTarget.className = origClass;/*dragObj is the grouping our item is in (set from the createDragContainer function).if the item is not in a grouping we ignore it since it cant be dragged with thisscript.*/var dragObj = target.getAttribute(DragObj); / if the mouse was moved over an element that is draggableif(dragObj!=null)/ mouseOver event - Change the items class if necessaryif(target!=lastTarget)writeHistory(target, Mouse Over Fired);var oClass = target.getAttribute(overClass);if(oClass)target.setAttribute(origClass, target.className);target.className = oClass;/ if the user is just starting to drag the elementif(iMouseDown & !lMouseState)writeHistory(target, Start Dragging);/ mouseDown targetcurTarget = target;/ Record the mouse x and y offset for the elementrootParent = curTarget.parentNode;rootSibling = curTarget.nextSibling;mouseOffset = getMouseOffset(target, ev);/ We remove anything that is in our dragHelper DIV so we can put a new item in it.for(var i=0; idragHelper.childNodes.length; i+) dragHelper.removeChild(dragHelper.childNodesi);/ Make a copy of the current item and put it in our drag helper.dragHelper.appendChild(curTarget.cloneNode(true);dragHelper.style.display = block;/ set the class on our helper DIV if necessaryvar dragClass = curTarget.getAttribute(dragClass);if(dragClass)dragHelper.firstChild.className = dragClass;/ disable dragging from our helper DIV (its already being dragged)dragHelper.firstChild.removeAttribute(DragObj);/*Record the current position of all drag/drop targets relatedto the element. We do this here so that we do not have to doit on the general mouse move event which fires when the mousemoves even 1 pixel. If we dont do this here the scriptwould run much slower.*/var dragConts = DragDropsdragObj;/*first record the width/height of our drag item. Then hide it sinceit is going to (potentially) be moved out of its parent.*/curTarget.setAttribute(startWidth, parseInt(curTarget.offsetWidth);curTarget.setAttribute(startHeight, parseInt(curTarget.offsetHeight);curTarget.style.display = none;/ loop through each possible drop containerfor(var i=0; idragConts.length; i+)with(dragContsi)var pos = getPosition(dragContsi);/*save the width, height and position of each container.Even though we are saving the width and height of eachcontainer back to the container this is much faster becausewe are saving the number and do not have to run throughany calculations again. Also, offsetHeight and offsetWidthare both fairly slow. You would never normally notice anyperformance hit from these two functions but our code isgoing to be running hundreds of times each second so everylittle bit helps!Note that the biggest performance gain here, by far, comesfrom not having to run through the getPosition functionhundreds of times.*/setAttribute(startWidth, parseInt(offsetWidth);setAttribute(startHeight, parseInt(offsetHeight);setAttribute(startLeft, pos.x);setAttribute(startTop, pos.y);/ loop through each child element of each containerfor(var j=0; jdragContsi.childNodes.length; j+)with(dragContsi.childNodesj)if(nodeName=#text) | (dragContsi.childNodesj=curTarget) continue;var pos = getPosition(dragContsi.childNodesj);/ save the width, height and position of each elementsetAttribute(startWidth, parseInt(offsetWidth);setAttribute(startHeight, parseInt(offsetHeight);setAttribute(startLeft, pos.x);setAttribute(startTop, pos.y);/ If we get in here we are dragging somethingif(curTarget)/ move our helper div to wherever the mouse is (adjusted by mouseOffset)dragHelper.style.top = mousePos.y - mouseOffset.y;dragHelper.style.left = mousePos.x - mouseOffset.x;var dragConts = DragDropscurTarget.getAttribute(DragObj);var activeCont = null;var xPos = mousePos.x - mouseOffset.x + (parseInt(curTarget.getAttribute(startWidth) /2);var yPos = mousePos.y - mouseOffset.y + (parseInt(curTarget.getAttribute(startHeight)/2);/ check each drop container to see if our target object is inside the containerfor(var i=0; idragConts.length; i+)with(dragContsi)if(parseInt(getAttribute(startLeft) xPos) &(parseInt(getAttribute(startTop) xPos) &(parseInt(getAttribute(startTop) + parseInt(getAttribute(startHeight) yPos)/*our target is inside of our container so save the container intothe activeCont variable and then exit the loop since we no longerneed to check the rest of the containers*/activeCont = dragContsi;/ exit the for loopbreak;/ Our target object is in one of our containers. Check to see where our div belongsif(activeCont)if(activeCont!=curTarget.parentNode)writeHistory(curTarget, Moved into +activeCont.id);/ beforeNode will hold the first node AFTER where our div belongsvar beforeNode = null;/ loop through each child node (skipping text nodes).for(var i=activeCont.childNodes.length-1; i=0; i-)with(activeCont.childNodesi)if(nodeName=#text) continue;/ if the current item is After the item being draggedif(curTarget != activeCont.childNodesi &(parseInt(getAttribute(startLeft) + parseInt(getAttribute(startWidth) xPos) &(parseInt(getAttribute(startTop) + parseInt(getAttribute(startHeight) yPos)beforeNode = activeCont.childNodesi;/ the item being dragged belongs before another itemif(beforeNode)if(beforeNode!=curTarget.nextSibling)writeHistory(curTarget, Inserted Before +beforeNode.id);activeCont.insertBefore(curTarget, beforeNode);/ the item being dragged belongs at the end of the current container else if(curTarget.nextSibling) | (curTarget.parentNode!=activeCont)writeHistory(curTarget, Inserted at end of +activeCont.id);activeCont.appendChild(curTarget);/ the timeout is here because the container doesnt immediately resizesetTimeout(function()var contPos = getPosition(activeCont);activeCont.setAttribute(startWidth, parseInt(activeCont.offsetWidth);activeCont.setAttribute(startHeight, parseInt(activeCont.offsetHeight);activeCont.setAttribute(startLeft, contPos.x);activeCont.setAttribute(startTop, contPos.y);, 5);/ make our drag item visibleif(curTarget.style.display!=)writeHistory(curTarget, Made Visible);curTarget.style.display = ;curTarget.style.visibility = hidden; else / our drag item is not in a container, so hide it.if(curTarget.style.display!=none)writeHistory(curTarget, Hidden);curTarget.style.display = none;/ track the current mouse state so we can compare against it next timelMouseState = iMouseDown;/ mouseMove targetlastTarget = target;if(Demos2)document.getElementById(MouseXPosition).value = mousePos.x;document.getElementById(MouseYPosition).value = mousePos.y;if(dragObject)dragObject.style.position = absolute;dragObject.style.top = mousePos.y - mouseOffset.y;dragObject.style.left = mousePos.x - mouseOffset.x;/ track the current mouse state so we can compare against it next timelMouseState = iMouseDown;/ this prevents items on the page from being highlighted while draggingif(curTarget | dragObject) return false;function mouseUp(ev)if(Demos0 | Demos4)if(curTarget)writeHistory(curTarget, Mouse Up Fired);dragHelper.style.display = none;if(curTarget.style.display = none)if(rootSibling)rootParent.insertBefore(curTarget, rootSibling); else rootParent.appendC
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2030供应链金融创新模式与资金周转效率分析报告
- 2025-2030二手车交易市场发展现状及数字化转型路径分析研究报告
- 2025-2030中国青年公寓消费群体画像与精准营销策略报告
- 2025-2030中国青年公寓市场格局变化与竞争策略研究报告
- 2025-2030中国青年公寓市场产业结构与投资风险评估报告
- 2025年职业培训服务项目立项申请报告
- 甘肃省兰州市联片办2026届九上化学期中复习检测模拟试题含解析
- 2026届淄博市重点中学九年级英语第一学期期末综合测试试题含解析
- 湖南省长沙市湘郡培粹实验中学2026届九年级英语第一学期期末联考模拟试题含解析
- 2026届山东省曲阜市化学九年级第一学期期末质量检测试题含解析
- 会场布置及座次安排
- (正式版)HGT 6313-2024 化工园区智慧化评价导则
- 科学科普节目策划方案
- 自我同一性理论与经验研究
- 上海交通大学中文主页
- 骨关节外科问诊知识讲座
- 国际篮联三对三篮球比赛记录表
- 室内装饰装修施工工艺标准规范及管理流程
- 【拓展阅读】类文阅读《燧人氏钻木取火》
- 李建涛员工从“老板”做起课件
- 注采压力分布规律研究课件
评论
0/150
提交评论