Last active
December 31, 2015 18:59
-
-
Save ceane/8030386 to your computer and use it in GitHub Desktop.
Collection of pure JS functions to get an element's center point
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getElementCenter(elem) { | |
var props = elem.getBoundingClientRect(), | |
x = 0, | |
y = 0; | |
//Calculating X | |
if (props.left > props.right) | |
x = (props.left - props.right)/2 + props.right; | |
else if (props.left < props.right) | |
x = (props.right - props.left)/2 + props.left | |
else x=props.left; | |
//Calculating Y | |
if (props.top > props.bottom) | |
y = (props.top > props.bottom)/2 + props.bottom; | |
else if (props.top < props.bottom) | |
y = (props.bottom - props.top)/2 + props.top | |
else | |
y=props.top; | |
return {x: x, y: y} | |
} | |
function getElementOffsetCenter(elem) { | |
var x = 0, | |
y = 0, | |
ontop, | |
topoffset = 0; | |
//Calculating X | |
x = elem.offsetWidth / 2 + elem.offsetLeft; | |
//Calculating Y | |
y = elem.offsetHeight / 2 + elem.offsetTop; | |
if (elem.offsetParent) { | |
parent = elem; | |
while (parent.offsetParent) { | |
topoffset += parent.offsetTop; | |
if (parent.offsetParent) { parent = parent.offsetParent; } | |
else break; | |
} | |
y = elem.offsetHeight / 2 +topoffset; | |
} | |
return {x: x, y: y} | |
} | |
function placeDotInCenter(elem) { | |
var dot = document.createElement('span'), | |
points = getElementCenter(elem); | |
dot.setAttribute('style', 'position: absolute; display: block; z-index:9999999999; width: 5px; height: 5px; background: red; top: '+(points.y)+'px; left: '+(points.x)+'px;'); | |
document.body.appendChild(dot); | |
window.addEventListener('scroll', updatePosition, false); | |
window.addEventListener('resize', updatePosition, false); | |
function updatePosition() { | |
points = getElementCenter(elem); | |
dot.setAttribute('style', 'position: absolute; display: block; z-index:99999999999; width: 5px; height: 5px; background: red; top: '+(points.y)+'px; left: '+(points.x)+'px;'); | |
} | |
} | |
function placeDotInOffsetCenter(elem) { | |
var dot = document.createElement('span'), | |
points = getElementOffsetCenter(elem); | |
dot.setAttribute('style', 'position: absolute; display: block; z-index:9999999999999; width: 5px; height: 5px; background: red; top: '+(points.y)+'px; left: '+(points.x)+'px;'); | |
document.body.appendChild(dot); | |
window.addEventListener('scroll', updatePosition, false); | |
window.addEventListener('resize', updatePosition, false); | |
function updatePosition() { | |
points = getElementOffsetCenter(elem); | |
dot.setAttribute('style', 'position: absolute; display: block; z-index:9999999999999; width: 5px; height: 5px; background: red; top: '+(points.y)+'px; left: '+(points.x)+'px;'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment