If you do not want to attach the element to the body , the following solution will work.
I approached this question, looking for a solution that would work without binding the div to the body, because I had a mouseover script that I wanted to run when the mouse was over a new element and the element that spawned It. While you're ready to use jQuery and inspired by @Liam William, answer:
var leftOffset = <<VALUE>>; var topOffset = <<VALUE>>; $(element).css("left", leftOffset - element.offset().left); $(element).css("top", topOffset - element.offset().top);
This solution works by subtracting the current left and top position of the element (relative to the body) to move the element to 0, 0. Placing the element wherever you want relative to the body is as easy as adding the left and top offset value.
Michael.Lumley Sep 29 '14 at 16:16 2014-09-29 16:16
source share