jQuery實現拖放,jquery拖放

來源:互聯網
上載者:User

jQuery實現拖放,jquery拖放

position屬性必須設定為absolute,否則不起作用

<!DOCTYPE html><html><head><meta charset="UTF-8"><script src="../public/js/jquery-1.8.3.js"></script><script src="../public/js/jquery-ui-1.10.4.custom.js"></script><title>拖放功能</title><style type="text/css">.drag{position:absolute;width:100px;height:100px;border:1px solid #ddd;background:#FBF2BD;text-align:center;padding:5px;top:100px;left:20px;cursor:move;}  </style><script type="text/javascript">$(document).ready(function(){    var _move=false;                                    //判斷目標對象是否處於移動狀態    var _x,_y;                                          //滑鼠離控制項左上方的相對x、y的位置    $(".drag").click(function(){    }).mousedown(function(e){                           //當按下滑鼠左鍵時        _move=true;                                     //將移動標記設為true        _x=e.pageX-parseInt($(".drag").css("left"));    //得到左上方x的位置        _y=e.pageY-parseInt($(".drag").css("top"));     //得到左上方y的位置        $(".drag").fadeTo(20,0.5);                      //單擊後開始拖動並透明顯示    })    $(document).mousemove(function(e){                  //若開始移動滑鼠且_move標記設定為true        if(_move){                var x=e.pageX-_x;            var y=e.pageY-_y;            $(".drag").css({top:y+'px',left:x+'px'});             //使用css設定控制項的新位置        }    }).mouseup(function(){        _move=false;        $(".drag").fadeTo('fast',1);                    //鬆開滑鼠後停止移動並恢複成不透明    });});</script></head><body>    <div class="drag">單擊這裡進行拖拽</div></body></html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.