Change this line:
var firstChild = parent.firstChild;
… to this:
var firstChild = parent.firstElementChild;
Otherwise, you’ll sometimes be grabbing the whitespace text nodes.
You don’t need the following code, because appendChild
will automatically move firstChild
:
parent.removeChild( firstChild );
Finally, reset the container’s left to 0 here:
parent.appendChild(firstChild);
parent.style.left= 0;
Otherwise, the -150px
offset will cause the second child to disappear offscreen when the first is appended to the parent.
solved appendChild method in a simple carousel with pure JavaScript [closed]