div 移动

2011-05-12 10:10 jquery实现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>拖动DIV</title>  
   <style type="text/css">  
    .show{  
     background:#7cd2f8;  
     width:100px;  
     height:100px;  
     text-align:center;  
     position:absolute;  
     z-index:1;  
     left:100px;  
     top:100px;  
    }  
     
   </style>  
   <script type="text/javascript" src="js/jq.js"></script>  
   <script type="text/javascript"><!--  
    $(document).ready(function()  
    {  
        $(".show").mousedown(function(e)//e鼠标事件  
        {  
            $(this).css("cursor","move");//改变鼠标指针的形状  
              
            var offset = $(this).offset();//DIV在页面的位置  
            var x = e.pageX - offset.left;//获得鼠标指针离DIV元素左边界的距离  
            var y = e.pageY - offset.top;//获得鼠标指针离DIV元素上边界的距离  
            $(document).bind("mousemove",function(ev)//绑定鼠标的移动事件,因为光标在DIV元素外面也要有效果,所以要用doucment的事件,而不用DIV元素的事件  
            {  
                $(".show").stop();//加上这个之后  
                  
                var _x = ev.pageX - x;//获得X轴方向移动的值  
                var _y = ev.pageY - y;//获得Y轴方向移动的值  
                  
                $(".show").animate({left:_x+"px",top:_y+"px"},10);  
            });  
              
        });  
          
        $(document).mouseup(function()  
        {  
            $(".show").css("cursor","default");  
            $(this).unbind("mousemove");  
        })  
    })  
     
// --></script>  
</head>  
<body>  
   <div class="show">  
    jquery实现DIV层拖动
   </div>  
</body>  
</html>

 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<html>  
    <head>  
        <title>test2.html</title>  
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">  
        <script type="text/javascript" src="jquery-1.10.2.js"></script>  
       <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->  
        <script type="text/javascript">  
            $(document).ready(function(){
                $("#bu").click(function(){
                    createDiv("hid", "dogdog");
                });
            });
            function createDiv(id, label, left, top)
            {
                $("body").append($("<div style=''>我是可以拖动的</div>").attr("id", id).text(label));
                $("#" + id).css({"background":"red", "position":"absolute", "width":"880", "height":"120"});
                $("#hid").mousedown(function(event){   
                    var offset=$("#hid").offset();   
                    x1=event.clientX-offset.left;   
                    y1=event.clientY-offset.top;   
                    $("#hid").mousemove(function(event){   
                       $("#hid").css("left",(event.clientX-x1)+"px");   
                       $("#hid").css("top",(event.clientY-y1)+"px");   
                    });   
                    $("#hid").mouseup(function(event){   
                     $("#hid").unbind("mousemove");   
                    });   
                });   
            }
       </script>  
    </head>  
    <body>  
        <input type=button value="点我显示div" id="bu">  
    </body>  
</html>
原文地址:https://www.cnblogs.com/dingyingsi/p/3310581.html