简体   繁体   English

如何获得图像的中心标签?

[英]how do I get the center tag of the image?

Say I have the following: 说我有以下几点:

document.elementFromPoint(12, 23);

which is an image, how do I figure out the center x and y coordinate of the image? 这是一张图片,如何计算图片的中心x和y坐标? relative-ly from the whole view? 从整体上来说相对?

ImageXCenter = ImageXPos + Math.abs( ImageWidth / ImageXPos ) / 2;
ImageYCenter = ImageYPos + Math.abs( ImageHeight / ImageYPos ) / 2;

This should do: 应该这样做:

Fiddle: http://jsfiddle.net/9ex2V/ 小提琴: http//jsfiddle.net/9ex2V/

function getCenter (e) {
    var left = e.offsetLeft,
        top = e.offsetTop,
        parent = e;

    while(parent = parent.offsetParent) {
        left += parent.offsetLeft;
        top += parent.offsetTop;  
    }


    return [
      left + Math.floor(e.offsetWidth / 2),
      top + Math.floor(e.offsetHeight / 2) 
    ];
}

Hope I haven't overlooked anything about html/body/anything border/margin/padding. 希望我没有对html / body / border / margin / padding有所了解。

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

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