Okay it would seem like it should be simple. I need to take an already existing div and move it according to mouse position within the window. I have searched everywhere and it has led me to over-complicated ways of doing the same thing and involves the use of j-query. I need to strictly use javascript for what I am trying to do.
Method :
var mousePosition;
var div;
(function createDiv(){
div = document.createElement("div");
div.style.position = "absolute";
div.style.left = "0px";
div.style.top = "0px";
div.style.width = "100px";
div.style.height = "100px";
div.style.background = "red";
div.style.color = "blue";
div.addEventListener('mousedown', handleKeyPressed, true);
document.body.appendChild(div);
})();
function handleKeyPressed(event) {
event.preventDefault();
mousePosition = {
x : event.clientX,
y : event.clientY
};
div.style.left = mousePosition.x;
div.style.top = mousePosition.y;
//alert("whoa!");
}
Check if this is smoother than adeneo: FIDDLE
I just made a small change to the @adeneo very well working answer. If everything is enclosed in a function, and every event is attached to the div, you can use it as part of a library.
Call the following function passing an id. If the div does not exist it is created.
I think you're looking for something more like this
FIDDLE
jquery is much easier to deploy. I am surprised you say you don't want to learn it.
You can save the jquery file in your local computer so you do not need internet to use jquery features.
In my case i have saved it in tools folder. So i do not need to be on internet.
For all the js many lines of js code answered above you only need one small line.