Animating Addclass/Removeclass With Jquery

animating addClass/removeClass with jQuery

Since you are not worried about IE, why not just use css transitions to provide the animation and jQuery to change the classes. Live example: http://jsfiddle.net/tw16/JfK6N/

#someDiv{
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;
}

CSS3 transition with jQuery .addClass and .removeClass

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;
}

adding animation on addClass + removeClass

You can't animate the display: none property. Only css rules with numeric values can be animated.

In this case you could use a fade in/out or a slide in/out animation like

jQuery(function($) {  $('.div1').on('click', function() {    $(this).fadeOut(1000, function() {      $(this).addClass('hide');    });    var $div2 = $(this).parent().find('.div2').removeClass('hide').css('display', 'none').fadeIn(1000);  })});
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.js"></script><link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/redmond/jquery-ui.css"><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.js"></script><link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.css"><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.js"></script>
<div class="parentWrapper"> <div class="div1">HELLO</div> <div class="div2 hide">GOODBYE</div></div>

Animate / apply Transition addClass and removeClass in jQuery?

From comments of different users I came to know that css transition can not be applied on display: none elements. So instead of display none I applied height 0px; opacity: 0; which worked fine for me.

.slider img{
height: 0px;
opacity: 0;
display: block;
-webkit-transition: opacity 2s ease;
-moz-transition: opacity 2s ease;
-ms-transition: opacity 2s ease;
-o-transition: opacity 2s ease;
transition: opacity 2s ease;
}

.slider img.active{
height: 360px;
opacity: 1;
-webkit-transition: opacity 2s ease;
-moz-transition: opacity 2s ease;
-ms-transition: opacity 2s ease;
-o-transition: opacity 2s ease;
transition: opacity 2s ease;
}

Thanks to the creator of this fiddle which gave me this idea.

jQuery CSS animations with addClass and removeClass

You could use the following to remove the class when the animation completes.

Updated Example

$(function () {
$('button').click(function () {
el = $('.element');
el.addClass('shake');
el.one('webkitAnimationEnd oanimationend msAnimationEnd animationend',
function (e) {
el.removeClass('shake');
});
});
});


Related Topics



Leave a reply



Submit