怎么用javascript进行拖拽

JS 实现控件拖拽/拖动 下面是实现控件拖拽的完整代码,只实现了拖拽,边界控制没做,可以自行添加。 (function ($) {     var move = false;             //标记控件是否处于被拖动状态     var dragOffsetX = 0;        //控件左边界和鼠标X轴的差     var dragOffsetY = 0;        //控件上边界和鼠标 阅读详情

怎么用javascript进行拖拽


本文译自:http://www.webreference.com/programming/javascript/mk/column2/index.html
所有版权归原文所有
由sohotx.com雪山老人翻译整理,转载请标明

Javascript的特点是dom的处理与网页效果,大多数情况我们只用到了这个语言的最简单的功能,比如制作图片轮播/网页的tab等等,这篇文章将向你展示如何在自己的网页上制作拖拽.

有很多理由让你的网站加入拖拽功能,最简单的一个是数据重组.例如:你有一个序列的内容让用户排序,用户需要给每个条目进行输入或者用select选择,替代前面这个方法的就是拖拽.或许你的网站也需要一个用户可以拖动的导航窗口!那么这些效果都是很简单:因为你可以很容易的实现!

网页上实现拖拽其实也不是很复杂.第一你需要知道鼠标坐标,第二你需要知道用户鼠标点击一个网页元素并实现拖拽,最后我们要实现移动这个元素.

样例- 拖拽页面元素
Item #1Item #2Item #3Item #4Item #5Item #6Item #7Item #8Item #9Item #10Item #11Item #12
获取鼠标移动信息

第一我们需要获取鼠标的坐标.我们加一个用户函数到document.onmousemove就可以了:

document.onmousemove = mouseMove;

function mouseMove(ev){
 ev           = ev || window.event;
 var mousePos = mouseCoords(ev);
}

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
 };
}

Demo - Move your mouseMouse X Position:    Mouse Y Position:  

实现上面的独立脚本,选取存下来,并把<>的全角换成半角,就可以看到效果!

<script>

function mouseMove(ev){
 ev           = ev || window.event;
 var mousePos = mouseCoords(ev);
  document.getElementById('xxx').value = mousePos.x;
  document.getElementById('yyy').value = mousePos.y;
}

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
 };
}

document.onmousemove = mouseMove;
</script>
<input id=xxx type=text>
<br>
<input id=yyy type=text>


你首先要声明一个evnet对象.不论何时你移动鼠标/点击/按键等等,会对应一个event的事件.在Internet Explorer里event是全局变量,会被存储在window.event里. 在firefox中,或者其他浏览器,event事件会被相应的自定义函数获取.当我们将mouseMove函数赋值于document.onmousemove,mouseMove会获取鼠标移动事件.

(ev = ev || window.event) 这样让ev在所有浏览器下获取了event事件,在Firefox下"||window.event"将不起作用,因为ev已经有了赋值.在MSIE下ev是空的,所以ev将设置为window.event.

因为我们在这篇文章中需要多次获取鼠标坐标,所以我们设计了mouseCoords这个函数,它只包含了一个参数,就是the event.

我们需要运行在MSIE与Firefox为首的其他浏览器下.Firefox以event.pageX和event.pageY来代表鼠标相应于文档左上角的位置.如果你有一个500*500的窗口,而且你的鼠标在正中间,那么paegX和pageY将是250,当你将页面往下滚动500px,那么pageY将是750.此时pageX不变,还是250.

MSIE和这个相反,MSIE将event.clientX与event.clientY来代表鼠标与ie窗口的位置,并不是文档.当我们有一个500*500的窗口,鼠标在正中间,那么clientX与clientY也是250,如果你垂直滚动窗口到任何位置,clientY仍然是250,因为相对ie窗口并没有变化.想得到正确的结果,我们必须加入scrollLeft与scrollTop这两个相对于文档鼠标位置的属性.最后,由于MSIE并没有0,0的文档起始位置,因为通常会设置2px的边框在周围,边框的宽度包含在document.body.clientLeft与clientTop这两个属性中,我们再加入这些到鼠标的位置中.

很幸运,这样mouseCoords函数就完成了,我们不再为坐标的事操心了.

捕捉鼠标点击
下次我们将知道鼠标何时点击与何时放开.如果我们跳过这一步,我们在做拖拽时将永远不知道鼠标移动上面时的动作,这将是恼人的与违反直觉的.

这里有两个函数帮助我们:onmousedown与onmouseup.我们预先设置函数来接收document.onmousemove,这样看起来很象我们会获取document.onmousedown与document.onmouseup.但是当我们获取document.onmousedown时,我们同时获取了任何对象的点击属性如:text,images,tables等等.我们只想获取那些需要拖拽的属性,所以我们设置函数来获取我们需要移动的对象.

Demo - Click any image
 
You clicked on:  
实现上面的独立脚本,选取存下来,并把<>的全角换成半角,就可以看到效果!

<script>
function mouseDown(ev){
 ev         = ev || window.event;
 var target = ev.target || ev.srcElement;

 if(target.onmousedown || target.getAttribute('DragObj')){
  return false;
 }
}

