The problem is the initial setup of display: none . In the browser layout manager, this means that the layout should be executed as if this element were not even in the DOM (this is all the same, mind you). This means that the CSS style is transform: translateX(-200px); will not apply.
Performing this action:
widget.style.display = 'block'; widget.className = 'visible';
launches both modifications essentially at the same time - the layout is only redone after both operators are executed. Insert document.getElementById('widget2').clientWidth; ( clientHeight also works) launches the layout manager for redrawing, thereby registering transform: translateX(-200px) .
As mentioned above, the solution should either use opacity instead of display (this would be my choice) or use setTimeout with a delay of 0 (see Why is setTimeout (fn, 0) sometimes useful? ).
Grüse source share