js實(shí)現(xiàn)iGoogleDivDrag模塊拖動層拖動特效的方法
本文實(shí)例講述了js實(shí)現(xiàn)iGoogleDivDrag模塊拖動層拖動特效的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js實(shí)現(xiàn)iGoogle Div Drag 模塊拖動 層拖動效果</title>
<style type="text/css">
*{margin:0px; padding:0px;}
body{position:relative; width:780px; height:800px; border:1px solid red}
.drag{width:200px; height:100px; border:1px solid #000;margin:20px; background:#fff}
.drag h1{margin:0px; padding:0px; font-size:12px; height:18px; line-height:18px; background:#E0E7F3; text-indent:20px;cursor:move;}
.center{ margin:200px; border:3px solid red}
</style>
<script type="text/javascript"></script>
</head>
<body>
<div class="drag" >
<h1><strong>www.baidu.com</strong></h1>
</div>
<div class="drag" >
<h1>www.163.com</h1>
</div>
<div class="drag" >
<h1><strong>www.dbjr.com.cn</strong></h1>
</div>
<div class="drag"><h1>測試二</h1></div>
<div class="drag"><h1>測試三</h1></div>
<div class="drag"><h1>測試四</h1></div>
<div class="drag"><h1>測試五</h1></div>
</body>
</html>
<script type="text/javascript">
/*
Author : popper.w
Version : v2.0
*/
var DragZindexNumber=0;
function drag(obj){
var ox,oy,ex,xy,tag=0,mask=0;
if(tag==0){
obj.onmousedown=function(e)
{
if(mask==1){return; }
obj.style.zIndex=DragZindexNumber++;
transp(obj,"start")
tag=1;
var e = e||window.event;
ex=getEventOffset(e).offsetX;
ey=getEventOffset(e).offsetY;
ox=parseInt(obj.offsetLeft);
oy=parseInt(obj.offsetTop);
tempDiv=document.createElement("div");
with(tempDiv.style)
{
width=obj.offsetWidth+"px";
height=obj.offsetHeight+"px";
border="1px dotted red";
position="absolute";
left=obj.offsetLeft+"px";
top=obj.offsetTop+"px";
zIndex=999;
}
document.body.appendChild(tempDiv);
this.ele=tempDiv;
fDragStart(tempDiv);
document.body.onmousemove=function(e){
if(tag==1)
{
var e=e||window.event;
tempDiv.style.left=parseInt(e.clientX)-ex+"px";
tempDiv.style.top=parseInt(e.clientY)-ey+"px";
}
else{if(!tempDiv==null)tempDiv.parentNode.removeChild(tempDiv)}
}
tempDiv.onmouseup=function(e)
{
var e=e||window.event;
fDragEnd(tempDiv);
obj.style.position="absolute";
movie(obj,parseInt(e.clientX)-ex-19,parseInt(e.clientY)-ey-20);
tempDiv.parentNode.removeChild(tempDiv);
tag=0;
}
}
}
}
function movie(o,l,t){
var a=1;
mask=1;
var ol=parseInt(o.offsetLeft);
var ot=parseInt(o.offsetTop);
var iTimer=setInterval(function(){
if(a==10)
{
transp(o,"end");
mask=0;
clearInterval(iTimer);
}
o.style.left=ol+a*(l-ol)/10+"px";
o.style.top=ot+a*(t-ot)/10+"px";
a++;
},20);
}
function fCancleBubble(e)
{
var e = window.event || e;
if (e.preventDefault) e.preventDefault();
else e.returnValue = false;
}
function transp(o,mode){
if(mode=="start"){
if(document.all){
o.style.filter = "Alpha(Opacity=50)";
}else{
o.style.opacity = 0.5;
}
}
else {
if(document.all){
o.style.filter = "Alpha(Opacity=100)";
}else{
o.style.opacity = 1;
}
}
}
function getOffset(evt)
{
var target = evt.target;
if (target.offsetLeft == undefined)
{
target = target.parentNode;
}
var pageCoord = getPageCoord(target);
var eventCoord =
{
x: window.pageXOffset + evt.clientX,
y: window.pageYOffset + evt.clientY
};
var offset =
{
offsetX: eventCoord.x - pageCoord.x,
offsetY: eventCoord.y - pageCoord.y
};
return offset;
}
function getPageCoord(element)
{
var coord = {x: 0, y: 0};
while (element)
{
coord.x += element.offsetLeft;
coord.y += element.offsetTop;
element = element.offsetParent;
}
return coord;
}
function getEventOffset(evt)
{
var msg = "";
if (evt.offsetX == undefined)
{
var evtOffsets = getOffset(evt);
msg={offsetX:evtOffsets.offsetX,offsetY:evtOffsets.offsetY};
}
else
{
msg={offsetX:evt.offsetX,offsetY:evt.offsetY};
}
return msg;
}
function fDragStart(XEle)
{
switch(fCkBrs())
{
case 3:
window.getSelection().removeAllRanges();
break;
default:
XEle.setCapture();
break;
}
}
function fDragEnd(XEle)
{
switch(fCkBrs())
{
case 3:
window.getSelection().removeAllRanges();
break;
default:
XEle.releaseCapture();
break;
}
}
function fCkBrs()
{
switch (navigator.appName)
{
case 'Opera': return 2;
case 'Netscape': return 3;
default: return 1;
}
}
var element=document.getElementsByTagName("div");
for(var i=0;i<element.length;i++){
if(element[i].className=="drag"){
drag(element[i])}
}
</script>
希望本文所述對大家的javascript程序設(shè)計(jì)有所幫助。
相關(guān)文章
微信小程序?qū)崿F(xiàn)搜索功能并跳轉(zhuǎn)搜索結(jié)果頁面
本文主要介紹了微信小程序?qū)崿F(xiàn)搜索功能并跳轉(zhuǎn)搜索結(jié)果頁面,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12深入理解JavaScript系列(29):設(shè)計(jì)模式之裝飾者模式詳解
這篇文章主要介紹了深入理解JavaScript系列(29):設(shè)計(jì)模式之裝飾者模式詳解,裝飾者用用于包裝同接口的對象,不僅允許你向方法添加行為,而且還可以將方法設(shè)置成原始對象調(diào)用(例如裝飾者的構(gòu)造函數(shù)),需要的朋友可以參考下2015-03-03淺談JavaScript事件綁定的常用方法及其優(yōu)缺點(diǎn)分析
下面小編就為大家?guī)硪黄獪\談JavaScript事件綁定的常用方法及其優(yōu)缺點(diǎn)分析。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11學(xué)習(xí)JS中的DOM節(jié)點(diǎn)以及操作
本篇文章給大家整理了關(guān)于JS中DOM節(jié)點(diǎn)的相關(guān)知識點(diǎn)以及代碼實(shí)例,有興趣的朋友可以跟著學(xué)習(xí)下。2018-04-04javascript模擬實(shí)現(xiàn)C# String.format函數(shù)功能代碼
這篇文章主要介紹了javascript模擬實(shí)現(xiàn)C# String.format函數(shù)功能,相信大家可以用的到2013-11-11JavaScript遍歷數(shù)組的三種方法map、forEach與filter實(shí)例詳解
這篇文章主要介紹了JavaScript遍歷數(shù)組的三種方法map、forEach與filter,結(jié)合實(shí)例形式詳細(xì)分析了javascript針對數(shù)組遍歷的map、forEach與filter三種方法相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-02-02JavaScript實(shí)現(xiàn)封裝一個快速生成目錄樹的全局腳本
目錄樹可以很好的介紹項(xiàng)目中各文件目錄的用途,幫助讀者了解整個項(xiàng)目結(jié)構(gòu)。本文就來用JavaScript封裝一個快速生成目錄樹的全局腳本,希望對大家有所幫助2023-03-03