javascript dragable的Move對象
更新時間:2009年08月05日 21:07:07 作者:
一個dragable的Move對象,大家可以運行下,測試看下效果。
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁面才能執(zhí)行]
其中比較重要的代碼:
復制代碼 代碼如下:
var Move = {
$: function(id){
return (typeof id == "object") ? id : document.getElementById(id);
},
pageX: function(elem){ //獲取目標elem的X坐標
return elem.offsetParent ? //如果能繼續(xù)得到上一個元素,增加當前的偏移量并繼續(xù)向上遞歸
elem.offsetLeft + this.pageX(elem.offsetParent) : elem.offsetLeft;
},
pageY: function(elem){ //獲取目標elem的Y坐標
return elem.offsetParent ? elem.offsetTop + this.pageX(elem.offsetParent) : elem.offsetTop;
},
make: function(id){
var elem = this.$(id);
var oldXY = null;
var newXY = null;
var x = 0; //記錄初始化是目標elem的x坐標
var y = 0; //記錄初始化是目標elem的y坐標
var t = this;
elem.onmouseover = function(e){
this.style.cursor = "default";
}
elem.onmousedown = function(e){
e = e || window.event;
this.style.position = "absolute";
this.style.cursor = "move";
x = t.pageX(this);
y = t.pageY(this);
var that = this;
oldXY = {
x: e.clientX,
y: e.clientY
}; //獲取鼠標在按下的時候的坐標
document.onmousemove = function(e){
e = e || window.event;
newXY = {
x: e.clientX,
y: e.clientY
}; //獲取鼠標在移動過程中的坐標
that.style.left = (newXY.x - oldXY.x + x) + "px";
that.style.top = (newXY.y - oldXY.y + y) + "px";
that.style.zIndex = "100";
}
}
elem.onmouseup = function(e){
this.style.cursor = "default";
this.style.zIndex = "0";
document.onmousemove = function(e){ //在放開鼠標的時候覆蓋掉mousemove事件函數(shù)
return;
}
}
}
}
相關(guān)文章
在JavaScript中call()與apply()區(qū)別
這篇文章主要介紹了在JavaScript中call()與apply()區(qū)別 的相關(guān)資料,需要的朋友可以參考下2016-01-01
js下獲得客戶端操作系統(tǒng)的函數(shù)代碼(1:vista,2:windows7,3:2000,4:xp,5:2003,6:20
有時候需要在客戶端獲取操作系統(tǒng)的版本,以便更好的給用戶合適的信息,提高用戶體驗,好不容易從網(wǎng)站發(fā)現(xiàn)了這段代碼,分享給大家。2011-10-10

