首頁  >  文章  >  web前端  >  js+html+css实现鼠标移动div实例_javascript技巧

js+html+css实现鼠标移动div实例_javascript技巧

WBOY
WBOY原創
2016-05-16 17:42:331027瀏覽

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";
}

html:
复制代码 代码如下:










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;
}
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn