<%@ Page Language="C#" AutoEventWireup="true" CodeFile="__mouseMoveDrop.aspx.cs" Inherits="__mouseMoveDrop" %>
<!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 runat="server">
<title></title>
<script type="text/javascript">
var x, y, z, down = false, obj
function init() {
obj = event.srcElement //事件触发对象
obj.setCapture() //设置属于当前对象的鼠标捕捉
z = obj.style.zIndex //获取对象的z轴坐标值
//设置对象的z轴坐标值为100,确保当前层显示在最前面
obj.style.zIndex = 100
x = event.offsetX //获取鼠标指针位置相对于触发事件的对象的X坐标
y = event.offsetY //获取鼠标指针位置相对于触发事件的对象的Y坐标
down = true //布尔值,判断鼠标是否已按下,true为按下,false为未按下
}
function moveit() {
//判断鼠标已被按下且onmouseover和onmousedown事件发生在同一对象上
if (down && event.srcElement == obj) {
with (obj.style) {
/*设置对象的X坐标值为文档在X轴方向上的滚动距离加上当前鼠标指针相当于文档对象的X坐标值减鼠标按下时指针位置相对于触发事件的对象的X坐标*/
posLeft = document.body.scrollLeft + event.x - x
/*设置对象的Y坐标值为文档在Y轴方向上的滚动距离加上当前鼠标指针相当于文档对象的Y坐标值减鼠标按下时指针位置相对于触发事件的对象的Y坐标*/
posTop = document.body.scrollTop + event.y - y
}
}
}
function stopdrag() {
//onmouseup事件触发时说明鼠标已经松开,所以设置down变量值为false
down = false
obj.style.zIndex = z //还原对象的Z轴坐标值
obj.releaseCapture() //释放当前对象的鼠标捕捉
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div onmousedown="init()" onmousemove="moveit()" onmouseup="stopdrag()" style="position:absolute;left:20;top:190;100;height:150;border:1px solid #000000;z-index:1;background:#eeeeee">Layer 1</div>
<div onmousedown="init()" onmousemove="moveit()" onmouseup="stopdrag()" style="position:absolute;left:60;top:10;100;height:150;border:1px solid #000000;z-index:2;background:#eeeeee">Layer 2</div>
<div onmousedown="init()" onmousemove="moveit()" onmouseup="stopdrag()" style="position:absolute;left:100;top:90;100;height:150;border:1px solid #000000;z-index:3;background:#eeeeee">Layer 3</div>
</div>
</form>
</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 runat="server">
<title></title>
<script type="text/javascript">
var x, y, z, down = false, obj
function init() {
obj = event.srcElement //事件触发对象
obj.setCapture() //设置属于当前对象的鼠标捕捉
z = obj.style.zIndex //获取对象的z轴坐标值
//设置对象的z轴坐标值为100,确保当前层显示在最前面
obj.style.zIndex = 100
x = event.offsetX //获取鼠标指针位置相对于触发事件的对象的X坐标
y = event.offsetY //获取鼠标指针位置相对于触发事件的对象的Y坐标
down = true //布尔值,判断鼠标是否已按下,true为按下,false为未按下
}
function moveit() {
//判断鼠标已被按下且onmouseover和onmousedown事件发生在同一对象上
if (down && event.srcElement == obj) {
with (obj.style) {
/*设置对象的X坐标值为文档在X轴方向上的滚动距离加上当前鼠标指针相当于文档对象的X坐标值减鼠标按下时指针位置相对于触发事件的对象的X坐标*/
posLeft = document.body.scrollLeft + event.x - x
/*设置对象的Y坐标值为文档在Y轴方向上的滚动距离加上当前鼠标指针相当于文档对象的Y坐标值减鼠标按下时指针位置相对于触发事件的对象的Y坐标*/
posTop = document.body.scrollTop + event.y - y
}
}
}
function stopdrag() {
//onmouseup事件触发时说明鼠标已经松开,所以设置down变量值为false
down = false
obj.style.zIndex = z //还原对象的Z轴坐标值
obj.releaseCapture() //释放当前对象的鼠标捕捉
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div onmousedown="init()" onmousemove="moveit()" onmouseup="stopdrag()" style="position:absolute;left:20;top:190;100;height:150;border:1px solid #000000;z-index:1;background:#eeeeee">Layer 1</div>
<div onmousedown="init()" onmousemove="moveit()" onmouseup="stopdrag()" style="position:absolute;left:60;top:10;100;height:150;border:1px solid #000000;z-index:2;background:#eeeeee">Layer 2</div>
<div onmousedown="init()" onmousemove="moveit()" onmouseup="stopdrag()" style="position:absolute;left:100;top:90;100;height:150;border:1px solid #000000;z-index:3;background:#eeeeee">Layer 3</div>
</div>
</form>
</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>
</head>
<body>
<div id="f" style="position: absolute; 200px; height: 150px; background-color: #ccc; top: 150px; left: 200px; z-index: 101; border: solid 1px blue;">
<div id="title" style="background-color: Blue; cursor: move; height: 20px; color: #fff;font-size: 13px; padding-top: 5px; padding-left: 10px;"> 拖动层 </div>
</div>
<script type="text/javascript">
var posX;
var posY;
fdiv = document.getElementById("f");
document.getElementById("title").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";
}
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<div id="f" style="position: absolute; 200px; height: 150px; background-color: #ccc; top: 150px; left: 200px; z-index: 101; border: solid 1px blue;">
<div id="title" style="background-color: Blue; cursor: move; height: 20px; color: #fff;font-size: 13px; padding-top: 5px; padding-left: 10px;"> 拖动层 </div>
</div>
<script type="text/javascript">
var posX;
var posY;
fdiv = document.getElementById("f");
document.getElementById("title").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";
}
</script>
</body>
</html>