我自己有写了个拖动的JS,给LZ参考下
<body>
<div id="dragger" onmouseover="this.style.cursor='move'" onmousedown="startMove();" onmouseup="stopMove();" style="position:absolute;border:solid 1px black;width:100px;height:100px;">
</div>
</body>
<script>
var dragOn=false;
var dragging=document.getElementById("dragger");
var draggerWidth=100;
var draggerHeight=100;
var offsetX=0;
var offsetY=0;document.onmousemove=move;function startMove()
{
dragging.style.cursor="default";
dragOn=true;
offsetX=event.clientX-dragging.style.pixelLeft;
offsetY=event.clientY-dragging.style.pixelTop;
}function stopMove()
{
dragging.style.cursor="move";
dragOn=false;
}function move()
{
if(dragOn)
{
var currentX=event.clientX-offsetX;
var currentY=event.clientY-offsetY;
if(currentX<0) currentX=0;
if(currentY<0) currentY=0;
if(currentX+draggerWidth>screen.availWidth) currentX=screen.availWidth-draggerWidth;
if(currentY+draggerHeight>screen.availHeight) currentY=screen.availHeight-draggerHeight;
dragging.style.pixelTop=currentY;
dragging.style.pixelLeft=currentX;
}
}
</script>

解决方案 »

  1.   

    谢谢!我想请问一下:JS中有没有ondrag函数??我在参考手册里看到有这么个函数的,但实际应用中却没发现
      

  2.   

    <script>
    //Dhtml:用ondrag事件简单的实现鼠标拖动物件.
    //原作:风云舞
    var xx=0,yy=0
    </script>
    <img src="http://www.lshdic.com/bbs/image/shajia.jpg" style='position:absolute;' ondragstart="setCapture();xx=event.x-this.offsetLeft;yy=event.y-this.offsetTo 
      

  3.   

    我写了下面的测试页面,为什么没有反映??
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>无标题文档</title>
    <Script>function a(){
     alert("drag")}</Script>
    </head><body >
     
      <div  id=map style=" float:left;position: absolute;top: 20; left:
              20;width:20000; height:4000; background-color:red" ondragend="a()" >
      adf
     </div>
      </div>
    </div></body>
    </html>大家帮忙看一下