I want to know how to animate JQuery addClass/removeClass functions?
for animate function, it seems that I have to put some CSS properties, but what about if I have a class which makes element displayed as block each time I trigger a click function, while all elements are displayed as hidden in CSS. How can I animate this process?
Here's my code:
<script src="js/jquery-1.9.1.min.js"></script>
<script>
var allSlides = $('li');
$('#nextSlide').click(function(){
var nextSlide = $('.active').next();
if (nextSlide.length == 0)
{
var nextSlide = allSlides.first();
}
$('.active').removeClass('active');
nextSlide.addClass('active');
return false;
});
$('#prevSlide').click(function(){
var prevSlide = $('.active').prev();
if (prevSlide.length == 0)
{
var prevSlide = allSlides.last();
}
$('.active').removeClass('active');
prevSlide.addClass('active');
return false;
});
</script>
The addClass() method adds one or more class names to the selected elements. This method does not remove existing class attributes, it only adds one or more class names to the class attribute. Tip: To add more than one class, separate the class names with spaces.
The jQuery animate() method is used to create custom animations. Syntax: $(selector). animate({params},speed,callback);
The removeClass() method removes one or more class names from the selected elements. Note: If no parameter is specified, this method will remove ALL class names from the selected elements.
You can apply the CSS3 transition property to the element being manipulated with jQuery. Here's an example with vendor prefixes:
element {
-webkit-transition: all 2s; // Chrome
-moz-transition: all 2s; // Mozilla
-o-transition: all 2s; // Opera
transition: all 2s;
}
If you're using jQueryUI you can use the $.toggleClass(); function.
http://api.jqueryui.com/toggleClass/
You can use jQuery .fadeIn() or you can use CSS3 transitions:
#nextSlide, #prevSlide {
display: none;
-webkit-transition: display .5s ease;
-moz-transition: display .5s ease;
-o-transition: display .5s ease;
}
.active {
transition: display .5s ease;
-webkit-transition: display .5s ease;
-moz-transition: display .5s ease;
-o-transition: display .5s ease;
}
That should work for you. You can add any other transitions to by replacing display
in the transition style.
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