【发布时间】:2011-02-16 14:30:40
【问题描述】:
我试图找到容器内图像相对于容器和彼此的正确位置,以便提供碰撞检测并做一些其他有用的事情。现在,不要想象有这样的 HTML:
<div id="imagesContainer">
<div id="image1" ><img src="image1.png" /></div>
<div id="image2" ><img src="image2.png" /></div>
<div id="image3" ><img src="image3.png" /></div>
</div>
还有一些关于文档的 JavaScript 代码已准备就绪:
var positions = {}; // should already contain some x,y pairs of other dragged imagges
$('#imagesContainer div').draggable({
cursor: 'crosshair',
drag: function(event){
var id = // get the draggable's id
positions[id].x = //find the x of the draggable
positions[id].y = //find the y of the draggable
// do calculations on the positions
} // drag:
});
与容器左上角相关的 div 的 x 和 y 坐标的真实或正确值是什么,以及如何设置它们并获取它们以供将来使用(从数据库保存/恢复等) ?
【问题讨论】:
-
我在尝试完成 jQuery UI 演示后写了这个问题
标签: javascript jquery jquery-ui dom draggable