[ACCEPTED]-Using JavaScript to increment top/left/bottom/right values-css

Accepted answer
Score: 20

Because style.top is a string with units on the end 9 of it like "300px" you can only do math with it 8 when you convert just the numeric part to 7 an actual number.

Assuming you have a positioned 6 element (so setting the top value will do something) and 5 you already have a top style set directly on 4 the element and not set via CSS (so getting 3 obj.style.top will actually get you something), you can 2 do it by parsing the number out of the style 1 value like this:

var obj = document.getElementById("something");
var topVal = parseInt(obj.style.top, 10);
obj.style.top = (topVal + 300) + "px";

Working example: http://jsfiddle.net/jfriend00/pt46X/

Score: 3

That won't work fine because, for example, if 2 top had a value of 200px, it would become "200px300px". Try 1 this:

var elem = document.getElementById("something");
elem.style.top = parseInt(elem.style.top, 10) + 300 + "px"

Demo WEEEE!!!!

Score: 0
let top = 0;
let left = 0;
let text = document.getElementById("TextToTranslate");
text.setAttribute("style","top:"+top+"px; "+left+":px;");

use this in a while loop and it works fine, i'm 2 just figuring out how to slow it down so 1 i can see the transition

More Related questions