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.
.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.
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.
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> <!--
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.
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;
}
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');
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/
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'});
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);
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With