Adding CSS3 animation to a button to enhance its appearance when its icon undergoes a transformation

I'm working on implementing a CSS transition for a button that changes when the font-awesome icon inside the button is replaced with another using jQuery.

Here is my HTML code for better understanding:

     <button type="button" class="navbar-toggle pull-left" data-toggle="collapse" data-target=".navbar-ex1-collapse">
        <span class="sr-only">Toggle navigation</span>
        <i class="fa fa-navicon fa-lg"></i>
     </button>  

The jQuery script that handles the icon replacement:

$(".navbar-toggle").on("click", function () {
        $(this).toggleClass("active");
        $('body').toggleClass("navbar-is-active");
        $('.navbar-toggle i').toggleClass('fa-navicon');
        $('.navbar-toggle i').toggleClass('fa-remove');
    });

Below are the CSS styles and transition effects applied to the button:

.navbar-toggle {
    width: 81px;
    height: 80px;
    background: #c4414f!important;
    border-radius: 0;
    border: 0 none;
    margin: 0;
    padding-left: 29px;
    z-index: 2000;
}

.navbar-default .navbar-toggle .icon-bar {
        background-color: #FFF;
        height: 3px;
        border-radius: 0;
    } 
    .navbar-toggle.active {
        -webkit-box-shadow:inset 1px 1px 1px 1px #000000;
        box-shadow: -1px 1px 1px 0px #333333;
    }

.navbar-toggle {
    color: #FFF;
    padding-left: 10px;
}
    .navbar-toggle i {
        font-size: 24px;
        -webkit-transition: all 0.5s;
        transition: all 0.5s;
    }

The issue I'm facing is that the transition does not work on the button when the menu icon changes to an 'X'. What could be causing this problem?

Here is a link to the Fiddle showcasing the code:

https://jsfiddle.net/471L8dfd/

For reference, here is an example of the type of transition effect I am trying to achieve with the navigation button:

https://i.sstatic.net/lDQDz.gif

Answer №1

To accomplish the desired effect, one can utilize the fadeOut and fadeIn functions:

 $(".navbar-toggle").on("click", function () {
     $(this).toggleClass("active");
     $('body').toggleClass("navbar-is-active");
     $('.navbar-toggle i').fadeOut(100, function(){
         $(this).toggleClass('fa-navicon');
     })
     $('.navbar-toggle i').toggleClass('fa-remove').fadeIn(150);
 });

https://jsfiddle.net/471L8dfd/3/

Note: It's important to toggle the class after the fadeOut in order to avoid icon popping. The first animation speed (100ms) is for the current icon's fadeOut, while the second pertains to the next icon's fadeIn. The total duration of the animation is the sum of these two values (100+150 = 250ms).

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Extra Charges for PayPal on Woocommerce Checkout Screen

