DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> JavaScript:Div層拖動效果實例代碼
JavaScript:Div層拖動效果實例代碼
編輯:關於JavaScript     

Div層拖動效果圖:

實現:
CSS:
復制代碼 代碼如下:
<style>
div
{
position:relative;
}
</style>

JS:
復制代碼 代碼如下:
<script type="text/javascript">
var mouseover=true
var xcoor;
var ycoor;
function coordinates()
{

if (event.srcElement.id.indexOf("wishbroad") == 0)
    {
    event.srcElement.style.zIndex = 1000;
    mouseover=true;
    pleft=event.srcElement.style.pixelLeft;
    ptop=event.srcElement.style.pixelTop;
    xcoor=event.clientX;
    ycoor=event.clientY;
    document.onmousemove=moveImage;
    }
}

function moveImage()
{
if (mouseover&&event.button==1)
    {
    event.srcElement.style.pixelLeft=pleft+event.clientX-xcoor;
    event.srcElement.style.pixelTop=ptop+event.clientY-ycoor;
    return false;
    }
}

function mouseup()
{
    event.srcElement.style.zIndex = 1;
    mouseover=false;
}
document.onmousedown=coordinates;
document.onmouseup=mouseup;
</script>

HTML:
復制代碼 代碼如下:
<html>
<head>Div層拖動</head>
<body>
<div id="wishbroad1" style="width:200px; height:100px;border:1px solid black;background:gray;">
中國
</div>
<div id="wishbroad2" style="width:200px; height:100px;border:1px solid red;background:red;">
美國
</div>
<div id="wishbroad3" style="width:200px; height:100px;border:1px solid red;background:green;">
日本
</div>
<div id="wishbroad4" style="width:200px; height:100px;border:1px solid red;background:pink;">
巴西
</div>
</body>
</html>

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved