Home >Web Front-end >JS Tutorial >Some common ideas and methods for js drag and drop_javascript skills

Some common ideas and methods for js drag and drop_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:55:061054browse

Common ideas for js dragging

1. Use onmousedown, onmousemove, and onmouseup to simulate events at the start of dragging, during dragging, and at the end of dragging ().

If the touch events of the mobile phone are ontouchstart, ontouchmove and ontouchend respectively.

2. When the onmousedown event occurs when the mouse is pressed: get the mouse position, get the position of the dragged element, and record the difference in vertical and horizontal coordinates between the two (). Bind onmousemove and onmouseup events to the document element.

When I first came into contact with js drag and drop, I was wondering why it was bound to the document instead of the dragged element? It turns out that if the dragged element is bound, when the mouse is dragged too fast, the mouse will be detached from the dragged element.

3. When the onmousemove event occurs when the mouse is dragged: Change the position of the dragged element to an absolute position. This can change the position of the element through left and top, so that the element will follow the dragging of the mouse. Pull to move. To obtain the mouse position, subtract the abscissa difference stored in step 2 from the mouse x coordinate (e.clientX) as the left value of the dragged element, and subtract the ordinate stored in step 2 from the mouse x coordinate (e.clientY). The difference is used as the top value of the dragged element. Realize the effect of elements following mouse dragging.

4. When the onmouseup event occurs when the mouse button bounces: clear the onmousemove and onmouseup events

The more popular drag and drop plug-in dom-drag class library (author: Aaron Boodman)

The source code is as follows

Copy the code The code is as follows:

/*其中( dom-drag.js )文件**************************************************
* dom-drag.js
* 09.25.2001
* www.youngpup.net
**************************************************
* 10.28.2001 - fixed minor bug where events
* sometimes fired off the handle, not the root.
**************************************************/

var Drag = {

obj : null,

init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
{
o.onmousedown = Drag.start;

o.hmode = bSwapHorzRef ? false : true ;
o.vmode = bSwapVertRef ? false : true ;

o.root = oRoot && oRoot != null ? oRoot : o ;

if (o.hmode && isNaN(parseInt(o.root.style.left ))) o.root.style.left = "0px";
if (o.vmode && isNaN(parseInt(o.root.style.top ))) o.root.style.top = "0px";
if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right = "0px";
if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

o.minX = typeof minX != 'undefined' ? minX : null;
o.minY = typeof minY != 'undefined' ? minY : null;
o.maxX = typeof maxX != 'undefined' ? maxX : null;
o.maxY = typeof maxY != 'undefined' ? maxY : null;

o.xMapper = fXMapper ? fXMapper : null;
o.yMapper = fYMapper ? fYMapper : null;

o.root.onDragStart = new Function();
o.root.onDragEnd = new Function();
o.root.onDrag = new Function();
},

start : function(e)
{
var o = Drag.obj = this;
e = Drag.fixE(e);
var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
o.root.onDragStart(x, y);

o.lastMouseX = e.clientX;
o.lastMouseY = e.clientY;

if (o.hmode) {
if (o.minX != null) o.minMouseX = e.clientX - x o.minX;
if (o.maxX != null) o.maxMouseX = o.minMouseX o.maxX - o.minX;
} else {
if (o.minX != null) o.maxMouseX = -o.minX e.clientX x;
if (o.maxX != null) o.minMouseX = -o.maxX e.clientX x;
}

if (o.vmode) {
if (o.minY != null) o.minMouseY = e.clientY - y o.minY;
if (o.maxY != null) o.maxMouseY = o.minMouseY o.maxY - o.minY;
} else {
if (o.minY != null) o.maxMouseY = -o.minY e.clientY y;
if (o.maxY != null) o.minMouseY = -o.maxY e.clientY y;
}

document.onmousemove = Drag.drag;
document.onmouseup = Drag.end;

return false;
},

drag : function(e)
{
e = Drag.fixE(e);
var o = Drag.obj;

var ey = e.clientY;
var ex = e.clientX;
var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
var nx, ny;

if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

nx = x ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
ny = y ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

if (o.xMapper) nx = o.xMapper(y)
else if (o.yMapper) ny = o.yMapper(x)

Drag.obj.root.style[o.hmode ? "left" : "right"] = nx "px";
Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny "px";
Drag.obj.lastMouseX = ex;
Drag.obj.lastMouseY = ey;

Drag.obj.root.onDrag(nx, ny);
return false;
},

end : function()
{
document.onmousemove = null;
document.onmouseup = null;
Drag.obj.root.onDragEnd( parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]),
parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
Drag.obj = null;
},

