Generate an animated sequence transitioning smoothly from the left side to the center

I am in need of creating an animation that will cause a menu to appear from the left side of the screen when a button is clicked. The menu should expand to cover 55% of the width of the main page.

You can view a demo of this animation on JSFiddle by following this link: JSFiddle Demo

In the provided JSFiddle demo, you will see a hidden menu and a button that move to the left. Initially, the menu with "link" elements should be concealed, and the button with the class ".glyphicon" should be positioned at the far left of the page.
Upon clicking the button, both the menu and the button should slide to the right and take up 55% of the main page's width.
However, I am facing difficulty implementing this animation. While I was able to shift the menu by altering its structure, I couldn't figure out how to move the button. Below is a snippet of my HTML code:

<div id="left-menu">
    <div id="map-menu" class="test">
      <nav class="menu_content">
        <ul>
          <li>Link</li>
          <li>Link</li>
          <li>Link</li>
          <li>Link</li>
        </ul>
      </nav>
    </div>
    <div id="icon-menu" class="test">
      <button id="button_menu" class="js-menu menu" type="button">
        <span class="glyphicon glyphicon-map-marker"></span>
      </button>
    </div>
  </div>

Additionally, here is some CSS code:

#left-menu {
  position: fixed;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
}
#map-menu, #icon-menu {
  display: inline-block;
  vertical-align: middle;
}

/* More CSS styling... */

Javascript is also utilized for this animation:

var isActive = false;

$('.js-menu').on('click', function() {
    if (isActive) {
        $(this).removeClass('active');
        $('#left_menu').removeClass('menu-open');
    } else {
        $(this).addClass('active');
        $('#left_menu').addClass('menu-open');
    }

    isActive = !isActive;
});

If anyone could offer assistance in adapting or reworking this animation, I would greatly appreciate it.

Answer №1

Discover a creative CSS approach without the need for javascript by leveraging the hidden checkbox's :checked state with display:none. The label associated with this checkbox must be positioned outside the #left-menu element to enable targeting using the ~ selector:

Explore JS Fiddle 1

#button_menu {
  display: none;
}
.glyphicon {
  width: 40px;
  height: 40px;
  display: inline-block;
  background-image: url('https://cdn1.iconfinder.com/data/icons/basic-ui-elements-round/700/06_menu_stack-2-128.png');
  background-size: 100%;
  position: fixed; 
  left: 5px;
  top: 50%; 
  transition: all 1s;
  cursor: pointer;
} 
#left-menu {
  background-color: orange;
  position: fixed; 
  left: -100%; 
  width: 55%;
  top: 50%; 
  transition: all 1s;
}
#button_menu:checked + .glyphicon {
  left: 55%; 
  transition: all 1s;
}
#button_menu:checked ~ #left-menu {
  left: 0; 
  transition: all 1s; 
}
.menu_content ul {
  margin: 0; 
  list-style: none; 
  padding: 0; 
}
.menu_content ul li {
  padding: 20px 5px; 
  font-size: 2rem;
}
.menu_content ul li:hover { 
  background: blue; 
}
<input type="checkbox" id="button_menu" class="js-menu">
<label for="button_menu" class="glyphicon"></label>
<div id="left-menu"> 
  <div id="map-menu" class="test"> 
    <nav class="menu_content"> 
      <ul> 
        <li>Link</li> 
        <li>Link</li>
        <li>Link</li>
        <li>Link</li>
      </ul> 
    </nav> 
  </div> 
</div>


Take a look at an elegant jQuery solution, where a flag variable toggleFlag is utilized. When the value is true, it sets the left property to 0, and if it's false, the value switches to -55% - representing the menu width of 55% as specified. Here we animate the movement of the .left-menu based on the updated left value while toggling the flag value accordingly.

Check out JS Fiddle 2

var menuIcon = $('.glyphicon'), 
  leftMenu = $('#left-menu'), 
  toggleFlag = true;

