Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS3 transition with jQuery .addClass and .removeClass

Tags:

html

jquery

css

I have a big main navigation panel that I want to animate when it's deploying (expanding).

I'm working with jQuery to trigger the visibility of it by adding/removing the class visible/hidden.

I had to set a delay time to apply the hidden class because the trigger is a button outside of the panel's div. (if I used a rollover on the button, once you rollout the panel will disappear)

The code is this

$(document).ready(function() {
    $('#menu-item-9').click(function(){
        $('#repair-drop').removeClass('hidden');
        $('#repair-drop').addClass('visible');
    });
$('#repair-drop').on('mouseleave', function(e) {
    setTimeout(function() {
        $('#repair-drop').removeClass('visible').addClass('hidden');
    }, 600);        

});
});

My CSS is as follows

.hidden{
    max-height: 0px;
    -webkit-transition: max-height 0.8s;
    -moz-transition: max-height 0.8s;
    transition: max-height 0.8s;

}
.visible{
    max-height: 500px;  
}

The transition effect is not working at all.

like image 979
Jaypee Avatar asked Jan 15 '14 21:01

Jaypee


People also ask

How to add and remove classes in JQuery elements?

.addClass() and .removeClass() methods in jQuery. In jQuery, there are various methods like – switchClass(), toggleClass(), addClass() and removeClass() by using them you can easily add or removes classes in the elements.

What is a transition in CSS 3?

CSS3 transitions are great; they help offload the tedious work from JavaScript when it comes to performing transition effects on the web page, such as moving an element from point A to B, fading it in/out, or basically any effect that involves the changing of one CSS property value to another.

How to remove a class from a text element using CSS?

You need to pass your class name within the method which you want to remove. Note – Pass your class names separated with space in the method for removing multiple classes. e.g. The following example removes addborder class from all text element. <!-- CSS --> <style> .addborder { border: 1px solid red; margin-bottom: 5px; } </style> <!--

What is the difference between addClass () and removeclass () methods?

Conclusion The addClass () method does not replace the existing class it simply adds or appends the new class within the element if it is not available. If you don’t specify any class-name in the removeClass () method then it will remove all classes from the selected element.


5 Answers

You are adding and removing the class that contains the transition CSS. I recommend moving that to its own rule DEMO.

.hidden{
    max-height: 0px;
}
.visible{
    max-height: 500px;  
}

#repair-drop{
    -webkit-transition: max-height 0.8s;
    -moz-transition: max-height 0.8s;
    transition: max-height 0.8s;
}
like image 27
Fresheyeball Avatar answered Oct 02 '22 02:10

Fresheyeball


I am tired of this issue, better use animation:

.container .element.animation  {
    animation: SHW .5s;
    animation-fill-mode: both
}
@keyframes SHW {
    from {
        transform:scale(0.7);
        opacity:0
    }
    to {
        transform: scale(1);
        opacity:1
    }
}

Add only to .element class .animation and its working:

$('.container .element').addClass('animation');
like image 198
user956584 Avatar answered Oct 02 '22 02:10

user956584


Don't remove the .hidden class; it contains your transition styles. Just add and remove the .visible class.

$(document).ready(function() {
    $('#menu-item-9').on('click', function(e) {
        $('#repair-drop').addClass('visible');
    });

    $('#repair-drop').on('mouseleave', function(e) {
        setTimeout(function() {
            $('#repair-drop').removeClass('visible');
        }, 600);        
    });
});

http://jsfiddle.net/mblase75/LjhNa/


That said, you might need to improve your solution to account for users rapidly mousing out of #repair-drop and clicking on #menu-item-9 before it can hide.

$(document).ready(function() {
    $('#menu-item-9').on('click', function(e) {
        $('#repair-drop').data('shown',true).addClass('visible');
    });

    $('#repair-drop').on('mouseleave', function(e) {
        $('#repair-drop').data('shown',false);
        setTimeout(function() {
            if (!$('#repair-drop').data('shown')) {
                $('#repair-drop').removeClass('visible');
            }
        }, 600);        
    });
});

http://jsfiddle.net/mblase75/b8QpB/

like image 30
Blazemonger Avatar answered Oct 02 '22 01:10

Blazemonger


Have you considered using jQuery UI's animation features? such as

jQuery('#menu-item-9').hide({duration:200,effect:'blind'});

You could also animate the removal of the class, like

jQuery('#menu-item-9').removeClass('hidden', {duration:200,effect:'blind'});
like image 21
jbwebtech Avatar answered Oct 02 '22 00:10

jbwebtech


I found a way to make this work using jquery easing plugin. Thanks to all for your responses

$(document).ready(function() {
    $('#menu-item-9').click(function(){
        $('#repair-drop').removeClass('hide');
        $('#repair-drop').animate({"max-height":"500px", "padding-top":"20px", "opacity":"1"},1500, "easeOutCubic");
    });
$('#repair-drop').on('mouseleave', function(e) {
    setTimeout(function() {
        $('#repair-drop').animate({"max-height":"0px", "overflow":"hidden", "padding":"0px","opacity":"0"},2000, "easeOutCubic");

    }, 600);        

});
});
like image 38
Jaypee Avatar answered Oct 02 '22 02:10

Jaypee