function makeClickable(item){
 if(!item) return;
 item.onmousedown = function(ev){
  document.getElementById('ClickImage').value = this.name;
 }
}
document.onmousedown = mouseDown;
window.onload = function(){
 makeClickable(document.getElementById('ClickImage1'));
 makeClickable(document.getElementById('ClickImage2'));
 makeClickable(document.getElementById('ClickImage3'));
 makeClickable(document.getElementById('ClickImage4'));
}</script>

<FIELDSET id=Demo3>
<h3>
  Demo - Click any image
</h3>
<IMG id=ClickImage1
src="http://www.webreference.com/programming/javascript/mk/column2/Dragging and Dropping in JavaScript_files/drag_drop_spade.gif"
name=Spade><IMG id=ClickImage2
src="http://www.webreference.com/programming/javascript/mk/column2/Dragging and Dropping in JavaScript_files/drag_drop_heart.gif"
name=Heart><IMG id=ClickImage3
src="http://www.webreference.com/programming/javascript/mk/column2/Dragging and Dropping in JavaScript_files/drag_drop_diamond.gif"
name=Diamond><IMG id=ClickImage4
src="http://www.webreference.com/programming/javascript/mk/column2/Dragging and Dropping in JavaScript_files/drag_drop_club.gif"
 name=Club>
<BR>You clicked on: <INPUT id=ClickImage type="text"> </FIELDSET>

移动一个元素
我们知道了怎么捕捉鼠标移动与点击.剩下的就是移动元素了.首先,要确定一个明确的页面位置,css样式表要用'absolute'.设置元素绝对位置意味着我们可以用样式表的.top和.left来定位,可以用相对位置来定位了.我们将鼠标的移动全部相对页面top-left,基于这点,我们可以进行下一步了.

当我们定义item.style.position='absolute',所有的操作都是改变left坐标与top坐标,然后它移动了.

document.onmousemove = mouseMove;
document.onmouseup   = mouseUp;

var dragObject  = null;
var mouseOffset = null;

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 getPosition(e){
 var left = 0;
 var top  = 0;

 while (e.offsetParent){
  left += e.offsetLeft;
  top  += e.offsetTop;
  e     = e.offsetParent;
 }

 left += e.offsetLeft;
 top  += e.offsetTop;

 return {x:left, y:top};
}

function mouseMove(ev){
 ev           = ev || window.event;
 var mousePos = mouseCoords(ev);

 if(dragObject){
  dragObject.style.position = 'absolute';
  dragObject.style.top      = mousePos.y - mouseOffset.y;
  dragObject.style.left     = mousePos.x - mouseOffset.x;

  return false;
 }
}
function mouseUp(){
 dragObject = null;
}

function makeDraggable(item){
 if(!item) return;
 item.onmousedown = function(ev){
  dragObject  = this;
  mouseOffset = getMouseOffset(this, ev);
  return false;
 }
}

Demo - Drag any of the images
 
实现上面效果的代码,注意将<>的全角换成半角

<script>
var iMouseDown  = false;
var dragObject  = null;
var curTarget   = null;

function makeDraggable(item){
 if(!item) return;
 item.onmousedown = function(ev){
  dragObject  = this;
  mouseOffset = getMouseOffset(this, ev);
  return false;
 }
}

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 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 mouseDown(ev){
 ev         = ev || window.event;
 var target = ev.target || ev.srcElement;

 if(target.onmousedown || target.getAttribute('DragObj')){
  return false;
 }
}

function mouseUp(ev){

 dragObject = null;

 iMouseDown = false;
}


function mouseMove(ev){
 ev         = ev || window.event;

 /*
 We are setting target to whatever item the mouse is currently on

 Firefox uses event.target here, MSIE uses event.srcElement
 */
 var target   = ev.target || ev.srcElement;
 var mousePos = mouseCoords(ev);

 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 time
 lMouseState = iMouseDown;

 // this prevents items on the page from being highlighted while dragging
 if(curTarget || dragObject) return false;
}

document.onmousemove = mouseMove;
document.onmousedown = mouseDown;
document.onmouseup   = mouseUp;

window.onload = function() {
 makeDraggable(document.getElementById('DragImage5'));
 makeDraggable(document.getElementById('DragImage6'));
 makeDraggable(document.getElementById('DragImage7'));
 makeDraggable(document.getElementById('DragImage8'));
}
</script>

<FIELDSET id=Demo5 style="HEIGHT: 70px"><h3>Demo - Drag any of the
images</h3><IMG id=DragImage5
src="images/drag_drop_spade.gif"><IMG
id=DragImage6
src="images/drag_drop_heart.gif"><IMG
id=DragImage7
src="images/drag_drop_diamond.gif"><IMG
id=DragImage8
src="images/drag_drop_club.gif"> </FIELDSET>

你会注意到这个代码几乎是前面的全集,将前面的合在一起就实现了拖拽效果了.

当我们点击一个item时,我们就获取了很多变量,如鼠标位置,鼠标位置自然就包含了那个item的坐标信息了.如果我们点击了一个20*20px图像的正中间,那么鼠标的相对坐标为{x:10,y:10}.当我们点击这个图像的左上角那么鼠标的相对坐标为{x:0,y:0}.当我们点击时,我们用这个方法取得一些鼠标与图片校对的信息.如果我们不能加载页面item,那么信息将是document信息,会忽略了点击的item信息.

