Javascript onclick fade div in (short code)

I look around the place and I can’t find exactly what they are after all.

The html structure is basically.

<ul> 
   <li><a href="">link</a></li>
   <div id="us">hidden info</div>
</ul>

css structure.

#us {display:none}

I would like to see the “us” div change from dipslay: none; to display: a block, in a graceful fade, as few lines of code as possible, and then again, when the link is clicked, to switch back to the: hidden screen.

I know that there are many things that can do this, but they are really looking for this simplicity in the code.

thanks for your time x

+3
source share
5 answers

.toggle() ( jQuery), :

$("ul li a").click(function() { $("#us").toggle("fast"); });

, jQuery 1.4.4+, .fadeToggle() :

$("ul li a").click(function() { $("#us").fadeToggle(); });

, <div> <ul>, ... .

+5

- :

$("#idfortheanchor").bind("click", function() {
    if ($("#us").is(":visible")) {
        $("#us").hide();
    } else {
        $("#us").show();
    }
});
+1

jQuery fade-toggle, :

$('a').toggle(
    function() { $('#us').fadeIn(); },
    function() { $('#us').fadeOut(); }
);
0

jQuery UI , . jQuery UI jQuery :

show( effect, [options], [speed], [callback] )

But check the site itself: http://jqueryui.com/demos/show/

0
source

Thank you all for your input! You really helped! The last piece that I used, which I find most appropriate, was from Nick.

$("ul li a").click(function() { $(this).parent().next().toggle("fast"); });

so thanks everyone! and hope someone else finds this useful! x

0
source

Source: https://habr.com/ru/post/1777740/


All Articles