javascript實現拖拽並替換網頁塊元素
見圖
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>DragToReplaceDeom</title>
<style>
#displayRoom{background:#eee;position:relative;float:left;clear:both;padding:30px 0px 0px 20px;margin-left:20px;margin-top:10px;}
.row{display:inline-block;float:left;width:90px;clear:none;top:0;/**//*background:#DFEFFC;*/}
.row span{display:inline-block;width:60px;clear:none;background:#9192B1;height:30px;line-height:30px;margin-bottom:30px;text-align:center;}
span.usr{cursor:pointer;position:absolute;display:inline-block;width:60px;clear:none;height:30px;line-height:30px;margin-bottom:30px;text-align:center;background:#4DA74D;color:#fff;}
span.usr.catch{background:#BD9B33!important;}
</style>
</head>
<body>
<div id="displayRoom">
<div class="row">
<span><span class="usr" id="345">學生甲</span></span><span></span><span></span><span></span></div>
<div class="row">
<span><span class="usr" id="123">學生乙</span></span><span id="3"></span><span></span><span></span></div>
<div class="row">
<span></span><span></span><span></span><span></span>
</div>
<div class="row">
<span></span><span><span class="usr" id="456">學生一</span></span><span></span><span></span>
</div>
</div>
<script type="text/javascript">var curTarget = null; //滑鼠拖拽的目標元素
var curPos = null;
var dropTarget = null; //要放下的目標元素
var iMouseDown = false; //滑鼠是否按下
var lMouseState = false; //前一個iMouseDown狀態
var dragreplaceCont = [];
var mouseOffset = null;
var callbackFunc = null;
Number.prototype.NaN0 = function() { return isNaN(this) ? 0 : this; }
function setdragreplace(obj, callback) {
dragreplaceCont.push(obj);
obj.setAttribute('candrag', '1');
if (callback != null && typeof (callback) == 'function') {
callbackFunc = callback;
}
}
//滑鼠移動
function mouseMove(ev) {
ev = ev || window.event;
var target = ev.target || ev.srcElement;
var mousePos = mouseCoords(ev);
//如果當前元素可拖拽
var dragObj = target.getAttribute('candrag');
if (dragObj != null) {
if (iMouseDown && !lMouseState) {
//剛開始拖拽
curTarget = target;
curPos = getPosition(target);
mouseOffset = getMouseOffset(target, ev);
// 清空輔助層
for (var i = 0; i < dragHelper.childNodes.length; i++) dragHelper.removeChild(dragHelper.childNodes[i]);
//克隆元素到輔助層,並移動到滑鼠位置
dragHelper.appendChild(curTarget.cloneNode(true));
dragHelper.style.display = 'block';
dragHelper.firstChild.removeAttribute('candrag');
//記錄拖拽元素的位置資訊
curTarget.setAttribute('startWidth', parseInt(curTarget.offsetWidth));
curTarget.setAttribute('startHeight', parseInt(curTarget.offsetHeight));
curTarget.style.display = 'none';
//記錄每個可接納元素的位置資訊,這裡一次記錄以後多次呼叫,獲取更高效能
for (var i = 0; i < dragreplaceCont.length; i++) {
with (dragreplaceCont[i]) {
if (dragreplaceCont[i] == curTarget)
continue;
var pos = getPosition(dragreplaceCont[i]);
setAttribute('startWidth', parseInt(offsetWidth));
setAttribute('startHeight', parseInt(offsetHeight));
setAttribute('startLeft', pos.x);
setAttribute('startTop', pos.y);
}
} //記錄end
} //剛開始拖拽end
}
//正在拖拽
if (curTarget != null) {
// move our helper div to wherever the mouse is (adjusted by mouseOffset)
dragHelper.style.top = mousePos.y - mouseOffset.y + "px";
dragHelper.style.left = mousePos.x - mouseOffset.x + "px";
//拖拽元素的中點
var xPos = mousePos.x - mouseOffset.x + (parseInt(curTarget.getAttribute('startWidth')) / 2);
var yPos = mousePos.y - mouseOffset.y + (parseInt(curTarget.getAttribute('startHeight')) / 2);
var havedrop = false;
for (var i = 0; i < dragreplaceCont.length; i++) {
with (dragreplaceCont[i]) {
if (dragreplaceCont[i] == curTarget)
continue;
if ((parseInt(getAttribute('startLeft')) < xPos) &&
(parseInt(getAttribute('startTop')) < yPos) &&
((parseInt(getAttribute('startLeft')) + parseInt(getAttribute('startWidth'))) > xPos) &&
((parseInt(getAttribute('startTop')) + parseInt(getAttribute('startHeight'))) > yPos)) {
havedrop = true;
dropTarget = dragreplaceCont[i];
dropTarget.className = 'usr catch';
break;
}
}
}
if (!havedrop && dropTarget != null) {
dropTarget.className = 'usr';
dropTarget = null;
}
} //正在拖拽end
lMouseState = iMouseDown;
if (curTarget) return false; //阻止其它響應(如:滑鼠框選文字)
}
//滑鼠鬆開
function mouseUp(ev) {
if (curTarget) {
dragHelper.style.display = 'none'; //隱藏輔助層
if (curTarget.style.display == 'none' && dropTarget != null) {
//有元素接納,兩者互換
var destP = dropTarget.parentNode;
var sourceP = curTarget.parentNode;
destP.appendChild(curTarget);
sourceP.appendChild(dropTarget);
dropTarget.className = 'usr';
dropTarget = null;
if (callbackFunc != null) {
callbackFunc(curTarget);
}
}
curTarget.style.display = '';
curTarget.style.visibility = 'visible';
curTarget.setAttribute('candrag', '1');
}
curTarget = null;
iMouseDown = false;
}
//滑鼠按下
function mouseDown(ev) {
//記錄變數狀態
iMouseDown = true;
//獲取事件屬性
ev = ev || window.event;
var target = ev.target || ev.srcElement;
if (target.onmousedown || target.getAttribute('candrag')) {//阻止其它響應(如:滑鼠雙擊文字)
return false;
}
}
//返回當前item相對頁面左上角的座標
function getPosition(e) {
var left = 0;
var top = 0;
while (e.offsetParent) {
left += e.offsetLeft + (e.currentStyle ? (parseInt(e.currentStyle.borderLeftWidth)).NaN0() : 0);
top += e.offsetTop + (e.currentStyle ? (parseInt(e.currentStyle.borderTopWidth)).NaN0() : 0);
e = e.offsetParent;
}
left += e.offsetLeft + (e.currentStyle ? (parseInt(e.currentStyle.borderLeftWidth)).NaN0() : 0);
top += e.offsetTop + (e.currentStyle ? (parseInt(e.currentStyle.borderTopWidth)).NaN0() : 0);
return { x: left, y: top };
}
//返回滑鼠相對頁面左上角的座標
function mouseCoords(ev) {
if (ev.pageX || ev.pageY) {
return { x: ev.pageX, y: ev.pageY };
}
return {
x: ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y: ev.clientY + document.body.scrollTop - document.body.clientTop
};
}
//滑鼠位置相對於item的偏移量
function getMouseOffset(target, ev) {
ev = ev || window.event;
var docPos = getPosition(target);
var mousePos = mouseCoords(ev);
return { x: mousePos.x - docPos.x, y: mousePos.y - docPos.y };
}
window.onload = function() {
document.onmousemove = mouseMove;
document.onmousedown = mouseDown;
document.onmouseup = mouseUp;
//輔助層用來顯示拖拽
dragHelper = document.createElement('DIV');
dragHelper.style.cssText = 'position:absolute;display:none;';
document.body.appendChild(dragHelper);
setdragreplace(document.getElementById('345'));
setdragreplace(document.getElementById('123'));
setdragreplace(document.getElementById('456'));
};
</script>
</body>
</html>
轉自:http://www.jb51.net/article/20982.htm
相關文章
- 行內元素與塊狀元素 行內替換元素與行內非替換元素的區別
- HTML 替換元素與非替換元素HTML
- 原生實現元素的拖拽
- HTML 空元素 And 可替換元素HTML
- 空元素和可替換元素
- QT之——QTableWidget拖拽單元格並替換內容(進階)QT
- jQuery實現的將指定元素中的內容替換jQuery
- JavaScript元素拖拽路徑回放效果JavaScript
- 使用python將元組轉換成列表,並替換其中元素Python
- javascript點選元素實現當前輪換展現效果JavaScript
- 什麼是可替換元素?
- JS實現網頁換膚功能效果JS網頁
- 網頁設計師的字型替換方法指南網頁
- 將每個元素替換為右側最大元素
- 頁面替換演算法演算法
- 實現最簡單的模板替換
- javascript的簡單模板替換JavaScript
- javascript實現網頁截圖匯出方案JavaScript網頁
- javascript實現網頁截圖操作介紹JavaScript網頁
- JavaScript實現區塊鏈JavaScript區塊鏈
- 實現java讀取網頁內容並下載網頁中出現的圖片Java網頁
- 實現element-ui對話方塊可拖拽功能UI
- JavaScript使網頁顯示動態效果並實現與使用者互動功能。JavaScript網頁
- [MAUI]實現動態拖拽排序網格UI排序
- 關於.net實現網站模板機制(非標籤替換)網站
- Cypress實現拖拽
- 塊級元素與內聯元素相互轉換
- 內聯元素和塊級元素相互轉換
- javascript實現網頁平滑定位程式碼例項JavaScript網頁
- JavaScript 替換字串全部指定內容JavaScript字串
- JavaScript replace()替換字串中指定字元JavaScript字串字元
- CSS 塊級元素和行內元素和行內塊元素 及其相互轉換CSS
- javascript頁面元素座標JavaScript
- 通過定位實現的div塊網頁中固定效果網頁
- LRU快取替換策略及C#實現快取C#
- 萌新如何用Python實現人臉替換?Python
- javascript實現網頁跳轉的程式碼例項JavaScript網頁
- 前端黑科技:使用 JavaScript 實現網頁掃碼功能前端JavaScript網頁