在做項目的時候會有一個彈出窗填寫一些資料,在我們想修改這些列表中的內容的時候就會點擊彈出框來修改內容,有時候我們一打開就有可能忘記我們要修改那個內容了,這時候就會想看看之前的內容的時候,又要關掉彈出框,這樣會有點麻煩,這個時候我們可以給彈出框的div做一個鼠標拖動的效果,廢話太多了,看下面的吧:)
html:
<div class="divBody" id="divBody" style="left: 29px; top: 14px;">
? ? ? ? ?<div class="divHead" id="divHead" style="cursor: move;"></div>
? ? ? ? ?<div class="content"></div>
? ? ? ? ?<div class="tail"></div>
</div>
js:
var posX;
var posY;
fdiv = document.getElementById("divBody");
document.getElementById("divHead").onmousedown=function(e)
{
if(!e) e = window.event; //IE
posX = e.clientX - parseInt(fdiv.style.left);
posY = e.clientY - parseInt(fdiv.style.top);
document.onmousemove = mousemove;
}
document.onmouseup = function()
{
document.onmousemove = null;
}
function mousemove(ev)
{
if(ev==null) ev = window.event;//IE
fdiv.style.left = (ev.clientX - posX) + "px";
fdiv.style.top = (ev.clientY - posY) + "px";
}
css:
.divBody{
margin-top:20px;
border: solid #CCC 1px;
width:500px;
height:400px;
position:relative;
z-index:0;
margin-left:auto;
margin-right:auto;
}
.divHead{
width:500px;
height:50px;
background-color:#CCC;
}
.content{
width:500px;
height:300px;
}
.tail{
background:#CCC;
height:50px;
width:500px;
display:table-cell;
vertical-align:middle;
}