Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate JQuery addClass/removeClass functions?

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>
like image 528
CairoCoder Avatar asked Mar 26 '13 15:03

CairoCoder


People also ask

What is addClass and removeClass in jQuery?

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.

What is the correct syntax of animate method?

The jQuery animate() method is used to create custom animations. Syntax: $(selector). animate({params},speed,callback);

What is the use of removeClass?

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.


3 Answers

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;
}
like image 85
aNewStart847 Avatar answered Oct 09 '22 20:10

aNewStart847


If you're using jQueryUI you can use the $.toggleClass(); function.

http://api.jqueryui.com/toggleClass/

like image 38
Derek Avatar answered Oct 09 '22 21:10

Derek


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.

like image 22
johnkoht Avatar answered Oct 09 '22 19:10

johnkoht