欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js拖拽一些常見的思路方法整理

 更新時間:2014年03月19日 17:04:44   作者:  
這篇文章主要介紹了js拖拽一些常見的思路方法,需要的朋友可以參考下
js拖拽的常見思路

1.通過onmousedown,onmousemove,onmouseup分別模擬開始拖拽,拖拽中和拖拽結(jié)束時的事件()。

如果手機(jī)的觸摸事件的話則分別是ontouchstart,ontouchmove和ontouchend。

2.鼠標(biāo)按下即發(fā)生onmousedown事件時:獲取鼠標(biāo)位置,獲取被拖動元素的位置,記錄兩者之間的縱橫坐標(biāo)的差值()。對document元素綁定onmousemove,onmouseup事件。

剛開始接觸js拖拽時,我當(dāng)時疑惑的是為什么是對document綁定而不是對被拖動的元素綁定呢?原來是如果對被拖動元素綁定的話當(dāng)鼠標(biāo)拖動過快時,會導(dǎo)致鼠標(biāo)與被拖動元素的脫離。

3.鼠標(biāo)拖動即發(fā)生onmousemove事件時:將被拖拽元素的position改成絕對位置,這個可以通過left和top改變該元素的位置,從而使得該元素隨著鼠標(biāo)的拖拽而移動。獲取鼠標(biāo)位置,將鼠標(biāo)x坐標(biāo)(e.clientX)減去第2步儲存的橫坐標(biāo)差作為被拖動元素的left值,將鼠標(biāo)x坐標(biāo)(e.clientY)減去第2步儲存的縱坐標(biāo)差作為被拖動元素的top值。實現(xiàn)元素跟隨鼠標(biāo)拖動的效果。

4.鼠標(biāo)按鍵彈起即發(fā)生onmouseup事件時:清空onmousemove和onmouseup事件

比較流行的拖拽插件dom-drag類庫(作者: 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;
}
};

二:拖拽排序也是一種常見的效果

常見實現(xiàn)思路

1.將點(diǎn)擊進(jìn)行拖拽的元素轉(zhuǎn)換為絕對路徑,同時新建一個臨時元素替代其所在的位置。

2.移動過程中通過循環(huán)計算鼠標(biāo)與剩余元素的位置關(guān)系,如果鼠標(biāo)位置處于該元素中時,在該元素的nextSibling前面插入第1步時創(chuàng)建的臨時元素;

3.結(jié)束時在臨時元素前面插入被拖拽元素,刪除臨時元素。

網(wǎng)上有個冷月無聲博主寫的挺好的,在此轉(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);

相關(guān)文章

  • NodeJS與Mysql的交互示例代碼

    NodeJS與Mysql的交互示例代碼

    實現(xiàn)NodeJS與Mysql的交互首先把Mysql Module裝到NodeJS中,具體實現(xiàn)及結(jié)果截圖如下,有此需求的朋友可以參考下,希望對大家有所幫助
    2013-08-08
  • Javascript中函數(shù)名.length屬性用法分析(對比arguments.length)

    Javascript中函數(shù)名.length屬性用法分析(對比arguments.length)

    這篇文章主要介紹了Javascript中函數(shù)名.length屬性用法,結(jié)合實例形式簡單對比分析了與arguments.length屬性的用法區(qū)別,需要的朋友可以參考下
    2016-09-09
  • 可插入圖片的TEXT文本框

    可插入圖片的TEXT文本框

    這篇文章主要介紹了可插入圖片的TEXT文本框,有需要的朋友可以參考一下
    2013-12-12
  • 如何使用TS對axios的進(jìn)行簡單封裝

    如何使用TS對axios的進(jìn)行簡單封裝

    在vue項目中和后臺交互獲取數(shù)據(jù)這塊,我們通常使用的是axios庫,這篇文章主要給大家介紹了關(guān)于如何使用TS對axios的進(jìn)行簡單封裝的相關(guān)資料,需要的朋友可以參考下
    2022-10-10
  • 詳解javascript表單的Ajax提交插件的使用

    詳解javascript表單的Ajax提交插件的使用

    本文主要介紹了javascript表單的Ajax提交插件的使用,具有一定的參考價值。下面跟著小編一起來看下吧
    2016-12-12
  • HTML5之WebSocket入門3 -通信模型socket.io

    HTML5之WebSocket入門3 -通信模型socket.io

    socket.io能為程序員提供客戶端和服務(wù)端一致的編程體驗,socket.io支持任何的瀏覽器,任何的Mobile設(shè)備。下面通過本篇文章給大家講解HTML5之WebSocket入門3 -通信模型socket.io,需要的朋友可以參考下
    2015-08-08
  • 原生js實現(xiàn)針對Dom節(jié)點(diǎn)的CRUD操作示例

    原生js實現(xiàn)針對Dom節(jié)點(diǎn)的CRUD操作示例

    這篇文章主要介紹了原生js實現(xiàn)針對Dom節(jié)點(diǎn)的CRUD操作,結(jié)合實例形式分析了javascript操作dom節(jié)點(diǎn)的創(chuàng)建、獲取、增刪改查等相關(guān)操作技巧,需要的朋友可以參考下
    2019-08-08
  • webpack打包中path.resolve(__dirname, 'dist')的含義解析

    webpack打包中path.resolve(__dirname, 'dist')的含義解

    這篇文章主要介紹了webpack打包中path.resolve(__dirname, 'dist')的含義解析,path:path.resolve(__dirname, 'dist')就是在打包之后的文件夾上拼接了一個文件夾,在打包時,直接生成,本文給大家講解的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • 詳細(xì)討論JavaScript中的求值策略

    詳細(xì)討論JavaScript中的求值策略

    這篇文章主要介紹了詳細(xì)討論JavaScript中的求值策略,對求值策略感興趣的同學(xué),可以參考下
    2021-04-04
  • JS判斷一個數(shù)是否是水仙花數(shù)

    JS判斷一個數(shù)是否是水仙花數(shù)

    水仙花數(shù)是指一個 n 位數(shù) ( n≥3 ),它的每個位上的數(shù)字的 n 次冪之和等于它本身。下面通過本文給大家分享JS判斷一個數(shù)是否是水仙花數(shù),需要的朋友參考下吧
    2017-06-06

最新評論