javascript dragable的Move對象
更新時間:2009年08月05日 21:07:07 作者:
一個dragable的Move對象,大家可以運(yùn)行下,測試看下效果。
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁面才能執(zhí)行]
其中比較重要的代碼:
復(fù)制代碼 代碼如下:
var Move = {
$: function(id){
return (typeof id == "object") ? id : document.getElementById(id);
},
pageX: function(elem){ //獲取目標(biāo)elem的X坐標(biāo)
return elem.offsetParent ? //如果能繼續(xù)得到上一個元素,增加當(dāng)前的偏移量并繼續(xù)向上遞歸
elem.offsetLeft + this.pageX(elem.offsetParent) : elem.offsetLeft;
},
pageY: function(elem){ //獲取目標(biāo)elem的Y坐標(biāo)
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; //記錄初始化是目標(biāo)elem的x坐標(biāo)
var y = 0; //記錄初始化是目標(biāo)elem的y坐標(biāo)
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
}; //獲取鼠標(biāo)在按下的時候的坐標(biāo)
document.onmousemove = function(e){
e = e || window.event;
newXY = {
x: e.clientX,
y: e.clientY
}; //獲取鼠標(biāo)在移動過程中的坐標(biāo)
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){ //在放開鼠標(biāo)的時候覆蓋掉mousemove事件函數(shù)
return;
}
}
}
}
您可能感興趣的文章:
- 基于AngularJS拖拽插件ngDraggable.js實(shí)現(xiàn)拖拽排序功能
- 深究AngularJS中ng-drag、ng-drop的用法
- jquery插件jquery.dragscale.js實(shí)現(xiàn)拖拽改變元素大小的方法(附demo源碼下載)
- js實(shí)現(xiàn)iGoogleDivDrag模塊拖動層拖動特效的方法
- 一起來寫段JS drag拖動代碼
- 一個js拖拽的效果類和dom-drag.js淺析
- 學(xué)習(xí)drag and drop js實(shí)現(xiàn)代碼經(jīng)典之作
- JQuery+drag.js上傳圖片并且實(shí)現(xiàn)圖片拖曳
相關(guān)文章
OpenLayer實(shí)現(xiàn)自定義坐標(biāo)點(diǎn)的繪制
OpenLayers?是一個專為Web?GIS?客戶端開發(fā)提供的JavaScript?類庫包,用于實(shí)現(xiàn)標(biāo)準(zhǔn)格式發(fā)布的地圖數(shù)據(jù)訪問。本文將利用OpenLayer實(shí)現(xiàn)自定義坐標(biāo)點(diǎn)的繪制,感興趣的可以了解一下2022-04-04js 采用delete實(shí)現(xiàn)繼承示例代碼
這篇文章主要介紹了js如何采用delete實(shí)現(xiàn)所謂的繼承,下面有個不錯的示例,大家可以參考下2014-05-05在JavaScript中call()與apply()區(qū)別
這篇文章主要介紹了在JavaScript中call()與apply()區(qū)別 的相關(guān)資料,需要的朋友可以參考下2016-01-01JavaScript實(shí)現(xiàn)一個簡單的圣誕游戲
圣誕節(jié)即將來臨,大家都在發(fā)圣誕樹,小編今天就為大家介紹一個基于JavaScript實(shí)現(xiàn)的圣誕小游戲,文中的示例代碼簡單易懂,感興趣的可以學(xué)習(xí)一下2021-12-12js下獲得客戶端操作系統(tǒng)的函數(shù)代碼(1:vista,2:windows7,3:2000,4:xp,5:2003,6:20
有時候需要在客戶端獲取操作系統(tǒng)的版本,以便更好的給用戶合適的信息,提高用戶體驗(yàn),好不容易從網(wǎng)站發(fā)現(xiàn)了這段代碼,分享給大家。2011-10-10