mouseOffset函数使用了另一个函数getPosition.getPosition的作用是返回item相对页面左上角的坐标,如果我们尝试获取item.offsetLeft或者item.style.left,那么我们将取得item相对与父级的位置,不是整个document.所有的脚本我们都是相对整个document,这样会更好一些.

为了完成getPosition任务,必须循环取得item的父级,我们将加载内容到item的左/上的位置.我们需要管理想要的top与left列表.

自从定义了mousemove这个函数,mouseMove就会一直运行.第一我们确定item的style.position为absolute,第二我们移动item到前面定义好的位置.当mouse点击被释放,dragObject被设置为null,mouseMove将不在做任何事.

Dropping an Item
前面的例子目的很简单,就是拖拽item到我们希望到的地方.我们经常还有其他目的如删除item,比如我们可以将item拖到垃圾桶里,或者其他页面定义的位置.

很不幸,我们有一个很大的难题,当我们拖拽,item会在鼠标之下,比如mouseove,mousedown,mouseup或者其他mouse action.如果我们拖拽一个item到垃圾桶上,鼠标信息还在item上,不在垃圾桶上.

怎么解决这个问题呢?有几个方法可以来解决.第一,这是以前比较推荐的,我们在移动鼠标时item会跟随鼠标,并占用了mouseover/mousemove等鼠标事件,我们不这样做,只是让item跟随着鼠标,并不占用mouseover等鼠标事件,这样会解决问题,但是这样并不好看,我们还是希望item能直接跟在mouse下.

另一个选择是不做item的拖拽.你可以改变鼠标指针来显示需要拖拽的item,然后放在鼠标释放的位置.这个解决方案,也是因为美学原因不予接受.

最后的解决方案是,我们并不去除拖拽效果.这种方法比前两种繁杂许多,我们需要定义我们需要释放目标的列表,当鼠标释放时,手工去检查释放的位置是否是在目标列表位置上,如果在,说明是释放在目标位置上了.

/*
All code from the previous example is needed with the exception
of the mouseUp function which is replaced below
*/

var dropTargets = [];

function addDropTarget(dropTarget){
 dropTargets.push(dropTarget);
}

function mouseUp(ev){
 ev           = ev || window.event;
 var mousePos = mouseCoords(ev);

 for(var i=0; i<dropTargets.length; i++){
  var curTarget  = dropTargets[i];
  var targPos    = getPosition(curTarget);
  var targWidth  = parseInt(curTarget.offsetWidth);
  var targHeight = parseInt(curTarget.offsetHeight);

  if(
   (mousePos.x > targPos.x)                &&

   (mousePos.x < (targPos.x + targWidth))  &&
   (mousePos.y > targPos.y)                &&
   (mousePos.y < (targPos.y + targHeight))){
    // dragObject was dropped onto curTarget!
  }
 }

 dragObject   = null;
}

Demo - Drag any image onto the trashcan
        
实现上面效果的代码,注意将<>的全角换成半角

<script>
var iMouseDown  = false;
var dragObject  = null;
Number.prototype.NaN0=function(){return isNaN(this)?0:this;}


// Demo 0 variables
var DragDrops   = [];
var curTarget   = null;
var lastTarget  = null;

 

function makeDraggable(item){
 if(!item) return;
 item.onmousedown = function(ev){
  dragObject  = this;
  mouseOffset = getMouseOffset(this, ev);
  return false;
 }
}

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 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 mouseDown(ev){
 ev         = ev || window.event;
 var target = ev.target || ev.srcElement;

 if(target.onmousedown || target.getAttribute('DragObj')){
  return false;
 }
}

function mouseUp(ev){

 //dragObject = null;

 if(dragObject){
  ev           = ev || window.event;
  var mousePos = mouseCoords(ev);

  var dT = dragObject.getAttribute('droptarget');
  if(dT){
   var targObj = document.getElementById(dT);
   var objPos  = getPosition(targObj);
   if((mousePos.x > objPos.x) && (mousePos.y > objPos.y)
&& (mousePos.x<(objPos.x+parseInt(targObj.offsetWidth)))
 && (mousePos.y<(objPos.y+parseInt(targObj.offsetHeight)))){
    var nSrc = targObj.getAttribute('newSrc');
    if(nSrc){
     dragObject.src = nSrc;
     setTimeout(function(){
      if(!dragObject || !dragObject.parentNode) return;
      dragObject.parentNode.removeChild(dragObject);
      dragObject = null;
     }, parseInt(targObj.getAttribute('timeout')));
    } else {
     dragObject.parentNode.removeChild(dragObject);
    }
   }
  }
 }
 dragObject = null;

 iMouseDown = false;
}


function mouseMove(ev){
 ev         = ev || window.event;

 /*
 We are setting target to whatever item the mouse is currently on

 Firefox uses event.target here, MSIE uses event.srcElement
 */
 var target   = ev.target || ev.srcElement;
 var mousePos = mouseCoords(ev);

 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 time
 lMouseState = iMouseDown;

 // this prevents items on the page from being highlighted while dragging
 if(curTarget || dragObject) return false;
}


