日期:2014-05-17 浏览次数:22325 次
<!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" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs
/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript">
  var X,Y;    //div的起始位置
  var sx,sy;  //光标开始的位置
  function move(e){
      $('#box').offset({top:(Y+(e.pageY-sy)),left:(X+(e.pageX-sx))});
  }
$(document).ready(function(){
  $("div").mousedown(function(e){
    var b=$(this).offset();
    X=b.left;
    Y=b.top;
    sx=e.pageX;
    sy=e.pageY;
    $(document).mousemove(move);
  });
  $("div").mouseup(function(){
        X= $('#box').offset().left;
        Y= $('#box').offset().top;
        //$('p').text("X = "+X+" | Y="+Y);
        $(document).unbind("mousemove",move);                    
  });
});
</script>
<title>test</title>
</head>
<body>
    <div style="background-color:#999;width:200px;height:200px;" id="box"> </div>
    <p></p>
</body>
</html>