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
/*其中( 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 if(list[i] === info.dObj) {
continue;
}
var off = X.getOffset(list[i]);
if(e.clientX > off[0] && e.clientX off[1] && e.clientY switch (true) {
case e.clientY 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);

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

This post compiles helpful cheat sheets, reference guides, quick recipes, and code snippets for Android, Blackberry, and iPhone app development. No developer should be without them! Touch Gesture Reference Guide (PDF) A valuable resource for desig

jQuery is a great JavaScript framework. However, as with any library, sometimes it’s necessary to get under the hood to discover what’s going on. Perhaps it’s because you’re tracing a bug or are just curious about how jQuery achieves a particular UI

10 fun jQuery game plugins to make your website more attractive and enhance user stickiness! While Flash is still the best software for developing casual web games, jQuery can also create surprising effects, and while not comparable to pure action Flash games, in some cases you can also have unexpected fun in your browser. jQuery tic toe game The "Hello world" of game programming now has a jQuery version. Source code jQuery Crazy Word Composition Game This is a fill-in-the-blank game, and it can produce some weird results due to not knowing the context of the word. Source code jQuery mine sweeping game

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

This tutorial demonstrates how to create a captivating parallax background effect using jQuery. We'll build a header banner with layered images that create a stunning visual depth. The updated plugin works with jQuery 1.6.4 and later. Download the


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools
