請訪問此鏈接並運行代碼。jQuery,Move Point和Get Coordination
http://jsfiddle.net/crisply/mQYVY/
簡要解釋,添加綠盒通過單擊[添加框]按鈕爲灰色區域。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="../Scripts/jquery-1.8.2.js"></script>
<script src="../Scripts/jquery-ui-1.8.24.js"></script>
<style type="text/css">
.draggable {
position: absolute;
width: 10px;
height: 10px;
background: green;
cursor: move;
}
#canvas {
width: 500px;
height: 400px;
background: #ccc;
position: relative;
margin: 2em auto;
}
#results {
text-align: center;
background: yellow;
}
</style>
<script type='text/javascript'>
//<![CDATA[
$(function() {
$(".draggable").draggable({
containment: "parent",
});
$('#btn_add').click(function() {
var htmlData = '<div class="draggable"></div>';
$('#canvas').append(htmlData);
$(".draggable").draggable();
});
});
//]]>
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="canvas">
<div class="draggable"></div>
</div>
<div id="results">coordination</div>
<input type='button' id="btn_add" value='Add box' />
<input type='button' id="btn_getCoord" value="Get Coordination" />
</form>
</body>
</html>
除了這段代碼,我想實現更多。
點擊[添加盒]按鈕,=>點生成的隨機位置。
點擊[獲取協調]按鈕, =>獲得幾個點和快速結果格(黃色區域)的協調。
像這樣。
-Coordination-
X:230,Y:222
X:122,Y:233
X:423,Y:55
X:50,Y:30
...
你可以給我一些組件嗎?
我真的很感謝你的幫助。
您可以使用:$(this).position()。left代替splice ... :) –
謝謝!投了你的答案:) – Phoenix
我還有一個問題。爲什麼不能爲新的可拖動div工作** [containment:「parent」] **?新的點可以從畫布出來。謝謝advace – user2423434