[英]In a Html Application, using javascript, how would I get an image's position on the screen an put it in a text document?
I have several images, with different id's, that use the jquery ui draggble div code: 我有几个具有不同ID的图像,它们使用jquery ui draggble div代码:
$( function() {
$( "#t" ).draggable();
} );
How do I get the x and y coordinates (left: px;top: px; in css) that the image is on and put it in a text document? 如何获取图像所在的x和y坐标(在CSS中为left:px; top:px;),并将其放在文本文档中?
Using jQuery UI, you have the position of the element on the "stop" event, which is triggered when the drag stops. 使用jQuery UI,您可以在“停止”事件上放置元素的位置,该事件在拖动停止时触发。
$( ".selector" ).draggable({
stop: function( event, ui ) {
// Here, on the ui object, you have both position and offset
// See below for an explanation of the difference
}
});
For retrieving the position of the element, relative to the offset parent , you should use the position()
api. 要获取元素相对于偏移父元素的位置,应使用
position()
api。 If you want the position relative to the document , you should use the offset()
api. 如果要相对于文档定位,则应使用
offset()
api。
jquery docs : jQuery文档:
When positioning a new element on top of an existing one for global manipulation (in particular, for implementing drag-and-drop), .offset() is more useful.
将新元素放置在现有元素的顶部以进行全局操作(尤其是用于实现拖放操作)时,.offset()更有用。
in both case, you can use the dot syntax to get a specific direction offset or position : 在这两种情况下,都可以使用点语法来获取特定的方向偏移量或位置:
$('#test').offset().left;
$('#test').offset().top;
// -----
$('#test').position().left;
$('#test').position().top;
For storing that information to a file, you may need an API where you send the data via an ajax call, and then do the file writing stuff (help here depend on the backend tech you choose). 为了将信息存储到文件中,您可能需要一个API,在其中您可以通过ajax调用发送数据,然后进行文件编写(此处的帮助取决于您选择的后端技术)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.