How to animate JQuery addClass/removeClass functions?

41,365

Solution 1

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

Solution 2

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

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

Solution 3

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.

Share:
41,365
CairoCoder
Author by

CairoCoder

I majored in Bioinformatics but have a passion for User Experience and Web Development. Everything I know about UI, UE, CSS, HTML, jQuery, PHP and MySQL was all self taught by looking online and buying books and video tutorials related to UX and Web Development. I have been continuously employed since high school and throughout college, which helped ramp me to my current positions at RAViN Jeanswear. I'm also in love with my current position at RAViN Jeanswear as Senior Web Developer and Team Leader. I am discovering that I seriously enjoy user flow, User Experience and Web Development, and would like to transition my career towards this wire-framing and development path. I love figuring out what makes users tick and click, also I adore developing easy to use and smooth back-ends. I also am getting serious about my Web Design, so feel free to message me if you would like a web design, psd to html/css or anything else! I would currently like to learn (or get to know better): User Experience Research, SASS, HTML 5, jQuery/jQuery-UI, JavaScript, AJAX, IOS Development. ------- Interesting Points of Discussion ------- 1. What's more important? Creating a product to create fantastic metrics? Or creating a product that creates a "good feeling" and a lasting impression? 2. When should you use tables, and when should you use CSS? Always CSS for presentation, of course! But sometimes divititis will get you down. How do you get around that? 3. Find me a way to wrap a really long word (like supercalifragilisticexpialidocious) onto the next line of a 20px width div in CSS that works in all browsers. (?) 4. When to use CMS, and when to start a "From Scratch" website? Specialties: HTML Markup, CSS, User Experience, User Interface, Photoshop, Photography, Agile, JIRA, PHP, OOP, JavaScript, jQuery, PHP Frameworks.

Updated on July 16, 2022

Comments

  • CairoCoder
    CairoCoder almost 2 years

    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>