function addDropTarget(item, target){
 item.setAttribute('droptarget', target);
}

document.onmousemove = mouseMove;
document.onmousedown = mouseDown;
document.onmouseup   = mouseUp;

window.onload = function (){
 makeDraggable(document.getElementById('DragImage9'));
 makeDraggable(document.getElementById('DragImage10'));
 makeDraggable(document.getElementById('DragImage11'));
 makeDraggable(document.getElementById('DragImage12'));

 addDropTarget(document.getElementById('DragImage9'),  'TrashImage1');
 addDropTarget(document.getElementById('DragImage10'), 'TrashImage1');
 addDropTarget(document.getElementById('DragImage11'), 'TrashImage1');
 addDropTarget(document.getElementById('DragImage12'), 'TrashImage1');
}
</script>

 

<FIELDSET id=Demo6 style="HEIGHT: 70px"><h3>Demo - Drag any image onto the
trashcan</h3><IMG id=TrashImage1
src="http://www.webreference.com/programming/javascript/mk/column2/Dragging and Dropping in JavaScript_files/drag_drop_trash.gif"
timeout="600" newSrc="images/transparent.gif">      
<IMG
id=DragImage9
src="http://www.webreference.com/programming/javascript/mk/column2/Dragging and Dropping in JavaScript_files/drag_drop_spade.gif">
<IMG
id=DragImage10
src="http://www.webreference.com/programming/javascript/mk/column2/Dragging and Dropping in JavaScript_files/drag_drop_heart.gif">
<IMG
id=DragImage11
src="http://www.webreference.com/programming/javascript/mk/column2/Dragging and Dropping in JavaScript_files/drag_drop_diamond.gif">
<IMG
id=DragImage12
src="http://www.webreference.com/programming/javascript/mk/column2/Dragging and Dropping in JavaScript_files/drag_drop_club.gif">
</FIELDSET>

鼠标释放时会去取是否有drop属性,如果存在,同时鼠标指针还在drop的范围内,执行drop操作.我们检查鼠标指针位置是否在目标范围是用(mousePos.x>targetPos.x),而且还要符合条件(mousePos.x<(targPos.x + targWidth)).如果所有的条件符合,说明指针确实在范围内,可以执行drop指令了.

Pulling It All Together
最后我们拥有了所有的drag/drop的脚本片断!下一个事情是我们将创建一个DOM处理.如果你不是很熟悉,请先阅读我的JavaScript Primer on DOM Manipulation.

下面的代码将创建container(容器),而且使任何一个需要drag/drop的item变成一个容器的item.代码在这个文章第二个demo的后面,它可以用户记录一个list(列表),定为一个导航窗口在左边或者右边,或者更多的函数你可以想到的.

下一步我们将通过"假代码"让reader看到真代码,下面为推荐:

当document第一次载入时,创建dragHelper DIV.dragHelper将给移动的item加阴影.真实的item没有被dragged,只是用了insertBefor和appendChild来移动了,我们隐藏了dragHelper
有了mouseDown与mouseUp函数.所有的操作会对应到当到iMouseDown的状态中,只有当mouse左键为按下时iMouseDown才为真,否则为假.
我们创建了全局变量DragDrops与全局函数CreateDragContainer.DragDrops包含了一系列相对彼此的容器.任何参数(containers)将通过CreatedcragContainer进行重组与序列化,这样可以自由的移动.CreateDragContainer函数也将item进行绑定与设置属性.
现在我们的代码知道每个item的加入,当我们移动处mouseMove,mouseMove函数首先会设置变量target,鼠标移动在上面的item,如果这个item在容器中(checked with getAttribute):
运行一小段代码来改变目标的样式.创造rollover效果
检查鼠标是否没有放开,如果没有
设置curTarget代表当前item
记录item的当前位置,如果需要的话,我们可以将它返回
克隆当前的item到dragHelper中,我们可以移动带阴影效果的item.
item拷贝到dragHelper后,原有的item还在鼠标指针下,我们必须删除掉dragObj,这样脚本起作用,dragObj被包含在一个容器中.
抓取容器中所有的item当前坐标,高度/宽度,这样只需要记录一次,当item被drag时,每随mouse移动,每移钟就会记录成千上万次.
如果没有,不需要做任何事,因为这不是一个需要移动的item
检查curTarget,它应该包含一个被移动的item,如果存在,进行下面操作
开始移动带有阴影的item,这个item就是前文所创建的
检查每个当前容器中的container,是否鼠标已经移动到这些范围内了
我们检查看一下正在拖动的item是属于哪个container
放置item在一个container的某一个item之前,或者整个container之后
确认item是可见的
如果鼠标不在container中,确认item是不可见了.
剩下的事就是捕捉mouseUp的事件了
实现一个拖动的全代码,注意将<>换成半角

