移动的层

移动的层 

<!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>无标题页</title>
    <link href="Site.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div>
   
   
    <div id="a" class="dragElement"> </div>
   
   
    <div id="dZone" style="position:absolute; top:100px; right:200; width:300px; height:300px" class="DefaultDropZoneColor">
    Drop Zone 1
    </div>
   
    <div id="dZone2" class="DefaultDropZoneColor" style="position:absolute; top:500px; right:200px; width:300px; height:300px">
    Drop Zone 2
    </div>
   
    <div id="dZone3" class="DefaultDropZoneColor" style="position:absolute; top:300;right:100px; width:100px; height:200px">
    Drop Zone 3
    </div>
   
   
    </div>
</body>
</html>
<script language="javascript" type="text/javascript">

var dropZoneArray = new Array(5);
dropZoneArray[0] = "dZone";
dropZoneArray[1] = "dZone2";
dropZoneArray[2] = "dZone3";

var mouseState = 'up';

function MakeElementDraggable(obj)
{
    var startX = 0;
    var startY = 0;

function InitiateDrag(e)
{
    var evt = e || window.event;
   
    startX = parseInt(evt.clientX);
    startY = parseInt(evt.clientY); 
   
    obj.style.top = parseInt(startY) + 'px';
    obj.style.left = parseInt(startX) + 'px'; 
 
   
    document.onmousemove = Drag;
    document.onmouseup = Drop; 
   
    return false;            
}

 

function Drop(e)
{
       var evt = e || window.event;
   
    // check that if we are in the drop zone
    if(IsInDropZone(evt))    
    {       
        document.onmouseup = null;
        document.onmousemove = null;
    }
}

function Drag(e)
{

    // only drag when the mouse is down
      
    var dropZoneObject;
   
    var evt = e || window.event;
   
    obj.style.top = evt.clientY + 'px';
    obj.style.left = evt.clientX + 'px';
   
    // Check if we are in the drop Zone
    if(IsInDropZone(evt))
    {       
        dropZoneObject = evt.srcElement;      
        dropZoneObject.className = 'highlightDropZone';      
    }
   
    else
    { 
        ResetColor();
        
    }
      
   
}

a.onmousedown = InitiateDrag;

}

function ResetColor()
{
    document.getElementById("dZone").className = 'DefaultDropZoneColor';
    document.getElementById("dZone2").className = 'DefaultDropZoneColor';   
    document.getElementById("dZone3").className = 'DefaultDropZoneColor';
      
}

function IsInDropZone(evt)
{
    var result = false;
   
    var obj = evt.srcElement;
   
    // iterate through the array and find it the id exists
    for(i = 0; i < dropZoneArray.length; i++)
    {
        if(obj.id == dropZoneArray[i])
        {
            result = true;
            break;
        }
    }
   
    return result;
}

// make the element draggable
window.onload = MakeElementDraggable(document.getElementById("a"));

</script>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值