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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2020-05-13 我要评论
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:
复制代码 代码如下:

<div class="divBody" id="divBody" style="left: 29px; top: 14px;"> <!--这里要加style="left: 29px; top: 14px;" 否则有问题-->
<div class="divHead" id="divHead" style="cursor: move;">
</div>
<div class="content">
</div>
<div class="tail">
</div>
</div>

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们