specifically this line: if (isNaN(zIndex) || !zIndex) zIndex = 1; is wrong, because default value of zIndex is auto which is default layer (same as zIndex 0) so should be if (isNaN(zIndex) || !zIndex) zIndex = 0;