亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

js實(shí)現(xiàn)拖拽 閉包函數(shù)詳細(xì)介紹

 更新時(shí)間:2012年11月25日 12:53:40   作者:  
在開(kāi)發(fā)過(guò)程中可能會(huì)使用js實(shí)現(xiàn)拖拽等相關(guān)功能,本文將以此問(wèn)題進(jìn)行深入介紹,需要了解的朋友可以參考下
js拖拽

采用簡(jiǎn)單的閉包實(shí)現(xiàn)方式
復(fù)制代碼 代碼如下:

/**
* Created with JetBrains WebStorm.
* User: lsj
* Date: 12-11-24
* Time: 下午12:59
* To change this template use File | Settings | File Templates.
*/
var dragmanager=(function()
{
//標(biāo)識(shí)移動(dòng)元素z軸坐標(biāo)
var index_z=1;
//當(dāng)前的拖拽元素
var drganow;
//移動(dòng)標(biāo)識(shí)符號(hào)
var dragbegin=false;
//鼠標(biāo)點(diǎn)擊時(shí)距離div左邊距離
var relativex=0;
//鼠標(biāo)點(diǎn)擊時(shí)距離div上邊距離
var relativey=0;
//標(biāo)識(shí)鼠標(biāo)是否移出
var isout=false;
return {
/**
* 為document綁定鼠標(biāo)提起事件,主要防止鼠標(biāo)移動(dòng)過(guò)快跳出el區(qū)域
*/
bingDocOnMouseUp:function()
{
//注冊(cè)全局的onmouseup事件,主要防止鼠標(biāo)移動(dòng)過(guò)快導(dǎo)致鼠標(biāo)和el不同步
document.onmouseup=function(e)
{
var ev = window.event || e;
if(isout && dragbegin)
{
//改變div的相對(duì)位置
drganow.style.left= (ev.clientX-relativex)+'px';
drganow.style.top=(ev.clientY-relativey)+'px';
drganow.style.cursor='normal';
dragbegin=false;
isout=false;
}
}
},
/**
* 將注入的元素綁定事件
* @param el
*/
registerElementEv:function(element)
{
element.onmousedown=function(e)
{
var ev = window.event || e;
var clientx=ev.clientX;
var clienty= ev.clientY;
var left= parseInt(this.style.left.substring(0, this.style.left.indexOf("p")));
var top= parseInt(this.style.top.substring(0, this.style.top.indexOf("p")));
relativex=clientx-left;
relativey=clienty-top;
index_z++;
this.style.zIndex=index_z;
drganow=this;
dragbegin=true;
}
element.onmousemove=function(e)
{
var ev = window.event || e;
//開(kāi)始拖拽
if(dragbegin)
{
//改變div的相對(duì)位置
this.style.left= (ev.clientX-relativex)+'px';
this.style.top=(ev.clientY-relativey)+'px';
this.style.cursor='move';
}
}
element.onmouseout=function(e)
{
isout=true;
}
element.onmouseup=function(e)
{
var ev = window.event || e;
if(dragbegin)
{
//改變div的相對(duì)位置
drganow.style.left= (ev.clientX-relativex)+'px';
drganow.style.top=(ev.clientY-relativey)+'px';
drganow.style.cursor='normal';
dragbegin=false;
}
}
}
}
})();

1.采用閉包的形式實(shí)現(xiàn) ,方便后期的維護(hù),將移動(dòng)過(guò)程所需的變量全部轉(zhuǎn)移進(jìn)gridmanager里面
2.拖拽過(guò)程中 鼠標(biāo)移動(dòng)過(guò)快導(dǎo)致移動(dòng)元素跟不上鼠標(biāo)的移動(dòng),所以要注冊(cè)document.oumouseup事件,該事件的開(kāi)關(guān)是有移動(dòng)元素的onmouseout事件觸發(fā)的
3.拖拽的過(guò)程中可能會(huì)觸發(fā)瀏覽器自身的onmousemove的select事件,可以進(jìn)行屏蔽ie下是onmousemove="document.selection.empty()"

相關(guān)文章

最新評(píng)論