27. Effects and animations with jQuery

Página 77

jQuery is an extremely popular JavaScript library that simplifies HTML programming. It offers a variety of powerful features, including the ability to create dynamic effects and animations. In this chapter, we'll explore how you can use jQuery to add some life and movement to your website or web application.

To begin with, it's important to understand that jQuery offers two main types of animations: those that you can apply directly to HTML elements, such as fade and slide, and those that you can create using the animate() function.

Fade and slide effects are simple to understand and implement. For example, to make an element fade out, you can use the .fadeOut() method. Here is an example:

<script>
$(document).ready(function(){
  $("button").click(function(){
    $("p").fadeOut();
  });
});
</script>

In this example, when the button is clicked, all elements of the paragraph fade out. You can control the speed of the fade effect by passing an argument to the .fadeOut() method.

Similarly, you can use the .slideDown() method to make an element slide down. Here is an example:

<script>
$(document).ready(function(){
  $("button").click(function(){
    $("p").slideDown();
  });
});
</script>

In this example, when the button is clicked, all paragraph elements slide down. Again, you can control the speed of the slide effect by passing an argument to the .slideDown() method.

Now, let's talk about the animate() function. This function allows you to create custom animations by modifying one or more CSS styles of an element over time. Here is an example:

<script>
$(document).ready(function(){
  $("button").click(function(){
    $("div").animate({left: '250px'});
  });
});
</script>

In this example, when the button is clicked, the div element moves 250 pixels to the left. You can animate almost any CSS style using the animate() function.

One of the most powerful things about the animate() function is that you can chain multiple animations together. Here is an example:

<script>
$(document).ready(function(){
  $("button").click(function(){
    $("div").animate({left: '250px'}).animate({top: '250px'});
  });
});
</script>

In this example, when the button is clicked, the div element moves 250 pixels to the left and then 250 pixels up. Animations run sequentially, one after the other.

Finally, jQuery also offers a variety of callbacks and promises that you can use to control the flow of your animations. For example, you can use the .done() method to run code when an animation is complete.

In summary, jQuery offers a variety of powerful tools for creating dynamic effects and animations. With a little practice, you can use these tools to add some life and movement to your website or web app. So go ahead and give it a try - you'll be amazed at how much you can do with just a few lines of code!

Now answer the exercise about the content:

What are the two main types of animations that jQuery offers?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

7828. Ajax and HTTP requests with Javascript

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text