The CSS transition will only be triggered when the class is turned on, not when it is turned off

Having some trouble with a transition effect involving multiple elements on a page. Specifically, I am working on a sliding side menu for the responsive top navigation menu. The goal is to have the relative body and a fixed header bar move to the right when the navigation toggle link is clicked.

While the transition of position right from 0 to 200px works smoothly when opening the navigation menu, the issue arises when closing it. The blue header bar instantly returns to right: 0px, while the transition on the red main content functions correctly. You can observe the word 'nav' move instantly before the rest of the content transitions in red. This discrepancy is more noticeable on the actual site I'm building compared to the fiddle linked below.

Check out this jsFiddle for reference:

 $('.menu-toggle').click(function() {
   $('body, .header-bar').toggleClass('menu-open');
   return false;
 });
* {
  box-sizing: border-box;
}
body {
  margin: 0;
  padding: 0;
}
header {
  background-color: #c00;
  width: 100%;
  margin: 0;
  padding: 10px;
}
.menu-toggle {
  color: white;
  font-weight: bold;
  cursor: pointer;
  float: right;
  position: relative;
  right: 0;
}
.header-bar {
  position: fixed;
  top: 0;
  right: 0;
  width: 100%;
  height: 15%;
  background-color: #00c;
  margin: 0;
  padding: 15px;
}
.menu {
  overflow-x: hidden;
  position: relative;
  right: 0;
  font-family: "Segoe UI", Arial, Helvetica, sans-serif;
  padding-top: 5px;
}
.menu-open {
  right: 200px;
}
.menu-open .menu-side {
  right: 0;
}
.menu-side {
  background-color: #333;
  border-right: 1px solid #000;
  position: fixed;
  top: 0;
  right: -200px;
  width: 200px;
  height: 100%;
  padding: 10px;
}
.menu,
.menu-open,
.menu-side {
  -webkit-transition: right 0.2s ease;
}
.logo {
  width: 250px;
  height: 100px;
  color: white;
  font-weight: bold;
  font-size: 36px;
  float: left;
  position: relative;
}
.menu-side ul {
  list-style: none;
  padding-left: 0;
  margin: 0
}
.menu-side ul li a {
  color: #eee;
  text-decoration: none;
}
.menu-side ul li a:hover {
  text-decoration: underline;
}
<header>
  <div class="header-bar">
    <div class="logo">MWP</div>

    <div class="menu-toggle">Nav</div>

    <nav class="menu-side">
      <ul>
        <li><a href="#about-me">About Me</a>
        </li>
        <li><a href="#my-work">My Work</a>
        </li>
        <li><a href="#testimonials">Testimonials</a>
        </li>
        <li><a href="#contact-me">Contact Me</a>
        </li>
      </ul>
    </nav>
  </div>

  <!-- omitted content filler was here - found in jFiddle -->
</header>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>

I've double-checked my CSS multiple times and confirmed that the jQuery is adding/removing the necessary classes. However, I can't seem to identify what I've done wrong. I need the blue bar to remain fixed in position.

Answer №1

Insert the transition on .header-bar. The transition currently will not activate on the way out because it is assigned to the class being removed. It should appear as follows:

.menu,
.header-side,
.header-bar,
.menu-side{
  transition: right 0.2s ease;
}

If you wish to have a different transition on the way in, then assign the transition to .menu-open as well. For instance, this code snippet will offer a slower transition on entering and a quicker transition on exiting:

.header-bar {
  transition: right 0.2s ease;
}

.menu-open {
  transition: right 0.5s ease;
}

Complete Example

Take note of the unprefixed transition. The -webkit- prefix may be necessary for older browsers. Include the prefixed property before the unprefixed property.

$('.menu-toggle').click(function() {
  $('body, .header-bar').toggleClass('menu-open');
  return false;
});
* {
  box-sizing: border-box;
}
body {
  margin: 0;
  padding: 0;
}
header {
  background-color: #c00;
  width: 100%;
  margin: 0;
  padding: 10px;
}
.menu-toggle {
  color: white;
  font-weight: bold;
  cursor: pointer;
  float: right;
  position: relative;
  right: 0;
}
.header-bar {
  position: fixed;
  top: 0;
  right: 0;
  width: 100%;
  height: 15%;
  background-color: #00c;
  margin: 0;
  padding: 15px;
}
.menu {
  overflow-x: hidden;
  position: relative;
  right: 0;
  font-family: "Segoe UI", Arial, Helvetica, sans-serif;
  padding-top: 5px;
}
.menu-open {
  right: 200px;
}
.menu-open .menu-side {
  right: 0;
}
.menu-side {
  background-color: #333;
  border-right: 1px solid #000;
  position: fixed;
  top: 0;
  right: -200px;
  width: 200px;
  height: 100%;
  padding: 10px;
}
.menu,
.menu-open,
.header-side,
.header-bar,
.menu-side{
  transition: right 0.2s ease;
}
.logo {
  width: 250px;
  height: 100px;
  color: white;
  font-weight: bold;
  font-size: 36px;
  float: left;
  position: relative;
}
.menu-side ul {
  list-style: none;
  padding-left: 0;
  margin: 0
}
.menu-side ul li a {
  color: #eee;
  text-decoration: none;
}
.menu-side ul li a:hover {
  text-decoration: underline;
}
<header>
  <div class="header-bar">
    <div class="logo">MWP</div>

    <div class="menu-toggle">Nav</div>

    <nav class="menu-side">
      <ul>
        <li><a href="#about-me">About Me</a>
        </li>
        <li><a href="#my-work">My Work</a>
        </li>
        <li><a href="#testimonials">Testimonials</a>
        </li>
        <li><a href="#contact-me">Contact Me</a>
        </li>
      </ul>
    </nav>
  </div>

  <!-- omitted content filler was here - found in jFiddle -->
