最好的拖拽js

來源:互聯網
上載者:User

標籤:

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><title>拖拽js</title><style type="text/css">html,body {overflow: hidden;}body,div,{margin: 0;padding: 0;}body {color: #fff;font: 12px/2 Arial;}p {padding: 0 10px;margin-top: 10px;}span {color: #ff0;padding-left: 5px;}#box {position: absolute;width: 300px;height: 150px;background: #D5CDDA;border: 2px solid #ccc;top: 150px;left: 400px;margin: 0;}#drag {height: 25px;cursor: move;background: #724a88;border-bottom: 2px solid #ccc;padding: 0 10px;}</style></head><body><div id="box"><div id="drag">拖動地區</div>被拖動的整個div</div></body></html><script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script><script type="text/javascript">$(function() {moveBox($("#box"), $("#drag"));})//B被拖動的div,BT可拖動地區function moveBox(B, BT) {var bDrag = false;var _x, _y;BT.mousedown(function(event) {var e = event || window.event;bDrag = true;_x = e.pageX - B.position().left;_y = e.pageY - B.position().top;return false})$(document).mousemove(function(event) {if(!bDrag) return false;var e = event || window.event;var x = e.pageX - _x;var y = e.pageY - _y;console.log(B.position());var maxL = $(document).width() - B.outerWidth();var maxT = $(document).height() - B.outerHeight();x = x < 0 ? 0 : x;x = x > maxL ? maxL : x;y = y < 0 ? 0 : y;y = y > maxT ? maxT : y;B.css({left: x,top: y});return false}).mouseup(function() {bDrag = false;return false})}</script>

  

最好的拖拽js

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.