Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 6312

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 27, 20242024-11-27T05:22:09+00:00 2024-11-27T05:22:09+00:00

Tired of People Missing Out on Your CSS Animations? jQuery Is the Answer…

  • 60k

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.

<!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> 
Enter fullscreen mode Exit fullscreen mode

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:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> 
Enter fullscreen mode Exit fullscreen mode

Next, let's add the following code inside of a script tag to the bottom of the body tag.

function wait(){   $h = $(document).height();   $pos = $(".info").position();   if($h > $pos.top){     $(".info").animate({"margin-left": "10%"}, 2000);   } } $(window).scroll(function(){   wait(); }); 
Enter fullscreen mode Exit fullscreen mode

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

<!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> 
Enter fullscreen mode Exit fullscreen mode

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.

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(); }); 
Enter fullscreen mode Exit fullscreen mode

By adding animate() functions onto each other like shown in this code, you can add animations to an element on top of each other.


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 on educative.

csshtmljavascriptwebdev
  • 0 0 Answers
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.