Customize the slide-in menu on your WordPress theme by adding jQuery and enhancing it with CSS animations

I'm attempting to create a sliding menu that appears when clicked and disappears when the close button is activated. Unfortunately, I can't seem to get it to slide out properly.

    jQuery(function($){
    $('#menu-open').click(function() {
        $('#slider-background').toggleClass('section-transform-active');
        $('#slider-menu').toggleClass('menu-container-active');
    });
    $('#menu-close').click(function() {
        $('#slider-background').toggleClass('section-transform-active');
        $('#slider-menu').toggleClass('menu-container-active');
    });
});

/button>



<div id="slider-background" class="section-transform">
      <button id="menu-close" style="float:right">X CLOSE</button>
  <div id="slider-menu" class="menu-container">
  <ul>
  <li>MENU ITEM 1</li>
    <li>MENU ITEM 2</li>
      <li>MENU ITEM 3</li>
        <li>MENU ITEM 4</li> 
  </ul>
  </div>
</div>

.section-transform {
    cursor: pointer;
  opacity: 0;
  -webkit-transition: width 1s ease;
    -moz-transition: width 1s ease;
    -o-transition: width 1s ease;
    -ms-transition: width 1s ease;
    transition: width 1s ease;
 }
  
.menu-container {
  padding-top: 50px;
  color: #888;
  font-size: 20px;
  opacity: 0;
}
  
.menu-container li:hover {
  color: #aaa;
}
  
.menu-container-active {
    opacity: 1;
    height: 100%;
    width: 100%;
}
  
.section-transform-active {
  opacity: 1;
    height: 100%;
    width: 50%;
    z-index: 99999;
    background: rgba(0, 0, 0, 0.8); 
}

Fiddle Link : https://jsfiddle.net/sad437xv/38/

In the jsfiddle demo, the sliding motion isn't smooth as intended. The transition seems abrupt instead of gradual. Additionally, trying to replicate the structure in Divi builder has been challenging. I would like the menu items/buttons to smoothly slide in along with the background overlay, but currently, the animation causes the text to appear jittery as their box width expands.

If you have any insights on where I might be going wrong, your help would be greatly appreciated. Thank you!

Answer №1

Here is an example of using slideToggle():

