jQuery实现DIV拖拽

    技术2026-01-22  12

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">     <title></title>      <script src="htgl/js/jquery-1.3.2.min.js" type="text/javascript"></script>     <script type="text/javascript">         $(function() {             var _move = false; //移动标记             var _x, _y; //鼠标离控件左上角的相对位置             $(".drag").mousedown(function(e) {                 _move = true;                 _x = e.pageX - parseInt($(".drag").css("left"));                 _y = e.pageY - parseInt($(".drag").css("top"));                 $(".drag").fadeTo(20, 0.5); //点击后开始拖动并透明显示             });             $(document).mousemove(function(e) {                 if (_move) {                     var x = e.pageX - _x; //移动时根据鼠标位置计算控件左上角的绝对位置                     var y = e.pageY - _y;                     $(".drag").css({ top: y, left: x }); //控件新位置                 }             }).mouseup(function() {                 _move = false;                 $(".drag").fadeTo("fast", 1); //松开鼠标后停止移动并恢复成不透明             });         });     </script>          <style>         .drag         {             border: 2px solid #000;              width: 100px;             height: 100px;             cursor: move;             position: absolute;             left: 0;             top: 0;         }     </style> </head> <body>     <form id="form1" runat="server">     <div class="drag">div拖拽     </div>      </form> </body> </html>
    最新回复(0)