What is the best way to create a mobile menu transition that moves in a "reverse" direction?

I have been working on animating a mobile menu where the three stripes transform into an 'X' when clicked and revert back to three stripes when closed. The transition to an 'X' is working perfectly, but when closing the menu, it jumps back to three stripes abruptly instead of smoothly transitioning back. This is because I completely remove the x class, but I am struggling to find a way to transition back to the original state smoothly.

Below is the code I have written so far:

HTML:

<button id="nav-toggle">
  <span class="toggle-pin"></span>
  <span class="toggle-pin"></span>
  <span class="toggle-pin"></span>
</button>

CSS (sass):

#nav-toggle {
  position: relative;
  z-index: 9000;
  float: right;
  margin: 15px 15px 0 0;
  background-color: transparent;
  border: none;
  outline: 0;
  -webkit-transition: all 0.5s ease;
  -moz-transition: all 0.5s ease;
  -ms-transition: all 0.5s ease;
  -o-transition: all 0.5s ease;
  transition: all 0.5s ease;

  @include respondFrom(widescreen) {
    display: none;
  }

  .toggle-pin {
    width: 34px;
    height: 4px;
    background: palette();
    display: block;
    margin: 5px 0;
    border-radius: 5px;
  }

  &.x {

    .toggle-pin {
      margin: 11px 0;
    }

    .toggle-pin:nth-child(1) {
      -webkit-transform: rotate(45deg);
      -moz-transform: rotate(45deg);
      transform: rotate(45deg);
      -webkit-transition: -webkit-transform .4s;
      transition: transform .4s;
    }

    .toggle-pin:nth-child(2) {
      opacity: 0;
      -webkit-transition: opacity .3s;
      transition: opacity .3s;
    }

    .toggle-pin:nth-child(3) {
      margin-top: -30px;
      -webkit-transform: rotate(-45deg);
      -moz-transform: rotate(-45deg);
      transform: rotate(-45deg);
      -webkit-transition: -webkit-transform .4s;
      transition: transform .4s;
    }
  }
}

The toggle script:

$navToggle.on('click', function() {

  if ($navToggle.hasClass('x')) {
    $navToggle.removeClass('x');
  }
  else {
    $navToggle.addClass('x');
  }
});

Answer №1

Simply establish default transitions:

CSS

#menu-toggle {
  position: relative;
  z-index: 9000;
  float: left;
  margin: 10px 20px 0 0;
  background-color: transparent;
  border: none;
  outline: 0;
  -webkit-transition: all 0.3s ease;
  -moz-transition: all 0.3s ease;
  -ms-transition: all 0.3s ease;
  -o-transition: all 0.3s ease;
  transition: all 0.3s ease;
  cursor: pointer;

  @include respondFrom(large-screen) {
    display: none;
  }

  .toggle-icon {
    width: 30px;
    height: 2px;
    background: blue;
    display: inline-block;
    margin: 3px 0;
    border-radius: 3px;

    &:nth-child(1),
    &:nth-child(3) {
      -webkit-transition: -webkit-transform .3s;
      transition: transform .3s;
    }
    &:nth-child(2) {
      -webkit-transition: opacity .2s;
      transition: opacity .2s;
    }
  }

  &.open {
    .toggle-icon {
      margin: 8px 0;

      &:nth-child(1) {
        -webkit-transform: rotate(45deg);
        -moz-transform: rotate(45deg);
        transform: rotate(45deg);
      }

      &:nth-child(2) {
        opacity: 0;
      }

      &:nth-child(3) {
        margin-top: -20px;
        -webkit-transform: rotate(-45deg);
        -moz-transform: rotate(-45deg);
        transform: rotate(-45deg);
      }
    }
  }
}

http://jsfiddle.net/exampleuser/5tdjg2h1/

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

Detecting collisions on a pixel-by-pixel basis within Javascript/Jquery/Gamequery

Currently, I am working on developing a web game using Jquery with the GameQuery plugin. However, I have encountered an issue where the GameQuery plugin does not support per pixel collision detection, only bounding boxes collision detection. Is there a way ...

Having trouble with jQuery scrollTop not working after loading content with ajax?

When the html content is loaded via ajax, I need to scroll to a specific element. This element has the attribute data-event-id="". However, there are instances when the variable $('.timelineToolPanel[data-event-id="'+id+'"]').offset().t ...

Fixing a CSS animation glitch when using JavaScript

I'm facing an unusual issue with my CSS/HTML Check out my code below: a:hover { color: deeppink; transition: all 0.2s ease-out } .logo { height: 300px; margin-top: -100px; transition: all 0.2s ease-in; transform: scale(1) } .logo:hover { transit ...