$(function($){
    $('#menu-open').click(function() { 
        $('#slider-background').animate({width:'toggle'}, 250);
    });
    $('#menu-close').click(function() { 
        $('#slider-background').animate({width:'toggle'}, 250);
    });
});
.section-transform { display:none; }
.menu-container { padding-top: 50px; color: #888;font-size: 20px;}
.menu-container li:hover {color: #aaa;}
.menu-container-active {opacity: 1;height: 100%;width: 100%;}
.section-transform-active {opacity: 1;height: 100%;width: 50%;z-index: 99999;background: rgba(0, 0, 0, 0.8);}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="menu-open">MENU OPEN</button>
<div id="slider-background" class="section-transform">
  <button id="menu-close" style="float:right">X CLOSE</button>
  <div id="slider-menu" class="menu-container">
    <ul>
      <li>MENU ITEM 1</li>
      <li>MENU ITEM 2</li>
      <li>MENU ITEM 3</li>
      <li>MENU ITEM 4</li> 
    </ul>
  </div>
</div>

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

Maintain the div's aspect ratio by adjusting its width according to its height

I am seeking a way to maintain the width of the .center div in relation to its height, following a 4:3 aspect ratio (4 units wide for every 3 units high). The height of the div should be set to 100%, and I also need to align the div horizontally at the cen ...

Ensure that a div element expands to 100% of its parent's height when its child's height is less than 100%

I am struggling with a flex div that contains 2 elements. I want both elements to take up the full parent height while their content remains at auto height, but I keep getting the inner content height. Here is my code: <html lang="en"> <head&g ...

Iterate through TD elements to retrieve their values

Is there a way to retrieve the value of my TD for deleting dates from my database using jQuery? This is the current code snippet I have: $(document).ready(function() { $('table#delTable td a.delete').click(function() ...

Automatically scroll to the end of the data retrieved through ajax

I am currently developing a live chat website for my users and I am using ajax to fetch the chat messages. However, I am facing an issue where whenever I enter a new message, it does get added to the database and displayed, but the scroll doesn't auto ...

Exploring ways to customize the input color of Material UI TextField when it is disabled [Version: 5.0.8]

I am having trouble changing the border color and text color when an input is disabled. I have tried multiple variations, as seen below: const textFieldStyle = { '& label': { color: darkMode?'#1976d2':'', } ...

When HTML elements are dynamically inserted through JavaScript using quilljs, they may cause conflicts with the layout properties

I am currently working on creating a simple webpage layout similar to that of Stack Overflow, with a sidebar and a main content area that can scroll. In my case, the content area is intended to host a QuillJS text editor. To integrate the QuillJS editor i ...

Tips for retrieving an element that has been dynamically added to the page in jQuery after it has loaded

Here is the code I used to dynamically add list items to an unordered list after the page has fully loaded: <ul id='hints'></ul> This jQuery script is responsible for adding a new list item to the ul element once the page has finis ...

Designing in Ionic 3.x: Implementing Ion-Icon with a gradient backdrop (ion-chip)

Is there a way to add a gradient background to an icon? I attempted to nest the ion-icon within an ion-chip, like so: <ion-chip class="my-chip"> <ion-icon name="basket></ion-icon> </ion-chip Then, in the .css file: ...

Fixing blurry text on canvas caused by Arbor.js mouse event issues

Currently, I am utilizing arborjs in my project. The text within the canvas is created using fillText in html5. While everything functions correctly on a Retina display MacBook, the text appears blurry. To address this, I applied the following solution: v ...

AJAX request is successful, however, the database remains empty with no data being inserted

I have been attempting to utilize ajax and php to insert data into a database, but for some reason, it is not functioning as expected. Despite validating all the itemName, category, and price fields in the html file, the data being inserted into the data ...

Buttoned Up: The Troubling Tale of jQuery and

Seems like this question might be a duplicate, but I'm not bothered. Despite searching on Google, I haven't found a solution. What could be the mistake here? $(document).ready(function() { $("#foo").click(function() { $.ajax({ type ...

Deliver tailored javascript code for mobile devices

Is there a way to display different javascript on mobile devices in a similar way to CSS media queries, as I am introducing a menu with mouseover elements that may not work well on touchscreens? Just for reference, the menu structure is somewhat similar t ...

Arranging elements in columns using Bootstrap

I am facing an issue with my columns in bootstrap, as they are currently appearing vertically, one above the other. I need them to be displayed side by side https://i.sstatic.net/ZKdj7.png Here is the code I am using: <div class="col-xl-4 col-lg ...

Opt for a line break over a semicolon when using jQuery

I need assistance with breaking the line wherever a semicolon is present. var locdata = { "locations": [{ "id": 0, "name": 'USA', "cx": 100, "cy": 100, "address":'545, 8t ...

Sliding Image Menu using jQuery

I am struggling with creating a menu using jquery mouseenter / mouseout effects. My goal is to have a small icon displayed that expands to the left and reveals the menu link when a user hovers over it. The issue I am facing is that the effect only works w ...

The onclick attribute on a button in HTML is not functioning properly following the inclusion of an Ajax load

I am facing an issue with a button that is being dynamically loaded from PHP code using Ajax. The button has an onclick attribute that calls a function named viewImage. The Ajax request is triggered every 5 seconds inside the loadImages function. Snippet ...

"Exploring the versatility of the width property in flexbox

If I change the flex-wrap to nowrap in the given code snippet with a width of 33.3333333333%, what would be the new calculation for the width? .box{ color: white; font-size: 100px; text-align: center; text-shadow: 4px 4px 0 rgba(0, 0, 0, 0, 1); ...

The process of creating a React build varies greatly from the initial development phase

Thank you for offering to help me! After building my React web app, it looks very different from development mode. I use serve -s build to monitor the app build. However, even on my online DigitalOcean server, it doesn't appear the same as in develop ...

Guide for verifying the minimum and maximum values when selecting a particular product from a dropdown menu

My goal is to implement validation for the width textbox when a user selects a specific product from the dropdown menu. The validation should only allow the user to enter a value between 10 and 200. I have successfully retrieved the selected value, but I ...

Avoid letting words be separated

My question involves HTML code <div class="col-md-4">...</div> <div class="col-md-4">...</div> <div class="col-md-4">Lorem Ipsum</div> When I view this on a mobile device, the text appears as follows: Lorem Ip- sum I ...