Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .animate() sets display:none, how to avoid?

I have the following code:

HTML:

<div id="clickme">
    Click me  :-)
</div>

<div id="info" style="background:red; width:100%; height:100px; margin-bottom:-100px; z-index:20; position:absolute; bottom:0px;">
  Stay, damn!
</div>

JavaScript:

$('#clickme').click(function() {
    $('#info').animate({
        marginBottom: 'toggle'
    },{
        duration:500
    });
});

It's also available at http://jsfiddle.net/DxnQJ/

Obviously, I want the #info DIV to appear/disappear whenever the #clickme DIV is clicked. It's working as intended, except that the #info DIV disappears after the animation due to jQuery setting its CSS display property to none.

How can I tell jQuery to stop hiding my DIV?

like image 812
Philip Avatar asked Dec 23 '11 00:12

Philip


People also ask

How do you stop the currently running animation in jQuery?

The jQuery stop() method is used to stop an animation or effect before it is finished. The stop() method works for all jQuery effect functions, including sliding, fading and custom animations. Syntax: $(selector).

How can use a animate () method in jQuery?

The animate() method performs a custom animation of a set of CSS properties. This method changes an element from one state to another with CSS styles. The CSS property value is changed gradually, to create an animated effect. Only numeric values can be animated (like "margin:30px").

What does jQuery .show do?

The show() method shows the hidden, selected elements. Note: show() works on elements hidden with jQuery methods and display:none in CSS (but not visibility:hidden).

How do you add transitions to display none?

If you even toggle the display property from none to block , your transition on other elements will not occur. To work around this, always allow the element to be display: block , but hide the element by adjusting any of these means: Set the height to 0 . Set the opacity to 0 .


1 Answers

I would recommend using .slideToggle:

$('#clickme').click(function() {
    $('#info').slideToggle(500);
});

Demo: http://jsfiddle.net/Daniel_Hug/DxnQJ/2

like image 152
Web_Designer Avatar answered Oct 27 '22 18:10

Web_Designer