Determine the size of a file with the help of jQuery

Similar Question: Check file input size with jQuery I am trying to determine if the size of an uploaded image is less than 5mb. This is the code I'm currently using: $("#fileUpload").change(function () { $file = $("#fileUpload"); ...

Only the first Yii ajax jQuery request is successful when multiple requests are made

There seems to be an issue with my input field and button pairs for ajax requests. The functionality works fine the first time, but after that initial request-response cycle, the other buttons don't trigger the ajax function. I have a data grid list ...

The location.reload function keeps reloading repeatedly. It should only reload once when clicked

Is there a way to reload a specific div container without using ajax when the client requests it? I attempted to refresh the page with the following code: $('li.status-item a').click(function() { window.location.href=window.location.href; ...

Rails successfully processed the request with a 200 OK status code, however, the $.ajax() function is triggering the 'error' callback instead of the 'success' callback

In my controller, the ajax request is handled as shown below (simplified): def create ... @answer = Answer.new(video: video_file) if @answer.save render json: @answer.video.url else ... end end This is how I have defined my ajax function: $.aja ...

Display a list next to a block of text and image using HTML and CSS styling

I am having trouble with keeping a navigation list aligned inline with a block of text that includes an image. My issue arises when I expand the browser to full screen size, causing the list to overlap the text block. Here is the HTML code snippet: <bo ...

Adjust the positioning of the background and create a smooth fade effect for

Is there a way to simultaneously change the background position and display an image in front of it? My current solution involves using a second div with padding to center it, but when I hover over the padding of the first div, the image does not change. ...

The method of altering a menu link in WordPress using jQuery varies according to whether the user is logged in or not

I need to update the last link on my menu. When a user is logged in, it should display a profile link; otherwise, it should show a sign-up link. ...

The z-index property in CSS is not functioning as expected when used with an absolute div inside a

https://i.sstatic.net/MnNQY.png Why isn't z-index working for me? I have a process div (container) Inside the process div, there are links in a row. These links are further divided into progress_number divs (large number) and progress_text divs (shor ...

Create a DIV element that completely fills the vertical space available

My webpage is currently looking quite empty, with only one DIV element present: <div style="height: 20%; min-height: 10px; max-height: 100px; width: 100%; background-color: blue;"></div> I' ...

Issues with Videojs Responsive Lightbox Functionality

I am looking for a solution to display VideoJS in a lightbox while keeping it responsive. I came across this helpful code snippet: https://github.com/rudkovskyi/videojs_popup. It seemed perfect until I tried using it with the latest version of Videojs and ...

Applying flexbox to create a visually appealing design

Is there a way to achieve this layout using flexbox without relying on grid due to limited support in IE11? I want to be able to easily add or remove small divs without adding more containers. To see the desired layout, check out this image: Desired Layou ...

How can the horizontal scroll bar width be adjusted? Is it possible to create a custom

Within my div, I have implemented multiple cards that scroll horizontally using the CSS property: overflow-x: scroll; This setup results in a horizontal scrollbar appearing under the div, which serves its purpose. However, I would prefer a customized scr ...

Minimize the width to display a scrollbar

Check out this page: where you'll find the same code snippet displayed twice - once through GitHub Gist embedding and the second using Jekyll's internal syntax highlighter Rouge. I'm currently working on styling the second code snippet to m ...

The orientation of the rating stars has transformed into a vertical layout

I'm having trouble creating a rating interface that prompts the user to rate the website (by clicking the stars) and leave a comment. The stars are displaying vertically instead of horizontally, as shown in the image provided in the link. I've tr ...

Issue arises when the logo and navigation menu overlap upon zooming in

I'm facing a couple of issues with the menu on my website that I can't seem to resolve. The code appears to be correct, but the problem arises when a user zooms in on the website, particularly on netbooks. The website logo and the navigation menu ...

Having trouble with CSS box alignment issues in the top margin

I am working on customizing a CSS box for a navigation menu. Here is the current CSS code: .navigation-div { text-align:right; margin-top:14px; box-shadow:0px 0px 10px rgba(0,0,0,0.47); padding: 0; color:#E3E3E3; background-color: ...

Search for a JSON key/value pair in JavaScript/jQuery by partially matching a variable and returning the corresponding value

I am currently working on a project that involves searching through a list of models to find the corresponding URL for a specific one. Sometimes I may not have the full key or value, but I will always have at least a part that is unique. At the moment, my ...