js拖拽一些常見(jiàn)的思路方法整理
更新時(shí)間:2014年03月19日 17:04:44 作者:
這篇文章主要介紹了js拖拽一些常見(jiàn)的思路方法,需要的朋友可以參考下
js拖拽的常見(jiàn)思路
1.通過(guò)onmousedown,onmousemove,onmouseup分別模擬開(kāi)始拖拽,拖拽中和拖拽結(jié)束時(shí)的事件()。
如果手機(jī)的觸摸事件的話則分別是ontouchstart,ontouchmove和ontouchend。
2.鼠標(biāo)按下即發(fā)生onmousedown事件時(shí):獲取鼠標(biāo)位置,獲取被拖動(dòng)元素的位置,記錄兩者之間的縱橫坐標(biāo)的差值()。對(duì)document元素綁定onmousemove,onmouseup事件。
剛開(kāi)始接觸js拖拽時(shí),我當(dāng)時(shí)疑惑的是為什么是對(duì)document綁定而不是對(duì)被拖動(dòng)的元素綁定呢?原來(lái)是如果對(duì)被拖動(dòng)元素綁定的話當(dāng)鼠標(biāo)拖動(dòng)過(guò)快時(shí),會(huì)導(dǎo)致鼠標(biāo)與被拖動(dòng)元素的脫離。
3.鼠標(biāo)拖動(dòng)即發(fā)生onmousemove事件時(shí):將被拖拽元素的position改成絕對(duì)位置,這個(gè)可以通過(guò)left和top改變?cè)撛氐奈恢?,從而使得該元素隨著鼠標(biāo)的拖拽而移動(dòng)。獲取鼠標(biāo)位置,將鼠標(biāo)x坐標(biāo)(e.clientX)減去第2步儲(chǔ)存的橫坐標(biāo)差作為被拖動(dòng)元素的left值,將鼠標(biāo)x坐標(biāo)(e.clientY)減去第2步儲(chǔ)存的縱坐標(biāo)差作為被拖動(dòng)元素的top值。實(shí)現(xiàn)元素跟隨鼠標(biāo)拖動(dòng)的效果。
4.鼠標(biāo)按鍵彈起即發(fā)生onmouseup事件時(shí):清空onmousemove和onmouseup事件
比較流行的拖拽插件dom-drag類庫(kù)(作者: Aaron Boodman)
其源代碼如下
/*其中( 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;
}
};
二:拖拽排序也是一種常見(jiàn)的效果
常見(jiàn)實(shí)現(xiàn)思路
1.將點(diǎn)擊進(jìn)行拖拽的元素轉(zhuǎn)換為絕對(duì)路徑,同時(shí)新建一個(gè)臨時(shí)元素替代其所在的位置。
2.移動(dòng)過(guò)程中通過(guò)循環(huán)計(jì)算鼠標(biāo)與剩余元素的位置關(guān)系,如果鼠標(biāo)位置處于該元素中時(shí),在該元素的nextSibling前面插入第1步時(shí)創(chuàng)建的臨時(shí)元素;
3.結(jié)束時(shí)在臨時(shí)元素前面插入被拖拽元素,刪除臨時(shí)元素。
網(wǎng)上有個(gè)冷月無(wú)聲博主寫(xiě)的挺好的,在此轉(zhuǎn)載一下其代碼
以下為其代碼
(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);
1.通過(guò)onmousedown,onmousemove,onmouseup分別模擬開(kāi)始拖拽,拖拽中和拖拽結(jié)束時(shí)的事件()。
如果手機(jī)的觸摸事件的話則分別是ontouchstart,ontouchmove和ontouchend。
2.鼠標(biāo)按下即發(fā)生onmousedown事件時(shí):獲取鼠標(biāo)位置,獲取被拖動(dòng)元素的位置,記錄兩者之間的縱橫坐標(biāo)的差值()。對(duì)document元素綁定onmousemove,onmouseup事件。
剛開(kāi)始接觸js拖拽時(shí),我當(dāng)時(shí)疑惑的是為什么是對(duì)document綁定而不是對(duì)被拖動(dòng)的元素綁定呢?原來(lái)是如果對(duì)被拖動(dòng)元素綁定的話當(dāng)鼠標(biāo)拖動(dòng)過(guò)快時(shí),會(huì)導(dǎo)致鼠標(biāo)與被拖動(dòng)元素的脫離。
3.鼠標(biāo)拖動(dòng)即發(fā)生onmousemove事件時(shí):將被拖拽元素的position改成絕對(duì)位置,這個(gè)可以通過(guò)left和top改變?cè)撛氐奈恢?,從而使得該元素隨著鼠標(biāo)的拖拽而移動(dòng)。獲取鼠標(biāo)位置,將鼠標(biāo)x坐標(biāo)(e.clientX)減去第2步儲(chǔ)存的橫坐標(biāo)差作為被拖動(dòng)元素的left值,將鼠標(biāo)x坐標(biāo)(e.clientY)減去第2步儲(chǔ)存的縱坐標(biāo)差作為被拖動(dòng)元素的top值。實(shí)現(xiàn)元素跟隨鼠標(biāo)拖動(dòng)的效果。
4.鼠標(biāo)按鍵彈起即發(fā)生onmouseup事件時(shí):清空onmousemove和onmouseup事件
比較流行的拖拽插件dom-drag類庫(kù)(作者: Aaron Boodman)
其源代碼如下
復(fù)制代碼 代碼如下:
/*其中( 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;
}
};
二:拖拽排序也是一種常見(jiàn)的效果
常見(jiàn)實(shí)現(xiàn)思路
1.將點(diǎn)擊進(jìn)行拖拽的元素轉(zhuǎn)換為絕對(duì)路徑,同時(shí)新建一個(gè)臨時(shí)元素替代其所在的位置。
2.移動(dòng)過(guò)程中通過(guò)循環(huán)計(jì)算鼠標(biāo)與剩余元素的位置關(guān)系,如果鼠標(biāo)位置處于該元素中時(shí),在該元素的nextSibling前面插入第1步時(shí)創(chuàng)建的臨時(shí)元素;
3.結(jié)束時(shí)在臨時(shí)元素前面插入被拖拽元素,刪除臨時(shí)元素。
網(wǎng)上有個(gè)冷月無(wú)聲博主寫(xiě)的挺好的,在此轉(zhuǎn)載一下其代碼
以下為其代碼
復(fù)制代碼 代碼如下:
(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);
您可能感興趣的文章:
- JQuery Dialog(JS 模態(tài)窗口,可拖拽的DIV)
- js實(shí)現(xiàn)拖拽效果
- Javascript拖拽系列文章2之offsetLeft、offsetTop、offsetWidth、offsetHeight屬性
- javascript支持firefox,ie7頁(yè)面布局拖拽效果代碼
- 關(guān)于js拖拽上傳 [一個(gè)拖拽上傳修改頭像的流程]
- js完美的div拖拽實(shí)例代碼
- javascript拖拽上傳類庫(kù)DropzoneJS使用方法
- 鼠標(biāo)拖拽移動(dòng)子窗體的JS實(shí)現(xiàn)
- 使用js實(shí)現(xiàn)的簡(jiǎn)單拖拽效果
- js 表格排序(編輯+拖拽+縮放)
- js最簡(jiǎn)單的拖拽效果實(shí)現(xiàn)代碼
- Javascript拖拽系列文章1之offsetParent屬性
- javascript實(shí)現(xiàn)移動(dòng)端上的觸屏拖拽功能
- JS實(shí)現(xiàn)的簡(jiǎn)單拖拽功能示例
相關(guān)文章
JavaScript手寫(xiě)一個(gè)前端存儲(chǔ)工具庫(kù)
在項(xiàng)目開(kāi)發(fā)的過(guò)程中,為了減少提高性能,減少請(qǐng)求,開(kāi)發(fā)者往往需要將一些不易改變的數(shù)據(jù)放入本地緩存中。本文就來(lái)用JavaScript手寫(xiě)一個(gè)前端存儲(chǔ)工具庫(kù),希望對(duì)大家有所幫助2023-02-02
JavaScript實(shí)現(xiàn)獲取select下拉框中第一個(gè)值的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)獲取select下拉框中第一個(gè)值的方法,涉及javascript針對(duì)頁(yè)面元素屬性的相關(guān)獲取操作技巧,需要的朋友可以參考下2018-02-02
JavaScript6 let 新語(yǔ)法優(yōu)勢(shì)介紹
這篇文章主要介紹了JavaScript6 let 新語(yǔ)法優(yōu)勢(shì)介紹的相關(guān)資料,需要的朋友可以參考下2016-07-07
用javascript實(shí)現(xiàn)的激活輸入框后隱藏初始內(nèi)容
用javascript實(shí)現(xiàn)的激活輸入框后隱藏初始內(nèi)容...2007-06-06
layer實(shí)現(xiàn)關(guān)閉彈出層刷新父界面功能詳解
這篇文章主要介紹了layer實(shí)現(xiàn)關(guān)閉彈出層刷新父界面功能,結(jié)合實(shí)例形式分析了使用layui的layer在關(guān)閉彈出層時(shí)刷新父界面的常用實(shí)現(xiàn)技巧與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2017-11-11
JavaScript實(shí)現(xiàn)數(shù)組去重的十種方法分享
去重是開(kāi)發(fā)中經(jīng)常會(huì)碰到的一個(gè)熱點(diǎn)問(wèn)題,這篇文章主要介紹了JavaScript中實(shí)現(xiàn)數(shù)組去重的10種方法,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-11-11
javascript實(shí)現(xiàn)input file上傳圖片預(yù)覽效果
這篇文章主要介紹了javascript實(shí)現(xiàn)input file上傳圖片預(yù)覽效果,感興趣的小伙伴們可以參考一下2015-12-12

