Skip to content

Instantly share code, notes, and snippets.

@pablohdzvizcarra
Last active April 18, 2020 01:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pablohdzvizcarra/d1343022842a89770d9ee2fb7d29377d to your computer and use it in GitHub Desktop.
Save pablohdzvizcarra/d1343022842a89770d9ee2fb7d29377d to your computer and use it in GitHub Desktop.
get coordinates of an element
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