I have a simple reveal animation created with javascript, the problem im having is that the elements on the same row all enter together and i will like to have a small delay between them that im not beeing able to achieve. On mobile as they stack its ok the problem is with desktop. Hope someone can help me. Many thanks
function reveal() {
var reveals = document.querySelectorAll(".reveal");
for (var i = 0; i < reveals.length; i ) {
var windowHeight = window.innerHeight;
var elementTop = reveals[i].getBoundingClientRect().top;
var elementVisible = 150;
if (elementTop < windowHeight - elementVisible) {
reveals[i].classList.add("active");
} else {
reveals[i].classList.remove("active");
}
}
}
window.addEventListener("scroll", reveal);
*{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Arial", sans-serif;
}
body{
background: #42455a;
}
section{
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
section:nth-child(1){
color: #e0ffff;
}
section:nth-child(2){
color: #42455a;
background: #e0ffff;
}
section:nth-child(3){
color: #e0ffff;
}
section:nth-child(4){
color: #42455a;
background: #e0ffff;
}
section .container{
margin: 100px;
}
section h1{
font-size: 3rem;
margin: 20px;
}
section h2{
font-size: 40px;
text-align: center;
text-transform: uppercase;
}
section .text-container{
display: flex;
}
section .text-container .text-box{
margin: 20px;
padding: 20px;
background: #00c2cb;
}
section .text-container .text-box h3{
font-size: 30px;
text-align: center;
text-transform: uppercase;
margin-bottom: 10px;
}
@media (max-width: 900px){
section h1{
font-size: 2rem;
text-align: center;
}
section .text-container{
flex-direction: column;
}
}
.reveal{
position: relative;
transform: translateY(150px);
opacity: 0;
transition: 1s all ease;
}
.reveal.active{
transform: translateY(0);
opacity: 1;
}
<section>
<h1>Scroll Down to Reveal Elements ↓</h1>
</section>
<section>
<div >
<h2>Caption</h2>
<div >
<div >
<h3>Section Text</h3>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore
eius molestiae perferendis eos provident vitae iste.
</p>
</div>
<div >
<h3>Section Text</h3>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore
eius molestiae perferendis eos provident vitae iste.
</p>
</div>
<div >
<h3>Section Text</h3>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore
eius molestiae perferendis eos provident vitae iste.
</p>
</div>
</div>
</div>
</section>
<section>
<div >
<h2>Caption</h2>
<div >
<div >
<h3>Section Text</h3>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore
eius molestiae perferendis eos provident vitae iste.
</p>
</div>
<div >
<h3>Section Text</h3>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore
eius molestiae perferendis eos provident vitae iste.
</p>
</div>
<div >
<h3>Section Text</h3>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore
eius molestiae perferendis eos provident vitae iste.
</p>
</div>
</div>
</div>
</section>
<section>
<div >
<h2>Caption</h2>
<div >
<div >
<h3>Section Text</h3>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore
eius molestiae perferendis eos provident vitae iste.
</p>
</div>
<div >
<h3>Section Text</h3>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore
eius molestiae perferendis eos provident vitae iste.
</p>
</div>
<div >
<h3>Section Text</h3>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore
eius molestiae perferendis eos provident vitae iste.
</p>
</div>
</div>
</div>
</section>
CodePudding user response:
You could use a timeout to delay them according to their index insed their respective .container
.
Something like
function reveal() {
const windowHeight = window.innerHeight;
const elementVisible = 150;
const visibilityLimit = windowHeight - elementVisible;
const containers = document.querySelectorAll(".container");
containers.forEach(container => {
const reveals = container.querySelectorAll(".reveal");
reveals.forEach((reveal, index) => {
const elementTop = reveal.getBoundingClientRect().top;
if (elementTop < visibilityLimit) {
setTimeout(() => {
reveal.classList.add("active")
}, index * 250);
} else {
reveal.classList.remove("active");
}
});
});
}
window.addEventListener("scroll", reveal);
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Arial", sans-serif;
}
body {
background: #42455a;
}
section {
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
section:nth-child(1) {
color: #e0ffff;
}
section:nth-child(2) {
color: #42455a;
background: #e0ffff;
}
section:nth-child(3) {
color: #e0ffff;
}
section:nth-child(4) {
color: #42455a;
background: #e0ffff;
}
section .container {
margin: 100px;
}
section h1 {
font-size: 3rem;
margin: 20px;
}
section h2 {
font-size: 40px;
text-align: center;
text-transform: uppercase;
}
section .text-container {
display: flex;
}
section .text-container .text-box {
margin: 20px;
padding: 20px;
background: #00c2cb;
}
section .text-container .text-box h3 {
font-size: 30px;
text-align: center;
text-transform: uppercase;
margin-bottom: 10px;
}
@media (max-width: 900px) {
section h1 {
font-size: 2rem;
text-align: center;
}
section .text-container {
flex-direction: column;
}
}
.reveal {
position: relative;
transform: translateY(150px);
opacity: 0;
transition: 1s all ease;
}
.reveal.active {
transform: translateY(0);
opacity: 1;
}
<section>
<h1>Scroll Down to Reveal Elements ↓</h1>
</section>
<section>
<div >
<h2>Caption</h2>
<div >
<div >
<h3>Section Text</h3>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore eius molestiae perferendis eos provident vitae iste.
</p>
</div>
<div >
<h3>Section Text</h3>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore eius molestiae perferendis eos provident vitae iste.
</p>
</div>
<div >
<h3>Section Text</h3>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore eius molestiae perferendis eos provident vitae iste.
</p>
</div>
</div>
</div>
</section>
<section>
<div >
<h2>Caption</h2>
<div >
<div >
<h3>Section Text</h3>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore eius molestiae perferendis eos provident vitae iste.
</p>
</div>
<div >
<h3>Section Text</h3>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore eius molestiae perferendis eos provident vitae iste.
</p>
</div>
<div >
<h3>Section Text</h3>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore eius molestiae perferendis eos provident vitae iste.
</p>
</div>
</div>
</div>
</section>
<section>
<div >
<h2>Caption</h2>
<div >
<div >
<h3>Section Text</h3>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore eius molestiae perferendis eos provident vitae iste.
</p>
</div>
<div >
<h3>Section Text</h3>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore eius molestiae perferendis eos provident vitae iste.
</p>
</div>
<div >
<h3>Section Text</h3>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore eius molestiae perferendis eos provident vitae iste.
</p>
</div>
</div>
</div>
</section>
CodePudding user response:
You can use setTimeout to execute a function with a delay in milliseconds. In the modified reveal function below I used this inside the for loop. By adding half a second (500 ms) every time you call setTimeout you can create the effect of delay.
function reveal() {
var reveals = document.querySelectorAll(".reveal");
let timeOutMili = 0;
for (var i = 0; i < reveals.length; i ) {
setTimeout(function(reveal) {
var windowHeight = window.innerHeight;
var elementTop = reveal.getBoundingClientRect().top;
var elementVisible = 150;
if (elementTop < windowHeight - elementVisible) {
reveal.classList.add("active");
} else {
reveal.classList.remove("active");
}
}, timeOutMili, reveals[i]);
timeOutMili = 500;
}
}