DEV Community

Robson Muniz
Robson Muniz

Posted on

2 1

Scroll Animation | JavaScript

Learn How to Make a Scroll Animation using CSS and JavaScript, step-by-step from scratch.
As you will notice in this tutorial: one block comes from the right and another one from the left!


<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Scroll Animation</title>
  <link rel="stylesheet" href="style.css">
  <script defer src="app.js"></script>
</head>

<body>
  <h1>Scroll to See 👀 the Animation</h1>

  <div class="box blue show">
    <h2>content</h2>
  </div>
  <div class="box red">
    <h2>content</h2>
  </div>
  <div class="box yellow">
    <h2>content</h2>
  </div>
  <div class="box green">
    <h2>content</h2>
  </div>
  <div class="box blue">
    <h2>content</h2>
  </div>
  <div class="box red">
    <h2>content</h2>
  </div>
  <div class="box yellow">
    <h2>content</h2>
  </div>
  <div class="box green">
    <h2>content</h2>
  </div>
  <div class="box blue">
    <h2>content</h2>
  </div>
  <div class="box red">
    <h2>content</h2>
  </div>
  <div class="box yellow">
    <h2>content</h2>
  </div>
  <div class="box green">
    <h2>content</h2>
  </div>

</body>

</html>
Enter fullscreen mode Exit fullscreen mode

CSS

@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;700&display=swap');

* {
    box-sizing: border-box;
}

/*Color*/
.blue {
    --clr: #4285f4;
}
.red {
    --clr: #db4437;
}
.yellow {
    --clr: #f4b400;
}
.green {
    --clr: #0f9d58;
}

body {
    background-color: #636e72;
    font-family: "Roboto", sans-serif;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    margin: 0;
    overflow-x: hidden;
}

h1 {
    color: rgb(226, 226, 226);
    text-transform: uppercase;
    margin: 30px;
    text-shadow: 2px 2px 10px #000;
}

.box {
    background-color: var(--clr);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 400px;
    height: 200px;
    margin: 10px;
    border-radius: 20px;
    box-shadow: 2px 4px 5px rgba(0, 0, 0, 0.3);
    transform: translateX(400%);
    transition: transform 0.4s ease;
}

.box:nth-of-type(even) {
    transform: translateX(-400%);
}

.box.show {
    transform: translateX(0);
}

.box h2 {
    font-size: 45px;
    text-shadow: 2px 2px 3px #000;
}
Enter fullscreen mode Exit fullscreen mode

JavaScript

const boxes = document.querySelectorAll('.box');

window.addEventListener('scroll', checkboxes);

checkboxes();

function checkboxes() {
    const triggerBottom = window.innerHeight / 5 * 4;

    boxes.forEach((box) => {
        const boxTop = box.getBoundingClientRect().top;

        if (boxTop < triggerBottom) {
            box.classList.add('show');
        } else {
            box.classList.remove('show');
        }
    });
}
Enter fullscreen mode Exit fullscreen mode

🛴 Follow me for more on:
📺YouTube: https://bit.ly/3oBQbc0
Facebook: https://bit.ly/3cp2S5W
Instagram: https://bit.ly/3Ihh2EB

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay