本文實例講述了JS仿iGoogle自訂首頁模組拖曳特效的方法。分享給大家供大家參考。具體實作方法如下:
if (document.documentElement && document.documentElement.scrollTop) {
返回{
x: ev.clientX document.documentElement.scrollLeft - document.documentElement.clientLeft,
y: ev.clientY document.documentElement.scrollTop - document.documentElement.clientTop
};
}
else if (document.body) {
返回{
x: ev.clientX document.body.scrollLeft - document.body.clientLeft,
y: ev.clientY document.body.scrollTop - document.body.clientTop
};
}
},
getElementPos: 函數(el) {
el = this.getItself(el);
var _x = 0, _y = 0;
做{
_x = el.offsetLeft;
_y = el.offsetTop;
while (el = el.offsetParent);
回傳 { x: _x, y: _y };
},
getItself: 函數(id) {
回傳「字串」== typeof 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. (document.body ? document.body.offsetHeight: 0) },
isIE: document.all ?真:假,
setOuterHtml: 函數(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: 函數(parentObj, tagName) {
if (Common.isIE) {
返回parentObj.firstChild;
}
否則{
var arr = ParentObj.getElementsByTagName(tagName);
回 arr[0];
}
},
lastChild: 函數(parentObj, tagName) {
if (Common.isIE) {
返回parentObj.lastChild;
}
否則{
var arr = ParentObj.getElementsByTagName(tagName);
return arr[arr.length - 1];
}
},
setCookie: 函數(姓名, 值) {
document.cookie = name "= value;
},
getCookie: 函數(姓名) {
var strCookie = document.cookie;
var arrCookie = strCookie.split("; ");
for (var i = 0; i
var arr = arrCookie[i].split("=");
if (!arr[1]) {
回復「」;
}
if (arr[0] == 姓名) {
回 arr[1];
}
}
返回「」;
},
delCookie: 函數(姓名) {
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 類別 = {
建立:函數(){
return function() { this.init.apply(this, 參數); }
}
}
var Drag = Class.create();
Drag.prototype = {
初始化:函數(titleBar,dragDiv,選項){
//設定點選是否為透明,且預設為透明60%
titleBar = Common.getItself(titleBar);
DragDiv = Common.getItself(dragDiv);
this.dragArea = { maxLeft: -9999, maxRight: 9999, maxTop: -9999, maxBottom: 9999 }; };
若(選項){
this.opacity = 選項.opacity ? (isNaN(parseInt(Options.opacity)) ? 100 : parseInt(OptiOptions.
if (選項.區) {
left && !isNaN(parseInt(Options.area.left))) { max.
right && !isNaN(parseInt(Options.area.right))) {this.dragArea.Right.
if (Options.area.top && !isNaN(parseInt(Options.area.top)) {this.dragArea.Toptop.
if (Options.area.bottom && !isNaN(parseInt(Options.area.bottom))) { Bot.
}
}
否則{
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 alse;
}
//處理特殊情況:在最上/下面MOVE時不碰到現有DIV的情況下,又回到起始拖曳的列最上/下方
var tmpColId;
for (c = 0; c
, (k = 0; k
, break;
}
}
}
var tmpPosFirstChild = Common.getElementPos(Common.firstChild(dragTbl.rows[0].cells[tmpColId], "DIVmp");
var tmpObj = { colId: tmpColId, firstChildUp: tmpPosFirstChild.y, lastChildDown: firstChildUp: tmpPosFirstChild.y, lastChildDown. ).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";
dashedElement.style.height = dragDiv.offsetHeight - 2 * parseInt(dashedElement.style.borderWidth) "px"; //加上px 保固FF
dashedElement.style.position = "relative";
if (dragDiv.nextSibling) {
dragDiv.parentNode.insertBefore(dashedElement, dragDiv.nextSibling);
}
else {
dragDiv.parentNode.appendChild(dashedElement);
}
//拖曳時變成absolute
dragDiv.style.width = dragDiv.offsetWidth "px";
dragDiv.style.position = "absolute";
dragDiv.style.zIndex = dragObj.GetZindex() 1;
var downPos = Common.getMousePos(ev);
dragObj.tmpY = downPos.y - dragDiv.offsetTop;
if (Common.isIE) {
} else {
window.captureEvents(Event.mousemove);
}
dragObj.SetOpacity(dragDiv, dragObj.opacity);
//FireFox 使用容器內拖曳圖片問題
if (ev.preventDefault) {
ev.preventDefault();
ev.stopPropagation();
}
document.onmousemove = function(e) {
if (dragObj.moveable) {
var ev = e || window.event || Common.getEvent();
var ev = e || window.event || Common.getEvent();
//IE 移除容器的拖曳圖片問題
if (document.all) //IE
{
的ev.returnValue = falsese
;
}
var movePos = Common.getMousePos(ev);
dragDiv.style.left = Math.max(Math.min(movePos.m. -Obmaxjoto.Obihai dijali dii端)adOdjkijijidiaki dWiakjm.Ob. px";
dragDiv.style.top = Math.max(Math.min(movePos.y -ragmObBotj.ragmObinjdraA. x";
var targetDiv = null;
for (var k = 0; k
地 if (dragDiv ====Obj.dragArray[i]) {
以來中對中對一點 movePos.xD.Objj.Ldrag.Obj. DragObj.dragArray[k].PosWidth
&& movePos.y > DragObj.dragArray[k].PosTop && movePos.y
) {
targetDiv = document.getElementById(dragObj.dragArray[k].DragId);
if (movePos.y
// 往上移
dashedElement.style.width = targetDiv.offsetWidth - 2 * parseInt(dashedElement.style.borderWidth) "px";
targetDiv.parentNode.insertBefore(dashedElement, targetDiv);
}
否則{
//往下移
dashedElement.style.width = targetDiv.offsetWidth - 2 * parseInt(dashedElement.style.borderWidth) "px";
if (targetDiv.nextSibling) {
targetDiv.parentNode.insertBefore(dashedElement, targetDiv.nextSibling);
}
否則{
targetDiv.parentNode.appendChild(dashedElement);
}
}
}
}
for (j = 0; j
var startLeft = Common.getElementPos(dragTbl.rows[0].cells[j]).x;
if (movePos.x > startLeft && movePos.x
///列無DIV
if (dragTbl.rows[0].cells[j].getElementsByTagName("div").length == 0) {
dashedElement.style.width = DragTbl.rows[0].cells[j].offsetWidth - 2 * parseInt(dashedElement.style.borderWidth) "px";
DragTbl.rows[0].cells[j].appendChild(dashedElement);
}
否則{
var posFirstChild = Common.getElementPos(Common.firstChild(dragTbl.rows[0].cells[j], "DIV"));
var posLastChild = Common.getElementPos(Common.lastChild(dragTbl.rows[0].cells[j], "DIV"));
//處理特殊情況:在最上/下面MOVE時不搬運現有DIV的情況下,又回到開始拖曳的列最上/下面
var tmpUp, tmpDown;
if (tmpObj.colId == j) {
"DIV").offsetHeight;
的"DIV").offsetWidth - 2 * parseInt(dashedElement.style.borderWidth) "px";
的 dragTbl.rows[0].cells[j], "DIV"));
的IV").offsetWidth - 2 * parseInt(dashedElement.style.borderWidth) "px";
的
}
}
}
};
if (dragObj.moveable) {
if (Common.isIE) {
DragDiv.releaseCapture();
}
否則{
window.releaseEvents(dragDiv.mousemove);
}
DragObj.SetOpacity(dragDiv, 100);
DragObj.moveable = false;
DragObj.tmpX = 0;
DragObj.tmpY = 0;
DragDiv.style.left = "";
DragDiv.style.top = "";
DragDiv.style.position = "";
dashedElement.parentNode.insertBefore(dragDiv, dashedElement);
dashedElement.parentNode.removeChild(dashedElement);
}
}
},
SetOpacity: 函數(dragDiv, n) {
if (Common.isIE) {
DragDiv.filters.alpha.opacity = n;
}
否則{
DragDiv.style.opacity = n / 100;
}
},
GetZindex: function() {
var maxZindex = 0;
var divs = document.getElementsByTagName("div");
for (z = 0; z
maxZindex = Math.max(maxZindex, divs[z].style.zIndex);
}
返回 maxZindex;
},
RegDragsPos: function() {
var arrDragDivs = new Array();
var DragTbl = document.getElementById("dragTable");
var tmpDiv, tmpPos;
for (i = 0; i
tmpDiv = DragTbl.getElementsByTagName("div")[i];
if (tmpDiv.className == "dragDiv") {
tmpPos = Common.getElementPos(tmpDiv);
arrDragDivs.push({ DragId: tmpDiv.id, PosLeft: tmpPos.x, PosTop: tmpPos.y, PosWidth: tmpDiv.offsetWidth, PosHeight: tmpDiv.offsetHeight });
}
}
返回 arrDragDivs;
}
}
window.onload = function() {
var DragTbl = document.getElementById("dragTable");
if (Common.getCookie("configArr")) {
var configArr = eval("(" Common.getCookie("configArr") ")");
for (i = 0; i
for (j = 0; j
DragTbl.rows[0].cells[i].appendChild(document.getElementById(configArr[i][j]));
}
}
}
new Drag("titleBar1", "dragDiv1");
new Drag("titleBar2", "dragDiv2");
new Drag("titleBar3", "dragDiv3");
new Drag("titleBar4", "dragDiv4");
new Drag("titleBar5", "dragDiv5");
}
window.onunload = function() {
var DragTbl = document.getElementById("dragTable");
var configArr = "";
for (i = 0; i
var tmpStr = "";
for (j = 0; j
tmpStr = (tmpStr == "" ? "" : ",") "'" DragTbl.rows[0].cells[i].getElementsByTagName("DIV")[j].id "'";
}
configArr = (configArr == "" ? "" : ",") "[" tmpStr "]";
}
//格式如下:[['dragDiv3','dragDiv5'],['dragDiv4','dragDiv1'],['dragDiv2']]
Common.setCookie("configArr", "[" configArr "]");
}
>腳本
.dragDiv,.nodragDiv{
位置:相對;
過濾器:alpha(不透明度=100);
不透明度:1;
邊距底部:6px;
背景顏色:#FFFFFF;
}
風格>
頭>
|
|