<STYLE>LI {
 MARGIN-BOTTOM: 10px
}
OL {
 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: #ff99cc
}
LEGEND {
 FONT-WEIGHT: bold; FONT-SIZE: 12px; COLOR: #666699; FONT-FAMILY: verdana, tahoma, arial
}
FIELDSET {
 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: 20px
}
pre{border:1 solid #CCC;background-color:#F8F8F0;padding:10px;}
</STYLE>


<script>

// iMouseDown represents the current mouse button state: up or down
/*
lMouseState represents the previous mouse button state so that we can
check 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 variables
var DragDrops   = [];
var curTarget   = null;
var lastTarget  = null;
var dragHelper  = null;
var tempDiv     = null;
var rootParent  = null;
var rootSibling = null;

Number.prototype.NaN0=function(){return isNaN(this)?0:this;}

function CreateDragContainer(){
 /*
 Create a new "Container Instance" so that items from one "Set" can not
 be dragged into items from another "Set"
 */
 var cDrag        = DragDrops.length;
 DragDrops[cDrag] = [];

 /*
 Each item passed to this function should be a "container".  Store each
 of these items in our current container
 */
 for(var i=0; i<arguments.length; i++){
  var cObj = arguments[i];
  DragDrops[cDrag].push(cObj);
  cObj.setAttribute('DropObj', cDrag);

  /*
  Every top level item in these containers should be draggable.  Do this
  by setting the DragObj attribute on each item and then later checking
  this attribute in the mouseMove function
  */
  for(var j=0; j<cObj.childNodes.length; j++){

   // Firefox puts in lots of #text nodes...skip these
   if(cObj.childNodes[j].nodeName=='#text') continue;

   cObj.childNodes[j].setAttribute('DragObj', cDrag);
  }
 }
}


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 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 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 mouseMove(ev){
 ev         = ev || window.event;

 /*
 We are setting target to whatever item the mouse is currently on

 Firefox uses event.target here, MSIE uses event.srcElement
 */
 var target   = ev.target || ev.srcElement;
 var mousePos = mouseCoords(ev);

 // mouseOut event - fires if the item the mouse is on has changed
 if(lastTarget && (target!==lastTarget)){
  // reset the classname for the target element
  var 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 can't be dragged with this
 script.
 */
 var dragObj = target.getAttribute('DragObj');

  // if the mouse was moved over an element that is draggable
 if(dragObj!=null){

  // mouseOver event - Change the item's class if necessary
  if(target!=lastTarget){
   var oClass = target.getAttribute('overClass');
   if(oClass){
    target.setAttribute('origClass', target.className);
    target.className = oClass;
   }
  }

  // if the user is just starting to drag the element
  if(iMouseDown && !lMouseState){
   // mouseDown target
   curTarget     = target;

   // Record the mouse x and y offset for the element
   rootParent    = 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; i<dragHelper.childNodes.length; i++) dragHelper.removeChild(dragHelper.childNodes[i]);

   // 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 necessary
   var dragClass = curTarget.getAttribute('dragClass');
   if(dragClass){
    dragHelper.firstChild.className = dragClass;
   }

   // disable dragging from our helper DIV (it's already being dragged)
   dragHelper.firstChild.removeAttribute('DragObj');

   /*
   Record the current position of all drag/drop targets related
   to the element.  We do this here so that we do not have to do
   it on the general mouse move event which fires when the mouse
   moves even 1 pixel.  If we don't do this here the script
   would run much slower.
   */
   var dragConts = DragDrops[dragObj];

   /*
   first record the width/height of our drag item.  Then hide it since
   it 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 container
   for(var i=0; i<dragConts.length; i++){
    with(dragConts[i]){
     var pos = getPosition(dragConts[i]);

     /*
     save the width, height and position of each container.

     Even though we are saving the width and height of each
     container back to the container this is much faster because
     we are saving the number and do not have to run through
     any calculations again.  Also, offsetHeight and offsetWidth
     are both fairly slow.  You would never normally notice any
     performance hit from these two functions but our code is
     going to be running hundreds of times each second so every
     little bit helps!

     Note that the biggest performance gain here, by far, comes
     from not having to run through the getPosition function
     hundreds 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 container
    for(var j=0; j<dragConts[i].childNodes.length; j++){
     with(dragConts[i].childNodes[j]){
      if((nodeName=='#text') || (dragConts[i].childNodes[j]==curTarget)) continue;

      var pos = getPosition(dragConts[i].childNodes[j]);

      // save the width, height and position of each element
      setAttribute('startWidth',  parseInt(offsetWidth));
      setAttribute('startHeight', parseInt(offsetHeight));
      setAttribute('startLeft',   pos.x);
      setAttribute('startTop',    pos.y);
     }
    }
   }
  }
 }

 // If we get in here we are dragging something
 if(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  = DragDrops[curTarget.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 container
  for(var i=0; i<dragConts.length; i++){
   with(dragConts[i]){
    if(((getAttribute('startLeft'))                               < xPos) &&
     ((getAttribute('startTop'))                                < yPos) &&
     ((getAttribute('startLeft') + getAttribute('startWidth'))  > xPos) &&
     ((getAttribute('startTop')  + getAttribute('startHeight')) > yPos)){

      /*
      our target is inside of our container so save the container into
      the activeCont variable and then exit the loop since we no longer
      need to check the rest of the containers
      */
      activeCont = dragConts[i];

      // exit the for loop
      break;
    }
   }
  }

  // Our target object is in one of our containers.  Check to see where our div belongs
  if(activeCont){
   // beforeNode will hold the first node AFTER where our div belongs
   var beforeNode = null;

   // loop through each child node (skipping text nodes).
   for(var i=activeCont.childNodes.length-1; i>=0; i--){
    with(activeCont.childNodes[i]){
     if(nodeName=='#text') continue;

     // if the current item is "After" the item being dragged
     if(
      curTarget != activeCont.childNodes[i]                              &&
      ((getAttribute('startLeft') + getAttribute('startWidth'))  > xPos) &&
      ((getAttribute('startTop')  + getAttribute('startHeight')) > yPos)){
       beforeNode = activeCont.childNodes[i];
     }
    }
   }

   // the item being dragged belongs before another item
   if(beforeNode){
    if(beforeNode!=curTarget.nextSibling){
     activeCont.insertBefore(curTarget, beforeNode);
    }

   // the item being dragged belongs at the end of the current container
   } else {
    if((curTarget.nextSibling) || (curTarget.parentNode!=activeCont)){
     activeCont.appendChild(curTarget);
    }
   }

   // make our drag item visible
   if(curTarget.style.display!=''){
    curTarget.style.display  = '';
   }
  } else {

   // our drag item is not in a container, so hide it.
   if(curTarget.style.display!='none'){
    curTarget.style.display  = 'none';
   }
  }
 }

 // track the current mouse state so we can compare against it next time
 lMouseState = iMouseDown;

 // mouseMove target
 lastTarget  = target;

 // track the current mouse state so we can compare against it next time
 lMouseState = iMouseDown;

 // this helps prevent items on the page from being highlighted while dragging
 return false;
}

