• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / jQuery fadeIn – Fading In Element in HTML

jQuery fadeIn – Fading In Element in HTML

February 27, 2022 Leave a Comment

We can use jQuery to fade in an element using the jQuery fadeIn() method. The jQuery fadeIn() method will change the opacity of an element to 1 at a speed that we can set.

$("#div1").fadeIn(1000);

In the code above, the number “1000” is the number of milliseconds you want the fade in to happen by, which in this case is 1000 milliseconds or 1 second.

If you leave the fadeIn() method empty it will have the default time of 400 milliseconds.

$("#div1").fadeIn();

Let’s say we have the following HTML:

<style>#div1 { background: green; display: none; }</style>
<div id="div1">
  <p>This is some div</p>
</div>

Right now #div1 is not being displayed. If we wanted to change the opacity of #div1 to 1 and show it, we can use the jQuery fadeIn() method in the following JavaScript code.

$("#div1").fadeIn();

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery("#div1").fadeIn();

Note that we could also do this by using the jQuery fadeTo() method.

$("#div1").fadeTo(1000,1);

Using the jQuery fadeIn Method to Show a Div with a Click

In this simple example, we will have a div with a greenish background. We will provide a button for the user to use to change the div’s opacity and show it. We will also provide a button to hide the div so that the user can fade in and fade out as many times as they want.

<style>#div2 { width: 300px; height: 200px; background: #7bbfa2; display: none; }</style>
<div id="div1">
  <div id="div2"></div>
  <div id="click-me1" class="click-me">Fade In</div>
  <div id="click-me2" class="click-me">Fade Out</div>
</div>

We can utilize both the jQuery click() method and jQuery fadeIn() method to show the div.

We will also use the jQuery fadeOut() method to let the user hide the div.

We will give both fadeIn and fadeOut methods the parameter 2000, which means it will display or hide the div in 2000 milliseconds(2 seconds).

Here is the JavaScript code below:

$("#click-me1").click(function(){
  $("#div2").fadeIn(2000);
});

$("#click-me2").click(function(){
  $("#div2").fadeOut(2000);
});

The final code and output for this example of using the jQuery fadeIn method to show a div is below:

Code Output:

Fade In
Fade Out

Full Code:

<style>#div2 { width: 300px; height: 200px; background: #7bbfa2; display: none; }</style>
<div id="div1">
  <div id="div2"></div>
  <div id="click-me1" class="click-me">Fade In</div>
  <div id="click-me2" class="click-me">Fade Out</div>
</div>

<script>

$("#click-me1").click(function(){
  $("#div2").fadeIn(2000);
});

$("#click-me2").click(function(){
  $("#div2").fadeOut(2000);
});

</script>

Hopefully this article has been useful for you to understand how to use the jQuery fadeIn method.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Change the Font Size of a Paragraph
  • 2.  jQuery focus – Changing Form Styles with the focus() Method
  • 3.  How to Check if Element Exists Using jQuery
  • 4.  Using jQuery to Change Inner HTML
  • 5.  How to Uncheck All Checkboxes in jQuery
  • 6.  jQuery focusin – Changing the Background Color with the focusin() Method
  • 7.  Using jQuery to Replace a Class
  • 8.  jQuery siblings – Get the Sibling Elements of a Div
  • 9.  Using jQuery to Set the Id of a Div
  • 10.  jQuery mouseleave – An Interactive Example of the mouseleave Method

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy