简体   繁体   English

在HTML应用程序中,使用javascript,如何将图像在屏幕上的位置放入文本文档中?

[英]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.

相关问题 如何使用 javascript (nodejs) 在此 html 文档中获取特定字段 - How would I get a specific field in this html document using javascript (nodejs) 我应该将JavaScript的加载屏幕脚本放在HTML文档的哪里? - Where should I put JavaScript's loading screen script on HTML Document? 如何在Javascript中获取图像的垂直位置? (就该文件而言) - How do I get the vertical position of an image in Javascript? (in relation to the document) 如何将 HTML 链接的 alt 文本放入 JavaScript? - How do I put HTML link’s alt text into JavaScript? 我如何使用javascript验证html中的文本字段? - How would i validate a text field in html using javascript? 如何使用javascript获取HTML文本节点的位置和大小? - How to get the position and size of a HTML text node using javascript? 如何使用javascript document.getSelecttion()获取html中选定文本的坐标 - how do I get co-ordinates of selected text in an html using javascript document.getSelecttion() 如何将符号或文本放入单击的图像中 position - How to put symbol or text into an image's clicked position 如何在UIWebView中使用javascript获取html文档大小 - How do I get the html document size using javascript in a UIWebView 如何使用JavaScript获取文本顶行的位置 - How do I get the position of text top line using JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM