简体   繁体   English

在客户端找到正方形的坐标

[英]Find the coordinates of square at client side

I have an image on my page which I can fetch via $('#myimage');我的页面上有一张图片,我可以通过 $('#myimage'); Now I want to find the coordinates of square which I will draw on image.现在我想找到我将在图像上绘制的正方形的坐标。 These end points of square should be exactly such that radius of square should be 20px(the center of the square will be center of image).这些正方形的端点应该正好是正方形的半径应该是20px(正方形的中心将是图像的中心)。 How can I get do the same with JavaScript or jQuery.我怎样才能对 JavaScript 或 jQuery 做同样的事情。

var dims = {x: 20, y: 20},
    obj = $('#myImage'),
    img = obj.offset(),
    coords = {top: img.top + (obj.height()-dims.y)/2,
              left: img.left + (obj.width()-dims.x)/2};

Working example: http://jsfiddle.net/22TMD/工作示例: http://jsfiddle.net/22TMD/

You can use position() instead of offset() if you want it relative to the offset parent and not the document (depends on how you're going to add the square in the markup).如果您希望它相对于偏移父级而不是文档(取决于您将如何在标记中添加正方形),则可以使用position()而不是offset() )。 So too, you can use innerHeight() and outerHeight() depending on the styling of the image.同样,您也可以根据图像的样式使用innerHeight()outerHeight()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM