JavaScript拖拽图层问题

来源:百度知道 编辑:UC知道 时间:2024/04/28 20:53:12
拖拽图层时鼠标指针变成了系统的禁止拖拽光标
应该如何解决这个问题
<div id="d1" style="position:absolute; cursor:move;" ondrag="drag()"">
</div>
<div id="d1" style="position:absolute; cursor:move;" ondrag="drag()" ondragstart="StartDrag()"">
</div>
var xx,yy;

function StartDrag()
{
xx=event.x;
yy=event.y;
}

function Drag()
{
this.style.left=d1.offsetLeft+(event.x-xx);
d1.style.top=d1.offsetTop+(event.y-yy);
xx=event.x;
yy=event.y;
}

你把Drag()发上来看啊。这样解决不了问题的。

<script>
var xx,yy;
var bStart=false;
function StartDrag()
{
bStart=true;
xx=event.x;
yy=event.y;
d1.onmousemove=Drag;
d1.onmouseup=StopDrag;
}

function Drag()
{
if(bStart)
{
d1.style.left=d1.offsetLeft+(event.x-xx);
d1.style.top=d1.offsetTop+(event.y-yy);
xx=event.x;
yy=event.y;
}
}
function StopDrag()
{
bStart=false;
}
</script>
<div id="d1" style="position:absolute; cursor:move;width:100px;height:100px;background-color:#000000;" onmousedown="StartDrag()">
</div>

不用ondrag,用onmousemove 事件比较好吧, 因为这样拖放时,别的空间不接受拖放对象,可能光标就变了。
firefox好像可以禁用默认的事件行为的来避免这种情况。ie就不知道了。

上次有个网页也问了一个层拖动的问题,我把源代码贴在我的百度空间了。自己去搜索看一下吧。

参考这里的鼠标捕获