I want to move multiple DIV with arrow keys and javascript (without jquery).
All my DIV have “position:absolute”, etc…
I made a function for this :
function move(orig, val) {
var num = parseInt(orig);
return ((num + val) + "px");
}
And I apply the move function like this :
myDiv.style.left= move(myDiv.style.left, moveX);
And it only “works” cause I noticed that when one of my DIV has a left style < 0, others DIV with left style > 0 “move” faster than him. So if I repeat back and forth, at the end all my DIV has the same left (didn’t try this vertical moves and top value).
Thanks in advance for you help (and excuse me for my bad english).
I suspect
parseIntis not appropriate in your case. If you’re using non-integers, then adding e.g.1.5will effectively act as if you added2because of integer rounding. That will inadvertently make it move faster.You have several options:
parseFloatto keep the decimal part.+orig.replace("px", ""), which works with non-integers (or other tricks like* 1).