Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .fadeIn() on page load?

I'm trying to set up some code so that I have a that is hidden at first but then fades in after the page is loaded.

I have the following HTML code:

<div class="hidden">
<p>This is some text.</p>
</div>

Then I also have this CSS code, which hides the <div>.

div.hidden
{
    display: none
}

Finally, I have my jQuery:

$(document).ready(function() {
    $('div').fadeOut(1);
    $('div').removeClass('hidden');
    $('div').fadeIn(1000);
});

What I was hoping would happen was that the first .fadeOut() would fade out the , the removeClass would stop the CSS from hiding it, and the final .fadeIn() would fade it back onto the page. Unfortunately, this did not work.

You can view the code here:Fiddle

So can someone please tell me how to keep a <div> hidden until a page loads, and then fade it in using jQuery?

Thanks!

like image 876
user2962388 Avatar asked Jan 26 '14 06:01

user2962388


2 Answers

The problem is fadeIn works on hidden elements, when you remove the hidden class before the fadeIn() is called the element is fully displayed so there is nothing to to fadeIn()

It should be

$(document).ready(function () {
    $('div.hidden').fadeIn(1000).removeClass('hidden');
});

Demo: Fiddle

like image 75
Arun P Johny Avatar answered Nov 05 '22 05:11

Arun P Johny


HTML Code:

<div class="toshow" style="display:none;">
    This is some text.
</div>

jquery Code:

$(document).ready(function () {
    $('div.toshow').fadeIn(2200);
    // OR $('div.toshow').show(2200);
    // OR $('div.toshow').slideDown("slow");
});

Change the jquery show()/hide() animation?

like image 5
eladolo Avatar answered Nov 05 '22 07:11

eladolo