function mouseUp(ev){
 if(curTarget){
  // hide our helper object - it is no longer needed
  dragHelper.style.display = 'none';

  // if the drag item is invisible put it back where it was before moving it
  if(curTarget.style.display == 'none'){
   if(rootSibling){
    rootParent.insertBefore(curTarget, rootSibling);
   } else {
    rootParent.appendChild(curTarget);
   }
  }

  // make sure the drag item is visible
  curTarget.style.display = '';
 }
 curTarget  = null;
 iMouseDown = false;
}

function mouseDown(){
 iMouseDown = true;
 if(lastTarget){
  return false;
 }
}

document.onmousemove = mouseMove;
document.onmousedown = mouseDown;
document.onmouseup   = mouseUp;

window.onload = function(){
 // Create our helper object that will show the item while dragging
 dragHelper = document.createElement('DIV');
 dragHelper.style.cssText = 'position:absolute;display:none;';
  
 CreateDragContainer(
  document.getElementById('DragContainer1'),
  document.getElementById('DragContainer2'),
  document.getElementById('DragContainer3')
 );

 document.body.appendChild(dragHelper);
}

</script>

<!--the mouse over and dragging class are defined on each item-->

<div class="DragContainer" id="DragContainer1">
 <div class="DragBox" id="Item1"  overClass="OverDragBox" dragClass="DragDragBox">Item #1</div>
 <div class="DragBox" id="Item2"  overClass="OverDragBox" dragClass="DragDragBox">Item #2</div>
 <div class="DragBox" id="Item3"  overClass="OverDragBox" dragClass="DragDragBox">Item #3</div>
 <div class="DragBox" id="Item4"  overClass="OverDragBox" dragClass="DragDragBox">Item #4</div>
</div>
<div class="DragContainer" id="DragContainer2">
 <div class="DragBox" id="Item5"  overClass="OverDragBox" dragClass="DragDragBox">Item #5</div>
 <div class="DragBox" id="Item6"  overClass="OverDragBox" dragClass="DragDragBox">Item #6</div>
 <div class="DragBox" id="Item7"  overClass="OverDragBox" dragClass="DragDragBox">Item #7</div>
 <div class="DragBox" id="Item8"  overClass="OverDragBox" dragClass="DragDragBox">Item #8</div>
</div>
<div class="DragContainer" id="DragContainer3">
 <div class="DragBox" id="Item9"  overClass="OverDragBox" dragClass="DragDragBox">Item #9</div>
 <div class="DragBox" id="Item10" overClass="OverDragBox" dragClass="DragDragBox">Item #10</div>
 <div class="DragBox" id="Item11" overClass="OverDragBox" dragClass="DragDragBox">Item #11</div>
 <div class="DragBox" id="Item12" overClass="OverDragBox" dragClass="DragDragBox">Item #12</div>
</div>
你现在拥有了拖拽的所有东西.

下面的三个demo是记录事件历史.当你的鼠标在item上移动,将记录所生的事件,如果你不明白可以尝试一下鼠标的划过或者拖动,看有什么发生.

Demo - Drag and Drop any item
Item #1Item #2Item #3Item #4Item #5Item #6Item #7Item #8Item #9Item #10Item #11Item #12History  Demo - Drag and Drop any item
Item #1
Item #2
Item #3
Item #4
Item #5
 Demo - Drag and Drop any item
12345678910111213141516
关于译者

雪山老人,lamp程序员,web开发者,你可以访问sohotx.com来了解他!

关于作者

Mark Kahn是一个web开发者与DBA.你可以访问他的主页 http://www.jslibrary.org联系他!

 