menuIcon.on('click', function() { 
  var leftVal = (toggleFlag) ? '0' : '-55%'; 
  $('#left-menu').animate({'left': leftVal }, 700); 
  toggleFlag = !toggleFlag; 
});
.glyphicon {
  width: 40px; 
  height: 40px; 
  display: inline-block; 
  background-image: url('https://cdn1.iconfinder.com/data/icons/basic-ui-elements-round/700/06_menu_stack-2-128.png'); 
  background-size: 100%; 
  position: absolute; 
  right: -45px; 
  top: 5px; 
  cursor: pointer;
} 
#left-menu {
  background-color: orange; 
  position: fixed; 
  left: -55%; 
  width: 55%; 
  top: 50%; 
} 
.slideIt { 
  color: red; 
} 
.menu_content ul { 
  margin: 0; 
  list-style: none; 
  padding: 0; 
} 
.menu_content ul li { 
  padding: 20px 5px; 
  font-size: 2rem; 
} 
.menu_content ul li:hover { 
  background: blue; 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="left-menu"> 
  <span class="glyphicon"></span> 
  <div id="map-menu" class="test"> 
    <nav class="menu_content"> 
      <ul> 
        <li>Link</li> 
        <li>Link</li>
        <li>Link</li>
        <li>Link</li> 
      </ul> 
    </nav> 
  </div> 
</div>

Answer №2

Upon reviewing your example and fiddle, a few things stand out. Firstly, it's advisable to have jquery loaded in the fiddle for experimentation.

Secondly, the use of isActive is not defined and unnecessary:

if (isActive) {

This should be replaced with:

if ($(this).hasClass('active')) {

The line:

isActive = !isActive;

can be completely removed.

Furthermore, pay attention to the distinctions between underscores and hyphens, such as:

$('#left-menu') != $('#left_menu')

Additionally, ensure everything is encapsulated within a document ready function so that it executes upon page load:

$(document).ready(function () {
  // code
});

With these corrections, your code may resemble:

$(document).ready(function () {
  $('.js-menu').on('click', function() {
    if ($(this).hasClass('active')) {
      $(this).removeClass('active');
      $('#left-menu').removeClass('menu-open');
    } else {
      $(this).addClass('active');
      $('#left-menu').addClass('menu-open');
    }
  });
});

To address moving your button, the issue lies in applying the transform only to the <nav> element within the menu-open class:

.menu-open nav {
  -webkit-transform: translateX(0);
  transform: translateX(0);
}

A solution could involve adding the transformation to your button as well:

.menu-open #icon-menu {
  -webkit-transform: translateX(55%);
  transform: translateX(55%);
}

Alternatively:

.menu-open .active {
  -webkit-transform: translateX(55%);
  transform: translateX(55%);
}

Further enhancements can be made for slide animations and more. This feedback should assist you in making initial progress.

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

Exploring JSON in PHP with AJAX loops

Having trouble with my JSON data retrieval process, looping through it, and using AJAX for the results. The data seems to be alternating instead of being posted properly. Any suggestions? php: <?php $url = "../json/work.json"; $contents = file_get_con ...

Adjusting the size of icons to fit within a container

I need a div that can display up to 7 icons, depending on certain selections. These icons are from ionicons library. Here is the current code snippet: <div class="item item-text-wrap" style="text-align:center;"> <button class="button" style=" ...

Error message indicating that Element <> cannot be scrolled into view persisting despite attempting common troubleshooting methods

Currently, I am utilizing Selenium to create a web scraper for downloading multiple podcast episodes from Spreaker. # https://www.spreaker.com/show/alabamas-morning-news-with-jt for i in range(3): print("Click number: {}".format(str(i))) see_mor ...

Issue with ng-submit not functioning properly within AngularJS form

I am a beginner with AngularJS and I am encountering an issue when trying to submit a simple form using ng-submit. The button is not working properly - it's unclickable and the cursor does not change when hovering over it. This problem seems to occur ...

Interacting with a Hapi JS API through a distinct Vue JS Frontend. The data in request.payload is not defined

As I take my first steps on Hapi JS, I am facing the challenge of connecting my app to a SQL Server DB. My current task involves sending login data from a Vue CLI JS frontend to a Hapi JS Api using axios. The login process essentially consists of a "SELEC ...

Arrange Image and text to adapt to different screen sizes

As a beginner in CSS, I am overwhelmed by the numerous ways to position elements on a webpage. I am currently trying to achieve two different layouts: Scenario 1 : Plenty of space available [Image] this is the text that I want Scenario 2 : Limited spac ...

Adjusting the content within a text area using an AngularJS service

I am currently editing text in a textarea within the admin view and I would like to display it through an angular service on the user view. However, I want the text to be displayed in multiple rows, maintaining the same format that I entered in the textare ...

Run the *.js file only when the current month is December

Alright, I'm stumped. I've been trying to create this script: <script> $(document).ready(function(){ var d = new Date(); n = d.getMonth(); if (n == 11) { src="extrafiles/effect/snow.js"; } }); </script& ...

Creating a jQuery datatable with unique sorting and filtering functionality

I have implemented jquery datatables in my MVC4 application using a simple configuration. In fact, I included a small jquery snippet in my layout.cshtml file that automatically styles all tables in the application without any customization needed. $(".dat ...

Sequential Loop Complexity in jQuery Deferred

Despite searching through numerous answers on stackoverflow, I haven't found a solution that fits my complex problem. What I need to accomplish is: Run a for loop with a variable length that fetches table rows using an ajax query. Once all the rows ...

Search through array elements that are nested deeply

Consider the following scenario: an array is provided as input containing various objects with nested elements. The goal is to filter this array in JavaScript and obtain a new array consisting only of objects where the key "navigation" has a value of true. ...

Collect information from forms and save it to a mobile phone number

Is it possible to forward form field details to a cell phone number via text message? Here is the code I currently have for sending form data to an email address. If any adjustments need to be made, please provide instructions. <?php if(isset($_POST[ ...

Chrome displaying an extJs Button image

Could it be that Chrome is evolving into the new IE in terms of CSS issues? Here is the code I have for creating ExtJS buttons within an accordion: var button = Ext.create('Ext.Button', { text: '<img src="'+resp.sellers.externa ...

Error encountered while using Chart.js with JSON dataset

Struggling to make this work... Here are the necessary scripts: <script src="Chart.js"></script> <script src="jquery-1.11.3.min.js"></script> This is the full code I am working with: <body> <div id="chartCanvas"> &l ...

Inserting information into a SQL database using PHP

Looking for some assistance here. I've double-checked the SQL table names multiple times, but whenever I try to post, I keep getting an error. I'm fairly new to this, so any help would be greatly appreciated. Thanks in advance. require_once(&ap ...

What is the best way to retrieve the data-id from a button that has been appended?

JavaScript $("#btn").on('click', function(){ $("#result").append("<button type='button' id='btnid' data-id='show' //want to retrieve this class='close pull-right' aria-hidden='true&apos ...

Unable to implement a delegate method effectively

I am currently facing an issue with using Ajax in a bootstrap PHP page. The problem is that I cannot use functions inside the onsubmit event because the button does not exist beforehand. Therefore, I have to resort to using delegate methods instead. Howeve ...

What is the process for forming a series of arrays from one singular array?

If I have a large array consisting of numbers from 1 to 18, is there a simple method to split it into pairs like [1,2], [3,4], [5,6], [7,8], [9,10], [11,12], [13,14] for n=2? The special case of n=2 is all I need. ...

Create a page that expands to full height with the ability to scroll

I am trying to achieve a layout using flex that fills the entire height of the screen. https://i.sstatic.net/s1RA5.png My goal is to have a red background that scrolls based on the content inside. If there is more content, I want it to maintain the same ...

Top recommendations for aligning CSS

As I delve into the world of web development, I've taken a shot at creating the first draft of my website siavoush-re.co.uk,. However, the more I research, the more evident it becomes that the positioning methods I've employed are not as effectiv ...