Stagger opacity of letters

Text to reveal

Text to reveal

        <html>
	<head>
		<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A==" crossorigin=""/>
		<link rel="stylesheet" href="https://unpkg.com/@picocss/pico@latest/css/pico.min.css">
		<style>
body {
	margin: 0;
	padding: 0;
}

.to-reveal-simple__letter-wrapper {
	display: inline-block;
	overflow: hidden;
}

.to-reveal-simple__letter--space {
	/* set width for empty space */
	width: 0.3em;
}

.to-reveal-multiline-width {
  width: 100px;
}

		</style>
	</head>
	<body>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet-gpx/1.7.0/gpx.min.js"></script>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.8.0/gsap.min.js"></script>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.8.0/ScrollTrigger.min.js"></script>
		<article class="container"><h1>Stagger opacity of letters</h1></article>

		<article class="container">
			<h1 class="to-reveal-stagger">Text to reveal</h1>
			<h1 class="to-reveal-multiline-width to-reveal-multiline-stagger-width">Text to reveal</h1>
		</article>

	</body>
</html>

<script>
		const addRevealFuncionality = (containerSelector) => {
						const textsToRevealEl = document.querySelector(containerSelector);

						const texts = textsToRevealEl.innerText;
						textsToRevealEl.innerText = '';


						texts.split('').forEach((letter) => {
										console.log(letter);
										const spanEl = document.createElement('span');
										const divEl = document.createElement('div');
								    spanEl.appendChild(divEl);
										divEl.innerText = letter;
										divEl.classList.add('to-reveal-simple__letter');
										spanEl.classList.add('to-reveal-simple__letter-wrapper');
										if (letter === ' ') {
														divEl.classList.add('to-reveal-simple__letter--space');
										}
										console.log(textsToRevealEl);
										textsToRevealEl.appendChild(spanEl);
										});

				}

    addRevealFuncionality('.to-reveal-stagger');
		const timeline1 = gsap.timeline({repeat: -1});
		timeline1.add('hide');
		timeline1.to('.to-reveal-stagger .to-reveal-simple__letter', { opacity: 0.5, stagger: 0.1}, 'hide');
		timeline1.add('show');
		timeline1.to('.to-reveal-stagger .to-reveal-simple__letter', { opacity: 1, stagger: 0.1}, 'show');

    addRevealFuncionality('.to-reveal-multiline-stagger-width');
		const timeline2 = gsap.timeline({repeat: -1});
		timeline2.add('hide');
		timeline2.to('.to-reveal-multiline-stagger-width .to-reveal-simple__letter', { opacity: 0.5, stagger: 0.1}, 'hide');
		timeline2.add('show');
		timeline2.to('.to-reveal-multiline-stagger-width .to-reveal-simple__letter', { opacity: 1, stagger: 0.1}, 'show');
</script>