SVG Draggy JS:简化SVG拖拽操作的JavaScript库插件 SVG(Scalable Vector Graphics)是一种基于XML的矢量图形格式,用于描述二维矢量图形、矢量/栅格图像混合图形以及点文本。它是Web图形的一个重要组成部分,具有轻量、易编辑和可交互的特点。 阅读详情

相关推荐

基于jsp页面的拖拽功能

基于jsp页面的拖拽功能,用户可以互相实现拖拽

页面悬浮按钮可拖动实现(vue和js写法)

注:如果想在vue项目中的每个页面都加上悬浮按钮,可用js,写在APP.vue的onLaunch()函数里。2.然后在main.js中注册全局组件(这里大家根据自己的实际路径替换)3.最后在需要这个悬浮按钮的页面模板中用这个组件就可以了。1.创建一个FloatingButton.vue的组件。1. 每个页面都能自动加上悬浮按钮。

qq_51819142的博客 5436

ROS2初级面试题汇总

总结和收集了一些关于ros2的问题和解答记录

qq_57633442的博客 5555

简单的悬浮窗+拖动

1.布局 &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com...

土豆你个卜奈奈的博客 631

拆解大疆Osmo Pocket:聊聊微型云台里那几根比头发丝还细的线,以及它为啥一开机就‘摇头晃脑’

本文深入拆解大疆Osmo Pocket微型云台的精密硬件结构,揭秘其开机自检时‘摇头晃脑’的奥秘。通过分析无刷电机、霍尔传感器和46AWG同轴线等核心组件,展现了大疆在微型云台设计中的工程智慧与创新技术,特别是FOC控制和陀螺仪的应用,实现了0.1°级的姿态控制精度。

weixin_27869497的博客 667

微信小程序真机HTTPS证书链失效排查与修复指南

HTTPS证书信任链是保障Web通信安全的基础机制,其核心在于客户端能否通过服务器提供的证书及其上级签发者,逐级回溯至自身预置的可信根证书。在微信小程序场景中,由于客户端不复用系统根证书库,而是采用固化、精简且更新滞后的自研CA白名单,导致大量符合浏览器标准的证书(如Let’s Encrypt新版ISRG Root X1、TrustAsia等)在真机环境下触发net::ERR_CERT_AUTHORITY_INVALID错误。该问题本质是证书链断裂或签名算法/密钥长度不合规,而非域名配置或CORS策略异常。

weixin_33935636的博客 677

如何使用js实现拖拽效果?

因为设置了拖拽属性的元素就可以被拖拽,结束拖拽的时候获取一下鼠标与页面的距离,减去鼠标与元素中的距离就行了,逻辑为旧有拖拽方式的第二种:盒子的位置 = 元素与页面间的距离 - 元素与盒子的距离。:返回元素与有定位的父类的上左边距,如果父类都没有定位,那么返回相对于页面的上左边距(本示例中页面内只有一个元素,故获取的值是元素相对于页面的距离):鼠标距离触发事件的元素的上左边距(本示例中只有一个元素,故获取的值为鼠标相对于盒子元素的上左边距)// 元素的位置 = 鼠标与页面的距离 - 鼠标与元素的距离。

黄彪博客 4716

PHP实战:5分钟搞定SM4国密加密算法集成(附完整代码)

本文详细介绍了如何在PHP项目中快速集成SM4国密加密算法,提供完整的代码实现和实际应用示例。从算法原理到核心代码解析,再到性能优化与安全实践,帮助开发者高效实现数据加密功能,满足金融、政务等领域的合规需求。

weixin_29315569的博客 354

【JS】原生实现矩形框的绘制/拖动/缩放/截图及导出功能

原生js实现矩形框的绘制/拖动/缩放/截图及导出功能 1.1、矩形框的绘制 1.2、矩形框的缩放 1.3、矩形框的拖动 1.4、矩形框的键盘移动方向 1.5、矩形框的截图及导出图片

qq_39404437的博客 4423

西门子与汇川伺服脉冲连接(速度模式)

如何使用西门子PLC与汇川伺服进行脉冲连接

2301_77347966的博客 1万+

YOLOv11来了,使用YOLOv11训练自己的数据集和推理(附YOLOv11网络结构图)

YOLOv11 由 Ultralytics 在 2024 年 9 月 30 日发布, 最新的 YOLOv11 模型在之前的 YOLO 版本引入了新功能和改进,以进一步提高性能和灵活性。YOLO11 在快速、准确且易于使用,使其成为各种对象检测和跟踪、实例分割、图像分类和姿态估计任务的绝佳选择。

在职AI算法工程师,擅长计算机视觉,YOLO目标检测、分割等,擅长web、pyqt界面可视化,好内容持续更新中,来这里跟大家一起学习,共同进步 28万+

Sortable.js:功能强大的JavaScript 拖拽库

Sortable.js:功能强大的JavaScript 拖拽库

流绪@微梦 1万+

华为OD机试真题2026双机位C卷 Java&Go 实现【用户入网定期复评】

用户开立支付账户,在开户完成后立即对用户进行一次风险模型评级,输出一个评分风险,评分范围1-100,等区间的分为5个等级,1-19为等级1,20-39为等级2,40-59为等级3,60-79为等级4,80-100为等级5,系统每天会任务对全量入网用户进行计算复评时间,输出评分给出新的风险等级。复评规则如下:

