js+html+css实现鼠标移动div实例_javascript技巧
程序员文章站
2022-05-16 14:36:57
...
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;
}
复制代码 代码如下:
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;
}
推荐阅读
-
鼠标拖拽移动子窗体的JS实现_javascript技巧
-
鼠标滚轮控制网页横向移动实现思路_javascript技巧
-
javascript实现在某个元素上阻止鼠标右键事件的方法和实例_javascript技巧
-
js实现文字跟随鼠标移动而移动的方法_javascript技巧
-
js实现键盘操作实现div的移动或改变的原理及代码_javascript技巧
-
获取鼠标在div中的相对位置的实现代码_javascript技巧
-
可以用鼠标拖动的DIV实现思路及代码_javascript技巧
-
javascript实现简单的鼠标拖动效果实例_javascript技巧
-
js+html+css实现鼠标移动div实例_javascript技巧
-
js实现GridView单选效果自动设置交替行、选中行、鼠标移动行背景色_javascript技巧