浏览主站 | 站长工具 | 新闻资讯 | 站长学院 | 站长盈利 | HTML教程 | 网址导航 | 站长周刊 | 会员投稿 | 滚动新闻 | RSS
发新话题
打印

类似google拖拽效果的原理实现

类似google拖拽效果的原理实现

[[wiki]wiki[/wiki]][/wiki]页面内容很简单 有三个div  一个用来拖拽的id是“a”,另外三个都是用来放置“a”的;
实现原理不难:
窗口打开后调用MakeElementDraggable函数。
MakeElementDraggable函数功能如下:
把a礫wiki]氖[/wiki]蟊臧聪率录南煊赶

TOP

<!DOC[wiki]type[/wiki] html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html [wiki]XML[/wiki]ns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Drag and Drop Demo</title>
   
    <style type="text/css">
        .dragElement
        {
            background-color:Green;
            
            [wiki]POS[/wiki]ition:absolute;
            z-index:5000;
                        display: block;
                        padding-top:30px;
        }
        
        .dropZone
        {
            background-color:Bl[wiki]UE[/wiki];
            width:300px;
            height:300px;
            position:absolute;
            top:100px;
            right:200px;
        }
        
        .highlightDropZone
        {   
            background-color:Yellow;
        }
        
        
        
        .DefaultDropZoneColor
        {
            background-color:Blue;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
   
    <div id="a" class="dragElement">拖拽窗口 </div>
   
   
    <div id="dZone" style="position:absolute; top:100px; right:200; width:300px; height:300px" class="DefaultDropZoneColor">
    放开区域 1
    </div>
   
    <div id="dZone2" class="DefaultDropZoneColor" style="position:absolute; top:500px; right:200px; width:300px; height:300px">
    放开区域 2
    </div>
   
    <div id="dZone3" class="DefaultDropZoneColor" style="position:absolute; top:300;right:100px; width:100px; height:200px">
    放开区域 3
    </div>
   
   
    </div>
    </form>
</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 dropZone[wiki]object[/wiki];
   
    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)
        {
            result = true;
            break;
        }
    }
   
    return result;
}

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

</script>gf

TOP

发新话题