javascript實(shí)現(xiàn)拖放效果
本文實(shí)例為大家分享的是一個拖放的效果,參考的代碼,重構(gòu)以下,加以理解學(xué)習(xí)。
首先來看效果:
拖動div
拖放狀態(tài):未開始
【程序說明】
拖動原理:其實(shí)就是在拖動塊上監(jiān)聽mousedown事件,鼠標(biāo)點(diǎn)擊時,通過事件對象獲取對應(yīng)的坐標(biāo)參數(shù)。然后鼠標(biāo)移動時再監(jiān)聽document上的mousemove事件,獲取鼠標(biāo)的clientX 和clientY坐標(biāo)然后設(shè)置拖動塊的left 和 top。
首先是監(jiān)聽mousedown事件
然后在Start上添加mousemove 和 mouseup 事件
//監(jiān)聽mousemove 和 mouseup事件 EventUtil.addEventHandler(document, "mousemove", this._fM); EventUtil.addEventHandler(document, "mouseup", this._fS);
鼠標(biāo)移動時,設(shè)置拖動塊的left 和 top 屬性 :
if(!this.LockX)this.Drag.style.left = iLeft + "px"; if(!this.LockY)this.Drag.style.top = iTop + "px";
水平和垂直鎖定:通過判斷LockX 和lockY屬性來限制對于的top 和 left 屬性即可。
范圍限制鎖定:通過計(jì)算容器的寬高和拖動塊的寬高差值來設(shè)定最大left值和top值,來限制拖動塊的left值和top值會在一定的范圍里。
完整DEMO:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript拖放效果</title>
<style type="text/css">
</style>
</head>
<body>
<div id="drag-wrap" style="height: 300px;margin:10px;border:5px solid #FF8000;background:#8080C0;position: relative;">
<div class="draggable" id="drag" style="width:100px;height: 100px;position: absolute;top: 100px;left:100px;background:#eee;border:1px solid #aceaac;cursor: move;">拖動div</div>
</div>
<input id="idReset" type="button" value="復(fù)位" />
<input id="idLock" type="button" value="鎖定全部" />
<input id="idLockX" type="button" value="鎖定水平" />
<input id="idLockY" type="button" value="鎖定垂直" />
<input id="idLimit" type="button" value="范圍鎖定" />
<input id="idLimitOff" type="button" value="取消范圍鎖定" />
<br />拖放狀態(tài):<span id="idShow">未開始</span>
<script type="text/javascript">
/**
*工具函數(shù)
*/
var isIE = (document.all) ? true : false ;
var $$ = function(id){
return "string" == typeof id ? document.getElementById(id) : id;
};
var Class = {
create: function() {
return function() { this.initialize.apply(this, arguments); }
}
};
var Extend = function(destination,source){
for(var property in source){
destination[property] = source[property];
}
};
var BindAsEventListener = function(object,func){
return function(event){
return func.call(object,event || window.event);
}
};
var Bind = function(object,func){
return function(){
return func.apply(object,arguments);
}
};
/**
*跨瀏覽器事件對象
*/
var EventUtil = {
//事件注冊處理程序
addEventHandler:function(oTarget,sEventType,fnHandler){
if(oTarget.addEventListener){
oTarget.addEventListener(sEventType,fnHandler,false);
}else if(oTarget.attachEvent){
oTarget.attachEvent("on"+sEventType,fnHandler);
}else{
oTarget["on"+sEventType] = fnHandler;
}
},
//事件移除處理程序
removeEventHandler:function(oTarget,sEventType,fnHandler){
if(oTarget.removeEventListener){
oTarget.removeEventListener(sEventType,fnHandler,false);
}else if(oTarget.detachEvent){
oTarget.detachEvent("on"+sEventType,fnHandler);
}else{
oTarget["on"+sEventType] = null;
}
},
getEvent:function(event){
return event ? event : window.event;
},
getTarget:function(event){
return event.target || event.srcElement;
}
};
/**
*拖放程序
*/
var Drag= Class.create();
Drag.prototype = {
//初始化對象
initialize:function(drag,options){
this.Drag = $$(drag);//拖放對象
this._x = this._y = 0;//記錄鼠標(biāo)相對于拖放對象的位置
//事件對象(用于綁定移除事件)
this._fM = BindAsEventListener(this, this.Move);
this._fS = Bind(this, this.Stop);
this.Drag.style.position = "absolute";
this.marginLeft = this.marginTop = 0;//記錄margin
//設(shè)置參數(shù)
this.setOptions(options);
//獲取相關(guān)參數(shù)及類型轉(zhuǎn)換
this.Limit = !!this.options.Limit;//轉(zhuǎn)換為布爾型
this.mxLeft = parseInt(this.options.mxLeft);
this.mxRight = parseInt(this.options.mxRight);
this.mxTop = parseInt(this.options.mxTop);
this.mxBottom = parseInt(this.options.mxBottom);
this.Lock = !!this.options.Lock;
this.LockX = !!this.options.LockX;
this.LockY = !!this.options.LockY;
this.onStart = this.options.onStart;
this.onMove = this.options.onMove;
this.onStop = this.options.onStop;
this._Handle = $$(this.options.Handle) || this.Drag;
this._mxContainer = $$(this.options.mxContainer) || null;
//監(jiān)聽拖動對象mousedown事件
EventUtil.addEventHandler(this.Drag, "mousedown", BindAsEventListener(this, this.Start));
},
//準(zhǔn)備拖動
Start:function(oEvent){
if(this.Lock){return;}//如果鎖定則不執(zhí)行
//記錄mousedown觸發(fā)時鼠標(biāo)相對于拖放對象的位置
this._x = oEvent.clientX - this.Drag.offsetLeft;
this._y = oEvent.clientY - this.Drag.offsetTop;
//監(jiān)聽mousemove 和 mouseup事件
EventUtil.addEventHandler(document, "mousemove", this._fM);
EventUtil.addEventHandler(document, "mouseup", this._fS);
},
//拖動
Move:function(oEvent){
//設(shè)置移動參數(shù)
var iLeft = oEvent.clientX - this._x , iTop = oEvent.clientY - this._y;
//設(shè)置范圍限制
if(this.Limit){
//設(shè)置范圍參數(shù)
var mxLeft = this.mxLeft, mxRight = this.mxRight, mxTop = this.mxTop, mxBottom = this.mxBottom;
//如果設(shè)置了容器,再修正范圍參數(shù)
if(!!this._mxContainer){
mxLeft = Math.max(mxLeft, 0);
mxTop = Math.max(mxTop, 0);
mxRight = Math.min(mxRight, this._mxContainer.clientWidth);
mxBottom = Math.min(mxBottom, this._mxContainer.clientHeight);
};
//修正移動參數(shù)
iLeft = Math.max(Math.min(iLeft, mxRight - this.Drag.offsetWidth), mxLeft);
iTop = Math.max(Math.min(iTop, mxBottom - this.Drag.offsetHeight), mxTop);
}
//XY鎖定
if(!this.LockX)this.Drag.style.left = iLeft + "px";
if(!this.LockY)this.Drag.style.top = iTop + "px";
//執(zhí)行附加程序
this.onMove();
},
//停止拖動
Stop:function(){
EventUtil.removeEventHandler(document, "mousemove", this._fM);
EventUtil.removeEventHandler(document, "mouseup", this._fS);
//執(zhí)行附加程序
this.onStop();
},
//設(shè)置默認(rèn)參數(shù)
setOptions:function(options){
this.options = {//默認(rèn)值
Handle: "",//設(shè)置觸發(fā)對象(不設(shè)置則使用拖放對象)
Limit: false,//是否設(shè)置范圍限制(為true時下面參數(shù)有用,可以是負(fù)數(shù))
mxLeft: 0,//左邊限制
mxRight: 9999,//右邊限制
mxTop: 0,//上邊限制
mxBottom: 9999,//下邊限制
mxContainer: "",//指定限制在容器內(nèi)
LockX: false,//是否鎖定水平方向拖放
LockY: false,//是否鎖定垂直方向拖放
Lock: false,//是否鎖定
Transparent: false,//是否透明
onStart: function(){},//開始移動時執(zhí)行
onMove: function(){},//移動時執(zhí)行
onStop: function(){}//結(jié)束移動時執(zhí)行
};
Extend(this.options, options || {});
}
};
//初始化拖動對象
var drag = new Drag('drag',{mxContainer:'drag-wrap',Limit:true,
onStart: function(){ $$("idShow").innerHTML = "開始拖放"; },
onMove: function(){ $$("idShow").innerHTML = "left:"+this.Drag.offsetLeft+";top:"+this.Drag.offsetTop; },
onStop: function(){ $$("idShow").innerHTML = "結(jié)束拖放"; }
});
$$("idReset").onclick = function(){
drag.Limit = true;
drag.mxLeft = drag.mxTop = 0;
drag.mxRight = drag.mxBottom = 9999;
drag.LockX = drag.LockY = drag.Lock = false;
$$("idShow").innerHTML = "復(fù)位";
}
$$("idLock").onclick = function(){ drag.Lock = true;$$("idShow").innerHTML = "鎖定全部";}
$$("idLockX").onclick = function(){ drag.LockX = true; $$("idShow").innerHTML = "鎖定水平";}
$$("idLockY").onclick = function(){ drag.LockY = true; $$("idShow").innerHTML = "鎖定垂直";}
$$("idLimit").onclick = function(){ drag.mxRight = drag.mxBottom = 200;drag.Limit = true;$$("idShow").innerHTML = "范圍鎖定"; }
$$("idLimitOff").onclick = function(){ drag.Limit = false; $$("idShow").innerHTML = "取消范圍鎖定";}
</script>
</body>
</html>
以上就是javascript實(shí)現(xiàn)拖放效果的代碼,希望對大家的學(xué)習(xí)有所幫助。

