Animate on scroll

using IntersectionObserver on JS and data attributes on html

by wkasiban

HTML

<body>
<div class="container">
       <h1>Portraits Paintings in Rijksmeseum</h1>
       <div class="card">
           <div class="image williamI animate" data-animate="animateImage 4s"></div>
           <h2>Portrait of William I</h2>
           <h3 class="animate" data-animate="animateTitle 2s">by : Adriaen Thomasz. Key, c. 1579</h3>
           <p class="animate" data-animate="animateContent 2s">Of the noblemen who rebelled against Phillip II’s Spanish rule of the Netherlands, William of Orange rose to become the great leader of the Dutch Revolt. He defended his own interests, strove for greater independence and advocated freedom of religion. In 1580 Phillip II put a price on his head, and he was assassinated four years later.</p>
       </div>
       <div class="card">
           <div class="image grilInBlue animate" data-animate="animateImage 4s"></div>
           <h2>Protrait of a Gril Dressed in Blue</h2>
           <h3 class="animate" data-animate="animateTitle 2s">by : Johannes Cornelisz. Verspronck, 1641</h3>
           <p class="animate" data-animate="animateContent 2s">Why is this portrait so popular? Because it depicts an adorable child dressed in her Sunday best? As was the custom of the day, the young girl is portrayed as a small adult lady. That she is playing a role is betrayed only by her facial expression. Unfortunately, we know nothing about her identity or her family. Perhaps she resided in Haarlem, like the portraitist Verspronck.</p>
       </div>
       <div class="card">
           <div class="image rambrandt animate" data-animate="animateImage 4s"></div>
           <h2>Self-Portrait as the Apostle Paul</h2>
           <h3 class="animate" data-animate="animateTitle 2s">by : Rembrandt van Rijn, 1661</h3>
           <p class="animate" data-animate="animateContent 2s">Rembrandt is 55 years old in this self-portrait, and he still looks at himself candidly. Here he assumes the guise of a personage from the Bible, the Apostle Paul. The sword...

CSS

@import url('https://fonts.googleapis.com/css2?family=Special+Elite&display=swap');

:root {
    --backgrond-color: #e0c9a6;
    --williamI: url('https://i.ibb.co/Cmgtxkm/2-C3795-D0-5-E3-A-48-BA-9-F52-CE45-ED14-D478.jpg');
    --grilInBlue: url('https://i.ibb.co/vkh81MZ/EABA2-DDB-2-CB4-41-ED-999-A-E45-E080-CA3-C8.jpg');
    --rambrandt: url('https://i.ibb.co/BNtFQp9/5-C57-ED38-B375-42-B1-98-FB-ED2877-C8-F303.jpg');
    --merryDrinker: url("https://i.ibb.co/RytNYM8/46-E54-F76-FFF6-4435-B961-BC2-CFAE97-DD3.jpg");
}

*, *::after, *::before {
    box-sizing: border-box;
}

body {
    background-color: var(--backgrond-color);
    font-family: 'Special Elite', cursive;
}

.container {
    display: flex;
    align-items: center;
    justify-content: space-around;
    flex-direction: column;
}

h1 {
    margin-top: 3rem;
}

h2 {
    margin-top: 2rem;
}

h3 {
    animation: animateTitle 2s infinite;
}

p {
    line-height: 1.8;
    margin-top: 2.5rem;
    animation: animateContent 2s infinite;
}

.card {
    border-radius: 10px;
    box-shadow: 20px 40px 33px rgba(0, 0, 0, 0.3);
    width: 70vh;
    padding: 2rem;
    margin-top: 3rem;
}

.image {
    width: 100%;
    height: 60vh;
    box-shadow: 10px 15px 5px 6px #e0c9a6;
    border-radius: 10px;
    background-size: cover;
    background-position: center center;
    margin-top: 2rem;
    box-shadow: 20px 40px 33px rgba(0, 0, 0, 0.3);
    animation: animateImage 4s infinite;
}

.williamI {
    background-image: var(--williamI);
}

.grilInBlue {
    background-image: var(--grilInBlue);
}

.rambrandt {
    background-image: var(--rambrandt);
}

.merryDrinker {
    background-image: var(--merryDrinker);
}

@keyframes animateTitle {
    from {
        transform: translateX(-50px);
        opacity: 0;
    }
    to {
        transform: translateX(0);
        opacity: 1;
    }
}

@keyframes animateContent {
    from {
        transform: translateX(50px);
        opacity: 0;
    }
    to {
        transform: translateX(0);
    ...

JavaScript

const animate = (entries) => {
    entries.forEach((entry) => {
        if (entry.isIntersecting) {
            console.log("The element is intersecting");
            entry.target.style.animation = entry.target.dataset.animate
        } else {
            entry.target.style.animation = 'none';
        }
    })
}

let observer = new IntersectionObserver(animate);

const animateItems = document.querySelectorAll('.animate')

animateItems.forEach(item => {
    observer.observe(item)
})