fixE : function(e)
{
if (typeof e == 'undefined') e = window.event;
if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
return e;
}
};

二:拖拽排序也是一种常见的效果

常见实现思路

1.将点击进行拖拽的元素转换为绝对路径,同时新建一个临时元素替代其所在的位置。

2.移动过程中通过循环计算鼠标与剩余元素的位置关系,如果鼠标位置处于该元素中时,在该元素的nextSibling前面插入第1步时创建的临时元素;

3.结束时在临时元素前面插入被拖拽元素,删除临时元素。

网上有个冷月无声博主写的挺好的,在此转载一下其代码

以下为其代码
复制代码 代码如下:

(function(win, doc){
var _this = null;
var info = {};
var list = [];
var Sortable = function(opts) {
this.opts = opts;
_this = this;
list = X.getByClass(this.opts.sortClass, doc);
X.addEvent(doc, 'mousedown', this.handleEvent);
X.addEvent(doc, 'mousemove', this.handleEvent);
X.addEvent(doc, 'mouseup', this.handleEvent);
};
Sortable.prototype = {
handleEvent: function(event) {
var e = event || win.event;
var target = event.target || event.srcElement;
switch (event.type) {
case 'mousedown':
X.hasClass(target, _this.opts.sortClass) && _this.downEvent.call(_this, e, target);
break;
case 'mousemove':
info.dObj && _this.moveEvent.call(_this, e, target);
break;
case 'mouseup':
info.dObj && _this.upEvent.call(_this, e, target);
break;
default: break;
}
},
downEvent: function(e, target) {
info.dObj = target;
var off = X.getOffset(target);
target.x = e.clientX - off[0];
target.y = e.clientY - off[1];
target.style.position = 'absolute';
target.style.left = off[0] 'px';
target.style.top = off[1] 'px';

info.vObj = doc.createElement('div');
info.vObj.style.width = off[2] 'px';
info.vObj.style.height = off[3] 'px';
target.parentNode.insertBefore(info.vObj, target);
},
moveEvent: function(e, target) {
win.getSelection ? win.getSelection().removeAllRanges() : doc.selection.empty();
info.dObj.style.left = e.clientX - info.dObj.x 'px';
info.dObj.style.top = e.clientY - info.dObj.y 'px';
for(var i = 0; i < list.length; i ) {
if(list[i] === info.dObj) {
continue;
}
var off = X.getOffset(list[i]);
if(e.clientX > off[0] && e.clientX < off[0] off[2] && e.clientY > off[1] && e.clientY < off[1] off[3]) {
switch (true) {
case e.clientY < (off[1] off[3]) / 2:
list[i].parentNode.insertBefore(info.vObj, list[i]);
break;
case !list[i].nextSibling:
list[i].parentNode.appendChild(info.vObj);
break;
default:
list[i].parentNode.insertBefore(info.vObj, list[i].nextSibling);
break;
}
}
}
},
upEvent: function(e, target) {
info.dObj.style.position = 'static';
info.vObj.parentNode.insertBefore(info.dObj, info.vObj);
info.dObj.parentNode.removeChild(info.vObj);
info = {};
}
};
win.Sortable = Sortable;
})(window, document);
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn