jQuery 的可拖动对象相对于其容器和同级容器的真实 x 和 y 坐标是多少?
我试图找到容器内图像相对于容器和彼此的正确位置,以便提供碰撞检测并做一些其他有用的事情。现在,不要想象有这样的 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 坐标的真实或正确值是什么,以及如何设置它们并获取它们以供将来使用(从数据库保存/恢复等)?
I'm trying to find the correct placement of images inside a container relative to the container and to eachother in order to provide colision detection and do some other useful stuff. Right now, lest imagine there is an HTML like:
<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>
and some JavaScript code on document ready:
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:
});
What are the true or correct values for the x and y coordinates of the div's relevant to the top left corner of their container, and how to set them and get them for future use (saving/restoring from database, etc.)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为(根据 此页面)该位置应该作为
Inside the
drag 提供您应该能够执行
回调请记住,
position
使用相对于父元素的坐标和相对于页面的offset
。当然,您可以通过添加或减去父坐标(相对于页面)来在两者之间切换。至于在数据库中保存和恢复位置,可以使用jQuery的AJAX方法,这是一个完全不同的问题。
I think (according to this page) that the position should be available as
Inside the
drag
callback you should be able to doRemember that
position
uses coordinates relative the parent element andoffset
relative to the page. Of course you can switch between the two by adding or subtracting the parent coordinates (relative to the page).As for saving and restoring positions in the database, you can use the AJAX methods of jQuery, and is a completely different problem.