拖动DIV层时 层不随滚动条移动 只在窗口里边移动
展开全部
看看是不是这个效果。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
.drag{
position:absolute;
width:150px;
border:2px solid black;
border-top:20px solid black;
top:100px;
padding:5px;
}
#drag1{left:20px;}
#drag2{left:190px;}
#drag3{left:360px;}
#drag4{left:530px;}
</style>
<script language="javascript">
var obj,x,y,dx,dy;
function Setup()
{
if(!document.getElementsByTagName)return;
divs=document.getElementsByTagName("div");
for(i=0;i<divs.length;i++)
{
if(divs[i].className!="drag") continue;
divs[i].onmousedown=Drag;
}
}
function Drag(e)
{
if(!e)var e=window.event;
obj=(e.target)?e.target:e.srcElement;
obj.style.borderColor="red";
dx=x-obj.offsetLeft;
dy=y-obj.offsetTop;
}
function Move(e)
{
if(!e)var e=window.event;
if(e.pageX)
{x=e.pageX;y=pageY;}
else if(e.clientX)
{x=e.clientX;y=e.clientY;}
else return;
if(obj)
{
obj.style.left=x-dx;
obj.style.top=y-dy;
}
}
function Drop()
{
if(!obj) return;
obj.style.borderColor="black";
obj=false;
}
document.onmousemove=Move;
document.onmouseup=Drop;
window.onload=Setup;
</script>
</head>
<body>
<h1>Drag and Drop in JavaScript</h1>
<div class="drag" id="drag1">
<h3>Box #1</h3>
<p>Click one of these boxes and hold the mouse button down to move it to a niew location.</p>
</div>
<div class="drag" id="drag2">
<h3>Box #2</h3>
<p>This is yet another box you can drag and drop.</p>
</div>
<div class="drag" id="drag3">
<h3>Box #3</h3>
<p>Click one of these boxes and hold the mouse button down to move it to a niew location.</p>
</div>
<div class="drag" id="drag4">
<h3>Box #4</h3>
<p>Click one of these boxes and hold the mouse button down to move it to a niew location.</p>
</div>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
.drag{
position:absolute;
width:150px;
border:2px solid black;
border-top:20px solid black;
top:100px;
padding:5px;
}
#drag1{left:20px;}
#drag2{left:190px;}
#drag3{left:360px;}
#drag4{left:530px;}
</style>
<script language="javascript">
var obj,x,y,dx,dy;
function Setup()
{
if(!document.getElementsByTagName)return;
divs=document.getElementsByTagName("div");
for(i=0;i<divs.length;i++)
{
if(divs[i].className!="drag") continue;
divs[i].onmousedown=Drag;
}
}
function Drag(e)
{
if(!e)var e=window.event;
obj=(e.target)?e.target:e.srcElement;
obj.style.borderColor="red";
dx=x-obj.offsetLeft;
dy=y-obj.offsetTop;
}
function Move(e)
{
if(!e)var e=window.event;
if(e.pageX)
{x=e.pageX;y=pageY;}
else if(e.clientX)
{x=e.clientX;y=e.clientY;}
else return;
if(obj)
{
obj.style.left=x-dx;
obj.style.top=y-dy;
}
}
function Drop()
{
if(!obj) return;
obj.style.borderColor="black";
obj=false;
}
document.onmousemove=Move;
document.onmouseup=Drop;
window.onload=Setup;
</script>
</head>
<body>
<h1>Drag and Drop in JavaScript</h1>
<div class="drag" id="drag1">
<h3>Box #1</h3>
<p>Click one of these boxes and hold the mouse button down to move it to a niew location.</p>
</div>
<div class="drag" id="drag2">
<h3>Box #2</h3>
<p>This is yet another box you can drag and drop.</p>
</div>
<div class="drag" id="drag3">
<h3>Box #3</h3>
<p>Click one of these boxes and hold the mouse button down to move it to a niew location.</p>
</div>
<div class="drag" id="drag4">
<h3>Box #4</h3>
<p>Click one of these boxes and hold the mouse button down to move it to a niew location.</p>
</div>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询