簡體   English   中英

如何獲得圖像的中心標簽?

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

說我有以下幾點:

document.elementFromPoint(12, 23);

這是一張圖片,如何計算圖片的中心x和y坐標? 從整體上來說相對?

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

應該這樣做:

小提琴: 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) 
    ];
}

希望我沒有對html / body / border / margin / padding有所了解。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM