這篇文章主要介紹了JS仿iGoogle自定義首頁模塊拖拽特效的方法,實例分析了頁面的布局及拖拽的實現技巧,非常具有實用價值,需要的朋友可以參考下
本文實例講述了JS仿iGoogle自定義首頁模塊拖拽特效的方法。分享給大家供大家參考。具體實現方法如下:
代碼如下:
if (document.documentElement && document.documentElement.scrollTop) {
return {
x: ev.clientX + document.documentElement.scrollLeft - document.documentElement.clientLeft,
y: ev.clientY + document.documentElement.scrollTop - document.documentElement.clientTop
};
}
else if (document.body) {
return {
x: ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y: ev.clientY + document.body.scrollTop - document.body.clientTop
};
}
},
getElementPos: function(el) {
el = this.getItself(el);
var _x = 0, _y = 0;
do {
_x += el.offsetLeft;
_y += el.offsetTop;
} while (el = el.offsetParent);
return { x: _x, y: _y };
},
getItself: function(id) {
return "string" == typeof id ? document.getElementById(id) : id;
},
getViewportSize: { w: (window.innerWidth) ? window.innerWidth : (document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : (document.body?document.body.offsetWidth:0), h: (window.innerHeight) ? window.innerHeight : (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : (document.body ? document.body.offsetHeight : 0) },
isIE: document.all ? true : false,
setOuterHtml: function(obj, html) {
var Objrange = document.createRange();
obj.innerHTML = html;
Objrange.selectNodeContents(obj);
var frag = Objrange.extractContents();
obj.parentNode.insertBefore(frag, obj);
obj.parentNode.removeChild(obj);
},
firstChild: function(parentObj, tagName) {
if (Common.isIE) {
return parentObj.firstChild;
}
else {
var arr = parentObj.getElementsByTagName(tagName);
return arr[0];
}
},
lastChild: function(parentObj, tagName) {
if (Common.isIE) {
return parentObj.lastChild;
}
else {
var arr = parentObj.getElementsByTagName(tagName);
return arr[arr.length - 1];
}
},
setCookie: function(name, value) {
document.cookie = name + "=" + value;
},
getCookie: function(name) {
var strCookie = document.cookie;
var arrCookie = strCookie.split("; ");
for (var i = 0; i < arrCookie.length; i++) {
var arr = arrCookie[i].split("=");
if (!arr[1]) {
return "";
}
if (arr[0] == name) {
return arr[1];
}
}
return "";
},
delCookie: function(name) {
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval = this.getCookie(name);
if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}
}
var Class = {
create: function() {
return function() { this.init.apply(this, arguments); }
}
}
var Drag = Class.create();
Drag.prototype = {
init: function(titleBar, dragDiv, Options) {
//設置點擊是否透明,默認透明60%
titleBar = Common.getItself(titleBar);
dragDiv = Common.getItself(dragDiv);
this.dragArea = { maxLeft: -9999, maxRight: 9999, maxTop: -9999, maxBottom: 9999 };
if (Options) {
this.opacity = Options.opacity ? (isNaN(parseInt(Options.opacity)) ? 100 : parseInt(Options.opacity)) : 100;
if (Options.area) {
if (Options.area.left && !isNaN(parseInt(Options.area.left))) { this.dragArea.maxLeft = Options.area.left };
if (Options.area.right && !isNaN(parseInt(Options.area.right))) { this.dragArea.maxRight = Options.area.right };
if (Options.area.top && !isNaN(parseInt(Options.area.top))) { this.dragArea.maxTop = Options.area.top };
if (Options.area.bottom && !isNaN(parseInt(Options.area.bottom))) { this.dragArea.maxBottom = Options.area.bottom };
}
}
else {
this.opacity = 60;
}
this.originDragDiv = null;
this.tmpX = 0;
this.tmpY = 0;
this.moveable = false;
this.dragArray = [];
var dragObj = this;
var dragTbl = document.getElementById("dragTable");
titleBar.onmousedown = function(e) {
var ev = e || window.event || Common.getEvent();
//只允許通過鼠標左鍵進行拖拽,IE鼠標左鍵為1 FireFox為0
if (Common.isIE && ev.button == 1 || !Common.isIE && ev.button == 0) {
}
else {
return false;
}
//處理特殊情況:在最上/下面MOVE時不碰到現有DIV的情況下,又回到起始拖拽的列最上/下方
var tmpColId;
for (c = 0; c < dragTbl.rows[0].cells.length; c++) {
for (k = 0; k < dragTbl.rows[0].cells[c].getElementsByTagName("DIV").length; k++) {
if (dragDiv.id == dragTbl.rows[0].cells[c].getElementsByTagName("DIV")[k].id) {
tmpColId = c;
break;
}
}
}
var tmpPosFirstChild = Common.getElementPos(Common.firstChild(dragTbl.rows[0].cells[tmpColId], "DIV"));
var tmpPosLastChild = Common.getElementPos(Common.lastChild(dragTbl.rows[0].cells[tmpColId], "DIV"));
var tmpObj = { colId: tmpColId, firstChildUp: tmpPosFirstChild.y, lastChildDown: tmpPosLastChild.y + Common.lastChild(dragTbl.rows[0].cells[tmpColId], "DIV").offsetHeight };
//保存當前可拖拽各容器的所在位置
dragObj.dragArray = dragObj.RegDragsPos();
//插入虛線框
var dashedElement = document.createElement("div");
dashedElement.style.cssText = dragDiv.style.cssText;
dashedElement.style.border = " dashed 2px #aaa ";
dashedElement.style.marginBottom = "6px";
dashedElement.style.width = dragDiv.offsetWidth - 2 * parseInt(dashedElement.style.borderWidth) + "px"; //減去boderWidth使虛線框大小保持與dragDiv一致
dashedElement.style.height = dragDiv.offsetHeight - 2 * parseInt(dashedElement.style.borderWidth) + "px"; //加上px 保證FF正確
dashedElement.style.position = "relative";
if (dragDiv.nextSibling) {