0
why do we need "px"
var pos = 0; //our box element var box = document.getElementById("box"); var t = setInterval(move, 10); function move() { if(pos >= 150) { clearInterval(t); } else { pos += 1; box.style.left = pos+"px"; } } // why do we need to add "px" in the code
2 Réponses
+ 1
'px' is not mandatory as you could use other css units, but css unit for a css rule (and it is) is required (among wich 'px' is the most commonly used from Javascript dynamic style update, but not always the best suited -- ie. for responsive designs)
0
This code snippet changes the position defined in CSS. The position is given as an absolute position from left border with a value of previous position on page from left measured in pixels plus 1 per interval.