</header>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>

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

I'm utilizing bootstrap to design a slider, but the length of the second div inside it is longer than the slider div

https://i.sstatic.net/0iIPb.png To ensure that the second div appears above the slide div, I have applied the position:absolute property to the second div. Since the first div is a slide, I had to position the second div outside of the first div. Do you h ...

Discover the index of the row when the value in the dropdown list is updated

I'm faced with a challenge regarding an HTML Table that contains a dropdown list in every row. I would like the background of each row to change whenever the value in the dropdown list is modified. Below is the code snippet: <table id="table1"> ...

How can JQuery detect input in the fields?

I have the following HTML code and I am looking to trigger a function based on the input in any of the input fields, regardless of the field number. <input type="text" pattern="[0-9]*" name="code" maxlength="1" au ...

Error is being returned by the JSONP callback

Looking to grasp JSONP. Based on my online research, I've gathered that it involves invoking a function with a callback. Other than that, is the way data is handled and the data format similar to JSON? I'm experimenting with JSONP as shown below ...

Delete the current row in the table before adding a new one with jquery

Having a HTML table and performing updates using Ajax. Everything is functioning properly so far. When clicking on the Edit button, the values are displayed in the textboxes correctly. However, when clicking on the Add button, the table is updated accordin ...

Tips for emphasizing v-list-item on click (Vue)

I am currently working on a side menu that changes based on the selected router model. My goal is to have the selected page highlighted when clicked. I would like this functionality for all the submenus as demonstrated in this example. Below are snippets ...

The rotation of an image in Microsoft Edge results in an HTML file display

One of the images on my website is sourced like this: <p> <img src="images/image.jpg" style="width: 50%;" /> </p> Surprisingly, in Chrome and Firefox, the image looks fine (just how I uploaded it to the server). H ...

The jQuery .click() function is not triggering when clicked

$("#backButton-1").click(function() { $("#form-2").empty(); $("#form-1").show(); }); I am experiencing trouble with this code snippet. The form-1 element is hidden, and the backButton-1 is created after the end of for ...

Determine the remaining load time in PHP after submitting a task

Is there a way to incorporate a progress bar that displays the percentage while "preparing the next page" before redirection? I have successfully implemented the progress bar animation, but now I am looking for an approximation of the time it will take be ...

Boxes that expand to full width and appear to float on

I am dealing with a situation where I have a container that holds multiple child elements such as boxes or sections. <section> <div>Box 1</div> <div>Box 2</div> <div>Box 3</div> <div>Box 4< ...

Utilizing the forEach method for decision-making

Here is an array called 'array' with values [10, 15, 20] var array = [10, 15, 20]; There is also a variable named N with a value of 20: var N = 20; I need to iterate through the array and check if N is greater than all numbers in the array in ...

How can I make my image shine when the mouse hovers over

I have recently come across a fascinating effect where an image glows up when the mouse hovers over it. This is not your typical border glow, but rather a unique enhancement of colors within the image itself. I discovered a library called Pixastic that can ...

Executing the SQL query more than once is not permitted

I seem to be facing a challenge in executing SQL queries multiple times. In the given code snippet, the first SQL query 【SELECT h.title, h.place, h.introduction ~】works fine. However, the second one 【SELECT name ~】fails to execute. If I comment ...

The slider thumb is not showing up properly on Microsoft Edge

Hey there! I'm experiencing an issue with the range slider's thumb display in Microsoft Edge. It appears to be positioned inside the track rather than outside as intended. Take a look at this snapshot for clarification: https://i.stack.imgur.co ...

Implementing CSS Media Print, tables elegantly transition to a fresh page

I am encountering a challenge when it comes to printing my dynamic table along with some preceding text. It seems that sometimes the table begins on a new page, resulting in the header test being isolated on the first page and only displaying the table on ...

JQuery Polaroid Gallery Viewer

Hey there, I could use a bit of assistance. While I have a natural knack for design, coding isn't exactly my strong suit. I've been exploring this jquery plugin to incorporate some dynamic elements into my website. You can find the source link h ...

Problems arise when JQuery fails to function properly alongside ajax page loading

While utilizing ajax to load the page, I encountered an issue where the jQuery on the loaded page template was not functioning until the page was manually refreshed. The ready function being used is: jQuery(document).ready(function() { jQuery(' ...

Tips for changing a fullscreen HTML5 video appearance

I discovered a way to change the appearance of a regular video element using this CSS code: transform: rotate(9deg) !important; But, when I try to make the video fullscreen, the user-agent CSS rules seem to automatically take control: https://i.sstatic. ...

What steps should I follow to generate a table using Ajax and Javascript when a button is clicked?

After spending hours following various tutorials and exploring previously asked questions, I am struggling to make this work. My HTML page looks like this: <!DOCTYPE html> <html> <head> <link type="text/css" rel="styleshee ...

Show validation error messages for radio buttons alongside images in a single row

Within a div, I have placed three radio buttons with images inline. My goal is to show a validation message using bootstrap when the submit button is clicked and there is no user input. I attempted to add another div after the radio buttons with the class ...