Tired of People Missing Out on Your CSS Animations? jQuery Is the Answer...
Tags: webdev, javascript, css, html
Published: 2021-12-02 19:59:49

You've probably been there... You've added a cool animation to one of your webpage's elements, but no-one sees it because the element is too far down the page, and by the time people scroll it into view, it's animation has already finished.
This can be easily solved using jQuery!

**************
**Let's take a simple webpage.**
```html
<!DOCTYPE html>
<html>
<head>
<style>
  @keyframes floatin {
    from { margin-left: 80% }
    to { margin-left: 10% }
  }
  h1{
    text-align: center;
  }
  p{
    margin-left: 20%;
    width: 60%;
  }
  .info{
    position: absolute;
    top: 1000px;
    font-size: 25px;
    margin-left: 80%;
  }
</style>
</head>
<body>
<h1>The top content</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis fringilla id justo ut tincidunt. Nullam in lectus ultrices, pretium velit sit amet, ullamcorper est. Suspendisse mattis ac diam sit amet convallis. Vivamus et sagittis justo. Praesent non commodo leo. Aenean pulvinar erat ac massa fermentum, vel aliquet mi consectetur. Proin et blandit neque, et porttitor tellus. Aliquam sollicitudin vehicula erat eu rutrum. Sed sit amet tortor quis tortor ullamcorper euismod. Donec eu vulputate sapien.
</p>
<div id="info">Hello there!</div>
</body>
</html>
```
{% codepen https://codepen.io/mattnikonorov/pen/YzxJwLz %}

As you can see, the animated ".info" element is too far down the webpage, and by the time you read the top content, it's animation would have long finished.

This can be solved using jQuery. 
First, we have to make sure to add the jQuery script tag inside this webpage's head tag:
```html
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
```

Next, let's add the following code inside of a script tag to the bottom of the body tag.
```javascript
function wait(){
  $h = $(document).height();
  $pos = $(".info").position();
  if($h > $pos.top){
    $(".info").animate({"margin-left": "10%"}, 2000);
  }
}
$(window).scroll(function(){
  wait();
});
```

**What does this jQuery code do?**
1. The "wait()" function first gets how much the user has scrolled down the page.
2. It checks if that number is larger than the top position of the ".info" element.
3. If it is, that element's "margin-left" property is changed from 80% to 10% in 2000 milliseconds(2 seconds) using jQuery's "animate()" function.
4. The "$(window).scroll(function())" then runs the "wait()" function every-time the use scrolls this webpage.

**Full code**
```html
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<style>
  @keyframes floatin {
    from { margin-left: 80% }
    to { margin-left: 10% }
  }
  h1{
    text-align: center;
  }
  p{
    margin-left: 20%;
    width: 60%;
  }
  .info{
    position: absolute;
    top: 1000px;
    font-size: 25px;
    margin-left: 80%;
  }
</style>
</head>
<body>
<h1>The top content</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis fringilla id justo ut tincidunt. Nullam in lectus ultrices, pretium velit sit amet, ullamcorper est. Suspendisse mattis ac diam sit amet convallis. Vivamus et sagittis justo. Praesent non commodo leo. Aenean pulvinar erat ac massa fermentum, vel aliquet mi consectetur. Proin et blandit neque, et porttitor tellus. Aliquam sollicitudin vehicula erat eu rutrum. Sed sit amet tortor quis tortor ullamcorper euismod. Donec eu vulputate sapien.
</p>
<div class="info">Hello there!</div>
<script>
function wait(){
  $h = $(document).height();
  $pos = $(".info").position();
  if($h > $pos.top){
    $(".info").animate({"margin-left": "10%"}, 2000);
  }
}
$(window).scroll(function(){
  wait();
});
</script>
</body>
</html>
```

{% codepen https://codepen.io/mattnikonorov/pen/vYeBBeZ %}

As you can see, the ".info" element is now being animated only when it is scrolled into view.

## Adding multiple animations
You can also add multiple animations to an element using jQuery. 
Let's take the same code, but this time add another animation to the ".info" element.
```javascript
function wait(){
  $h = $(document).height();
  $pos = $(".info").position();
  if($h > $pos.top){
    $(".info").animate({"margin-left": "10%"}, 2000).animate({"font-size": "40px"}, 2000);
  }
}
$(window).scroll(function(){
  wait();
});
```
By adding `animate()` functions onto each other like shown in this code, you can add animations to an element on top of each other.

{% codepen https://codepen.io/mattnikonorov/pen/zYEOWaz %}

*************
### There it is, a simple yet useful web development trick.

If you're intrigued by jQuery and it's possibilities, I would highly recommend this [jQuery course](https://bit.ly/3IcCvOx) on [educative](https://bit.ly/3EeDfAi).