MISAYAONE的博客 147

JavaScript鼠标拖拽和JS运动

笔记 一、拖拽效果 分析思路: 鼠标需要按下 mousedown 鼠标移动过程中,物体随着鼠标的移动而移动 mousemove 鼠标抬起时,物体停止移动 mouseup 注意: 1.鼠标按下事件添加给物体,鼠标移动和抬起都应该添加页面 2.鼠标相对于物体的位置 var o = document.getElementById('box');//物体 //鼠标按下事件 o.onmousedown = function (e) {

Majy_的博客 366

SNAP计算哨兵2号的LAI/FVC/FAPAR

SNAP的计算LAI方法是基于PROSAIL模型,集成的模块,很方便。首先要下载SNAP软件,下载步骤见。

所有专家都曾始于新手 2505

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

JavaScript:js实现拖拽

js实现拖拽 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>js实现拖拽</title> <script src=

一火的专栏 1085

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

Dragdealer.js 是什么?12KB零依赖的原生JavaScript拖拽组件全解析

在网页开发中,"拖拽"往往意味着引入 jQuery UI 或动辄几十 KB 的第三方库。而 **Dragdealer.js 拖拽组件** 打破了这一印象:它是一个基于原生 JavaScript(Vanilla JS)实现的拖拽组件,完整源码仅约 12KB、**零依赖**,却能用两个简单的 DOM 元素构建出滑杆、轮播图、滑动解锁、内容遮罩等丰富的交互界面。本文将带你全面了解 Dragdealer.

gitblog_00734的博客 922

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

原生JS拖拽从原理到实现

拖拽在js中挺常用的,不过目前有很多做拖拽的插件,H5也以及支持了拖拽事件,不过了解一下拖拽的原理也是有必要的,其实用原生js实现拖拽并不难。 首先讲一下拖拽的原理,假设浏览器上有一个元素(元素已经绝对定位了),起始位置为(mx, my),即距浏览器上方mypx,左方mxpx,如下图 拖拽元素的过程无非就是改变该元素的left值和top值,那么left和top值设为多少才对呢,其实这个...

daydream13580130043的博客 1790

interact.js 前端拖拽插件

interact.js 是一款轻量且功能强大的 JavaScript 拖拽交互库,核心用于实现元素的拖拽、缩放、旋转等交互式操作。

0.0的博客 1170

js拖拽和放置 javascript drag and drop

js拖拽和放置 javascript drag and drop

hfrujhv的博客 1367

Interact.js终极指南:10分钟掌握JavaScript拖拽与缩放技术

作为前端开发者,你一定遇到过需要实现拖拽、缩放等交互功能的需求。interact.js正是为解决这些问题而生的强大JavaScript库,它提供了简单易用的API来实现复杂的交互体验。这个轻量级库支持惯性效果、吸附对齐和多点触控,是现代Web开发的必备工具。 ## 🔥 为什么选择interact.js? interact.js是一个功能强大的JavaScript拖拽库,专门为现代浏览器设计,

gitblog_00224的博客 492

JS实现拖拽元素交换位置

源码:js-essays/draggable 基本效果展示: 基本思路: 使元素可以被拖拽(利用 draggable 属性, 注意:链接和图片默认是可拖拽的) 拖拽时,通过被拖拽节点和其他节点的drag事件交互,来切换两个节点的位置(非唯一方法) 添加用户拖拽交互效果 用到的方法: ondrag 事件在元素或者选取的文本被拖动时触发。 在拖动目标上触发(源目标元素): ondragstart - 用户开始拖动元素 ondrag - 元素正在被拖动 ondragend - 用户结束元素拖动

艾欢欢的博客 5005

从零开始学JavaScript——JS拖拽三大事件

说到拖拽,我们要知道的三大事件 onmousedown(鼠标按下选择元素) onmousemove(按住鼠标移动元素) onmousedown(鼠标松开释放元素) 接下来我们来看一个小案例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge">

m0_46186102的博客 1813

原生js如何做拖拽_javascript的拖拽怎么做?可拖拽的窗口(div)

原生js如何做拖拽_javascript的拖拽怎么做?可拖拽的窗口(div) <!DOCTYPE html> <html id="hId"> <head> <meta charset="UTF-8"> <title></title> </head> <style type="text/css"> *{ margin:0px; padding:0px; } #box{ position:absolute;

杨星的博客 171

纯JS(JavaScript)实现拖拽效果,兼容各大浏览器

为了让每一个看了这篇博客的人都会拖拽效果,我会以最简单最有条理的为大家讲清楚。 首先大致讲解一下拖拽的实现思路 确定拖拽对象,就是需要拖拽的元素,我们给它一个id或一个class作为唯一标识 拖拽过程用到三个时间,鼠标按下事件,鼠标移动事件,鼠标抬起事件。 一定要养成先理清思路再具体实现的习惯,上面的思路列出来就感觉很简单了吧。 第一步,确定拖拽对象,这里我写了一个div<div id="div1

似码非码的博客 2623
上一篇: 在Option条目中填充前导空格的方法
下一篇: 用Javascript写的一个映射表类
SmartJavaer
博客等级 码龄21年 13粉丝 37原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值