(html/javascript)可以用滑鼠自由拖動的div(收藏)

來源:互聯網
上載者:User

<html><head>
<title>測試可動div</title>
<script language='javascript' type='text/javascript'>
var offset_x;
        var offset_y;
        function Milan_StartMove(oEvent)
        {
            var whichButton;
            if(document.all&&oEvent.button==1) whichButton=true;
            else { if(oEvent.button==0)whichButton=true;}
            if(whichButton)
            {
                var oDiv=document.getElementById("oDiv");
                offset_x=parseInt(oEvent.clientX-oDiv.offsetLeft);
                offset_y=parseInt(oEvent.clientY-oDiv.offsetTop);
                document.documentElement.onmousemove=function(mEvent)
                {   
                    var eEvent;
                    if(document.all) eEvent=event;
                    else{eEvent=mEvent;}
                    var oDiv=document.getElementById("oDiv");
                    var x=eEvent.clientX-offset_x;
                    var y=eEvent.clientY-offset_y;
                    oDiv.style.left=(x)+"px";
                    oDiv.style.top=(y)+"px";
                }
            }
        }
function Milan_StopMove(oEvent){document.documentElement.onmousemove=null; }
</script>
</head>
<body>
<div id="oDiv" onmousedown="Milan_StartMove(event)" onmouseup="Milan_StopMove(event)"

style="cursor:move;position:absolute;width:100px;height:60px;border:1px solid

silver;left:100px;top:100px;z-index:9999;"></div>
</body></html>

document.all[]是文檔中所有標籤組成的一個陣列變數,包括了文檔對象中所有元素;
event.button的值:0沒按鍵    1按左鍵   2按右鍵 3按左和右鍵 4按中間鍵   5按左和中間鍵 6按右和中間鍵    7按所有的鍵

下面對此代碼進行改進,模仿window,並且讓它可以蓋住select
<!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>測試可動div</title>
<script language='javascript' type='text/javascript'>
var offset_x;
var offset_y;
function Milan_StartMove(oEvent,div_id)
{
    var whichButton;
    if(document.all&&oEvent.button==1) whichButton=true;
    else { if(oEvent.button==0)whichButton=true;}
    if(whichButton)
    {
        var oDiv=div_id;
        offset_x=parseInt(oEvent.clientX-oDiv.offsetLeft);
        offset_y=parseInt(oEvent.clientY-oDiv.offsetTop);
        document.documentElement.onmousemove=function(mEvent)
        {   
            var eEvent;
            if(document.all) eEvent=event;
            else{eEvent=mEvent;}
            var oDiv=div_id;
            var x=eEvent.clientX-offset_x;
            var y=eEvent.clientY-offset_y;
            oDiv.style.left=(x)+"px";
            oDiv.style.top=(y)+"px";
            var d_oDiv=document.getElementById("disable_"+oDiv.id);
            d_oDiv.style.left=(x)+"px";
            d_oDiv.style.top=(y)+"px";
        }
    }
}
function Milan_StopMove(oEvent){document.documentElement.onmousemove=null; }
function div_Close(o)
{var oDiv=o; oDiv.style.display="none";var d_oDiv=document.getElementById("disable_"+o.id);d_oDiv.style.display="none";}
</script>
</head>
<body>
<div id="oDiv" style="position:absolute;width:100px;height:60px;border:1px solid silver;left:100px;top:100px;z-index:9999;">
<div id="move" onmousedown="Milan_StartMove(event,this.parentNode)" onmouseup="Milan_StopMove(event)"
style="cursor:move;width:100%;height:15px;background-color:#0066cc; font-size:10px;">
<div onclick="div_Close(this.parentNode.parentNode)" style="float:right; width:10px; height:100%; cursor:hand; background-color:#cc3333; color:White;font-size:15px;">X</div>
</div>
<span>測試一下</span>
</div>
<div id="disable_oDiv" style="position:absolute;left:100px;top:100px;width:100px; height:60px; z-index:9998;FILTER:alpha(opacity=50);";>
<iframe src="about:blank" name="hiddenIframe" width="100%" frameborder="0" height="60px" title="遮蓋層"></iframe></div>
<select name="ListHead1$DropDownList3" id="ListHead1_DropDownList3">
<option selected="selected" value=""></option>
<option value="2">3333</option>
<option value="6">1111</option>
<option value="B">222</option>
</select>
</body>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.