// get center position of an html element function getPositionAtCenter(element) { var data = element.getBoundingClientRect(); return { x: data.left + data.width / 2, y: data.top + data.height / 2 }; } // get distance between two elements function getDistanceBetweenElements(a, b) { var aPosition = getPositionAtCenter(a); var bPosition = getPositionAtCenter(b); return Math.sqrt(Math.pow(aPosition.x - bPosition.x, 2) + Math.pow(aPosition.y - bPosition.y, 2)); } function get_distance() { var distance = getDistanceBetweenElements( document.getElementById(object1), document.getElementById(object2) ); return distance; }