in my HTML/CSS web project I wanted to include a CSS-based animation using @keyframes, which worked like a charm. Then I tried adding a possibility to only load the animations when they are visible to the viewer using IntersectionObserver - Intersection Observer API.
For that I followed this tutorial - (section: "Add the class when the element is scrolled into view")
So far so good but here comes my Problem:
I have three classes that are sliding in from the right and are supposed to stop at different widths. For some reason though as soon as I add the intersection observer, the animation is super laggy. They slide in, suddenly stop, slide a little bit more and then jump into the final position instead of sliding smoothly as they were before.
Here is what I have done:
const observer = new IntersectionObserver(entries => {
// Loop over the entries
entries.forEach(entry => {
// If the element is visible
if (entry.isIntersecting) {
// Add the animation class
entry.target.classList.add('rectangle-animation-one','rectangle-animation-two','rectangle-animation-three');
}
});
});
observer.observe(document.querySelector('.rectangle-one'));
observer.observe(document.querySelector('.rectangle-two'));
observer.observe(document.querySelector('.rectangle-three'));
.section-flex{
display:flex;
flex-direction: column;
padding: 30px;
}
.rectangle-one {
margin-left: auto;
height: 125px;
line-height: 125px;
width: 80%;
font-size: 20px;
color: #fff;
text-align: center;
background: #EC6F72;
box-shadow: 0px 7px 24px 3px rgba(0, 0, 0, 0.25);
}
.rectangle-animation-one {
animation-duration: 1s;
animation-name: slidein-one;
animation-iteration-count: 1;
animation-direction:normal;
}
@keyframes slidein-one {
from {
margin-left:100%;
width:300%
}
to {
margin-left:20%;
}
}
.rectangle-two {
margin-top: 0px;
margin-left: auto;
height: 125px;
line-height: 125px;
width: 55%;
font-size: 20px;
color: #fff;
text-align: center;
background: #FFB94D;
box-shadow: 0px 7px 24px 3px rgba(0, 0, 0, 0.25);
}
.rectangle-animation-two {
animation-duration: 1s;
animation-name: slidein-two;
animation-iteration-count: 1;
animation-direction:normal;
}
@keyframes slidein-two {
from {
margin-left:100%;
width:300%
}
to {
margin-left:45%;
}
}
.rectangle-three {
margin-top: 0px;
margin-left: auto;
height: 125px;
line-height: 125px;
width: 40%;
font-size: 20px;
color: #fff;
text-align: center;
background: #3C51AA;
box-shadow: 0px 7px 24px 3px rgba(0, 0, 0, 0.25);
}
.rectangle-animation-three {
animation-duration: 1s;
animation-name: slidein-three;
animation-iteration-count: 1;
animation-direction:normal;
}
@keyframes slidein-three {
from {
margin-left:100%;
width:300%
}
to {
margin-left:60%;
}
}
<div class="sectionflex">
<div class="rectangle-one">
<p>TEXT 1</p>
</div>
<div class="rectangle-two">
<p>TEXT 2</p>
</div>
<div class="rectangle-three">
<p>TEXT 3</p>
</div>
</div>
<script src="app.js"></script>
Any ideas on what I'm doing wrong here? I'm almost certain its the way I'm trying to add all three animations in the javascript code, I've tried all sorts of combinations but can't get it right.
Thanks for any help