Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to animate show a hidden div in jquery?

Tags:

jquery

Dumb question but I can't seem to figure this out.

I have a div and hide it when the page loads like so

$("e").hide();

then when a user persons a specific action I want the div to animate or slide down gracefully. But on my site the animation just flashes and shoes the hidden div and no fade or slideDown effects occur.

I use

$("#e").hide();
$("#p").change(function() {
    if ($("#p").val() === 'Married') {
        $("#e").slideDown(500);
    } else {
        $("#e").slideUp(500);
    }
});
like image 504
Eli Avatar asked Jan 31 '11 08:01

Eli


3 Answers

You can use animate to do the same thing animate like this.

$("#e").hide();
$("#p").change(function(){
    if($("#p").val() === 'Married'){
        $("#e").animate( { "opacity": "show", top:"100"} , 500 );
    }else{
        $("#e").animate( { "opacity": "show", top:"150"} , 5000 );
    }
});

to slide up and down you can play with height and width of div.

like image 195
Vivek Avatar answered Nov 08 '22 07:11

Vivek


Instead of:

 {
    $("#e").slideDown(500);
 } else {
    $("#e").slideUp(500);
 }

Write this:

$("#e").toggle(500);

This will show or hide your DIV. It's 1 line solution.

like image 39
Enriqe Avatar answered Nov 08 '22 06:11

Enriqe


Use the Toggle function in order to do this.

$("#p").toggle(function(){
    // Your toggle code here
});
like image 35
Neil Knight Avatar answered Nov 08 '22 07:11

Neil Knight