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

24th Jun 2020, 1:44 AM
Zhengrong Yan
Zhengrong Yan - avatar
2 Respostas
+ 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)
24th Jun 2020, 5:43 AM
visph
visph - avatar
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.
24th Jun 2020, 2:39 AM
Sandra Meyer
Sandra Meyer - avatar