Last active
April 18, 2020 01:10
-
-
Save pablohdzvizcarra/d1343022842a89770d9ee2fb7d29377d to your computer and use it in GitHub Desktop.
get coordinates of an element
This file contains hidden or 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 getCoords(elem) { | |
let box = elem.getBoundingClientRect(); | |
return { | |
top: box.top + window.pageYOffset, | |
left: box.left + windows.pageXOffset | |
} | |
} | |
// add an element with the coordinates obtained | |
function createMessageUnder(element, html) { | |
let message = document.createElement('div'); | |
message.style.cssText = "position:absolute; color:black"; | |
let coords = getCoords(element); | |
message.style.left = coords.left + "px"; | |
message.style.top = coords.bottom + "px"; | |
message.innerHTML = html; | |
return message; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment