2014-09-20 54 views
0

上午有2個不同高度和寬度的div,其圖像爲背景。將一個DIV的座標映射到另一個DIV

enter image description here

我的要求是讓一個div的裁剪座標,並負責協調到另一個DIV位置..我需要的座標..我 如何能做到這一點。

回答

0

看看getBoundingClientRect() ...請注意,您需要照顧父節點。如果div元素的父母有不同的位置,那麼你需要將這個差異變成accout。

下面的代碼假定兩個divs都有相同的父級並且絕對定位。

var bbox = div1.getBoundingClientRect(); 
div2.style.top = bbox.top + 'px'; 
div2.style.left = bboox.left + 'px'; 
div2.style.width = bbox.right - bbox.left + 'px'; 
div2.style.height = bbox.bottom - bbox.top + 'px';