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>m.fzitv.net</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è)計有所幫助。
相關(guān)文章
(currentStyle)javascript為何有時用style得不到已設(shè)定的CSS的屬性
(currentStyle)javascript為何有時用style得不到已設(shè)定的CSS的屬性...2007-08-08
JavaScript實(shí)現(xiàn)音樂導(dǎo)航效果
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)音樂導(dǎo)航效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-11-11
webpack之基礎(chǔ)打包優(yōu)化的實(shí)現(xiàn)
本文主要介紹了webpack之基礎(chǔ)打包優(yōu)化的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下<BR>2022-02-02
詳解JavaScript添加給定的標(biāo)簽選項(xiàng)
這篇文章給大家分享了JavaScript實(shí)現(xiàn)添加給定的標(biāo)簽選項(xiàng)的相關(guān)知識點(diǎn)內(nèi)容以及代碼實(shí)例,有興趣的朋友們參考下。2018-09-09
JS 實(shí)現(xiàn)Div向上浮動的實(shí)現(xiàn)代碼
用js實(shí)現(xiàn)的可以讓div慢慢上升效果實(shí)現(xiàn)代碼,喜歡的朋友可以參考下2012-10-10
JavaScript遍歷求解數(shù)獨(dú)問題的主要思路小結(jié)
數(shù)獨(dú)游戲非常流行,其規(guī)則就是1到9數(shù)字填入9*9宮格并要求每一行、每一列、每一個粗線(小型)宮內(nèi)的數(shù)字不重復(fù),對此我們來看一下JavaScript遍歷求解數(shù)獨(dú)問題的主要思路小結(jié)2016-06-06
跟我學(xué)習(xí)javascript的函數(shù)和函數(shù)表達(dá)式
跟我學(xué)習(xí)javascript的函數(shù)和函數(shù)表達(dá)式,詳細(xì)介紹了函數(shù)表達(dá)式、命名函數(shù)表達(dá)式的方法,感興趣的小伙伴們可以參考一下2015-11-11

