Looking for a better way to make all titles slide in upon scrolling instead of coding individually? Consider using a forEach loop! Here's how you can modify the code below:
function slideInLeft() {
document.querySelectorAll('.subtitle-left').forEach(title => {
var greenPosition = title.getBoundingClientRect().top;
var screenPosition = window.innerHeight / 1.2;
if (greenPosition < screenPosition) {
title.classList.add('slide-in-left');
}
});
}
window.addEventListener('scroll', slideInLeft);
.green-box-left {
display: block;
position: relative;
width: 20vw;
height: 6vw;
background: #daf4dc;
}
.subtitle-left {
font-family: 'termina', sans-serif;
font-weight: 600;
font-style: normal;
font-size: 2.3vw;
position: absolute;
color: black;
transition: 1s all;
left: 28%;
margin-top: 9%;
transform: translateX(-139%);
}
.slide-in-left {
transform: translateX(0%);
}
<div class="green-box-left">
<h5 class="subtitle-left">HOMEPAGES</h5>
</div>
<br><br>
<div class="green-box-left">
<h5 class="subtitle-left">HOMEPAGES 2</h5>
</div>