Our team at Woocommerce is currently in the process of implementing an extra fee for orders purchased through the Paypal payment gateway. We successfully adjusted the price sent to Paypal with the following code: add_filter('woocommerce_paypal_args& ...

A more efficient approach to creating a personalized design

After realizing the original question was unclear and wouldn't solve my problem, I have decided to edit it and create a new one. For my project, I require a customized layout where users can move, resize, add, or remove each box according to their pr ...

I am experiencing difficulty getting Bootstrap Columns to appear next to each other despite trying numerous solutions

Check out this code snippet: <div class="mainptext"> <h3><strong>Master the following:</strong></h3> <div class="container"> <div class="row"> <div class ...

Error message "500 Internal Server Error" is displayed when making an AJAX post request in

After choosing an option from a dropdown list, I am trying to load data using an AJAX request. However, upon selection, I encounter a 500 (Internal Server Error). How can this issue be resolved? Check out my AJAX code below: $('#teacher').on(&a ...

In IE11, the letter type list with the style of lower-alpha no longer displays proper indentation

When viewing on IE11, the list in lower-alpha style (list-style-type: lower-alpha) is missing indentation. However, the indentation remains intact when viewed on Chrome, Edge, and Firefox browsers. The code snippet below showcases some of the CSS styles be ...

Form Validation Using a Separate Function

Currently, I am utilizing the jQuery Validation plugin from http://jqueryvalidation.org/ to validate my forms. By default, the validation process is triggered when the form is submitted. However, I would like to initiate the validation when a specific func ...

Detection of Unauthorized Video Downloads

Is it feasible to identify individuals who illegally download videos from websites, such as YouTube, by using video grabber applications? I wonder if these downloads could be traced in some way, enabling the identification of offenders. Is there a method t ...

How can you ensure a form is properly validated using javascript before utilizing ajax to submit it

I've been working on developing a website and I am in need of an attractive login/registration/forgot password form. My aim was to utilize 'ajax' to enhance the user experience, leading me to immerse myself in a steep learning curve for the ...

Alter appearance using various classes

I am seeking assistance in changing the font of text using classes. I have multiple texts with different classes and I want to be able to edit all the texts without adding another dropdown menu. I believe that the change needs to occur in a script. Pleas ...

Exploring the inner workings of CSS shapes

http://jsfiddle.net/zth05v3q/ div { background:red; width:100px; height:100px; } div:after{ content: ''; position: absolute; display: block; border: 40px solid green; border-left-width: 15px; bord ...

Issue with integrating Contact Form 7 HTML into Wordpress

I've revisited this question with a new "input" HTML tag Despite changing everything, CSS still isn't being applied. HTML Code : <form action="#" class="contact-one__form row"> <div class="col-lg-6"> ...

Errors occur when dealing with higher order functions, such as the "map is

Struggling with higher order functions in JavaScript - I keep running into the error 'map is undefined'. Anyone able to offer some assistance? function mapper(f) { return function(a) { return map(a, f); }; } var increment = function(x) { re ...

Unable to modify variable to play audio

As I work on constructing my website, I am incorporating various sounds to enhance user experience when interacting with buttons and other elements. However, managing multiple audio files has proven challenging, as overlapping sounds often result in no aud ...

Steps for creating a one-sided container in CSS

I've created a container class with the following CSS: .container { margin: 0 auto; width: min(90%, 70.5rem); } This setup centers the content on the page within the container, which is great for organization. However, I'm looking to creat ...

Center-align the table element

I've been struggling to center align this table element, specifically the one containing the square, but nothing seems to be working. Can anyone lend a hand in fixing this issue? <table> <tbody> <tr> <td>TURKEY - SU ...

Are you a fan of Ajax calls using JSON or HTML?

Which method is most recommended for implementing AJAX? If you were developing a search page using PHP and Jquery for the AJAX calls, how would you manage the response? a) Would you prefer to have the response include all relevant HTML and styling? or ...

CSS border margin-bottom attribute not functioning correctly

I am trying to create a page border by wrapping it around the content using a <div> element. The parent element is the actual page itself. However, I'm having trouble with the margin-bottom property as it doesn't seem to be working properly ...

Aligning hyperlink with full-height image within a table cell (navigation buttons)

I am currently working on creating a traditional navigation bar that occupies 20% of the screen width and consists of 3 buttons. Each button is supposed to have an icon that is centered both vertically and horizontally. Additionally, I want the buttons to ...

Unable to append dynamic data to elements using jQuery

I'm facing an issue with binding events to select boxes that are added dynamically to the page through Ajax or DOM after the initial loop. Below is the view part of my code: <div class="row" id="dsp"> <div class="col-md-2"> & ...

What are the steps to resolve issues with URL Encoding?

Here is my website link: forumdisplay.php?462-%EE%F9%E7%F7%E9%ED-%E1%F8%F9%FA I suspect that the url is URL Encoded. I am using vbulletin 4.1.12 How can I resolve this issue? Replace 462-%EE%F9%E7%F7%E9%ED-%E1%F8%F9%FA with f=462 ...