Minimize JavaScript code for simplifying jQuery CSS modifications (show/hide off-canvas navigation)

I am currently working on developing a custom off-canvas navigation system that is similar to the functionality of Foundation. Essentially, I have 2 icons that trigger CSS adjustments when clicked. I am contemplating whether it is feasible to streamline the code by consolidating these actions into a single toggle function rather than having separate functions for opening and closing with distinct divs. This is the current setup I am working with:

jQuery(".navMenuOpen").click(function(event) {
    event.preventDefault();
    jQuery(".page-wrapper").css("left", "70%");
    jQuery(".off-canvas-nav").css("left", "0")
    jQuery(".navMenuOpen").css("display", "none");
    jQuery(".navMenuClose").css("display", "block");
});
jQuery(".navMenuClose").click(function(event) {
    event.preventDefault();
    jQuery(".page-wrapper").css("left", "0");
    jQuery(".off-canvas-nav").css("left", "-100%")
    jQuery(".navMenuOpen").css("display", "block");
    jQuery(".navMenuClose").css("display", "none");
});

My question is: Is there a way to simplify this process down to a single function/HTML element?

Thank you in advance!

Answer №1

You may want to experiment with something along these lines.

$(".menuIconOpen, .menuIconClose").click(function (event) {
    var containerLeft = '80%',
        sidebarLeft = '0',
        openVisibility = 'hidden',
        closeVisibility = 'visible',
        $this = $(this);

    event.preventDefault();

    if ($this.hasClass('menuIconClose')) {
        containerLeft = '0',
        sidebarLeft = '-100%',
        openVisibility = 'visible',
        closeVisibility = 'hidden';
    }
    $(".content-wrapper").css("left", containerLeft);
    $(".sidebar-menu").css("left", sidebarLeft)
    $(".menuIconOpen").css("visibility", openVisibility);
    $(".menuIconClose").css("visibility", closeVisibility);
});

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 am experiencing an issue with setting the border to none using .navbar-toggler

I'm currently utilizing bootstrap 5, and I've encountered an issue with setting the border and outline to none in my CSS file. Specifically, when I apply these properties to the nav-toggler class, it seems to have no effect. .menu-bar .navbar- ...

Tips for eliminating the border surrounding the entire table in Material-ui

Is there a way to edit a table in Material-ui without displaying it as a card? I'm looking to remove the border around the entire table, but I couldn't find any information on how to do that. Can someone help me out? Here is a link about the com ...

Styling a textbox within a gridview using JavaScript

Looking for a way to format a textbox within a gridview using JavaScript? Take a look at how the gridview columns are set up: <Columns> <asp:TemplateField> <ItemTemplate><asp:ImageButton runat="server" id="imgbtnEnterAmt" ...

Enlarge image when clicked

I need help creating a gallery page where users can click on an image to enlarge it, then navigate through the images using arrows on either side. I am not familiar with JavaScript, so could someone guide me on how to achieve this? Also, I would apprecia ...

Creating a stylish CSS effect by adding a dashed border underneath two div elements

Looking for help with a layout where there are two tables side by side, represented by the blue boxes. The goal is to be able to select a row in each table and then click the red link button below. The connecting lines between the boxes and the link button ...

Discovering the index of an item in Angular

My delete function emits socket.io to update the other party's items list and remove the specific item. The issue arises when I receive the socket data as I struggle to find the matching item to update it. Logic User 1 deletes a message User 2 receiv ...

Incorporate a jQuery function into a PHP variable

Is there a way to write a complete JQuery function inside a PHP variable so that it can be called wherever the variable is placed? The variable should include callback JSON text and PHP variables. If this is not possible, is there an alternative approach ...

packages have gone AWOL in the Bitbucket pipeline

Hello everyone. I am currently working on setting up a pipeline for my nextjs-project and I've encountered a problem. I have a specific function for generating buildId # scripts/generate-build-id.js const nextBuildId = require('next-build-id&ap ...

Issues with resetting AngularJS form---"The AngularJS form

I have been putting in a lot of effort to make this work. Despite my knowledge about child scopes, prototypal inheritance, and the use of dot notation for the model, I am facing an issue with resetting the form. You can access the hosted form here. The rel ...

Avoid passing down line-height settings to elements within nested elements

Is there a way to prevent the span styled with font-size: 12px from inheriting the line-height of the span styled with font-size:18px? I need the line-height value to be 1.5 for both 18px and 1.5px font sizes, but I am unable to separate the two spans. ht ...

Issues with Ajax.BeginForm causing JSON to be returned as undefined (Confirmed by Dev Tools)

I'm facing a strange issue while attempting to retrieve a basic JSON object from my controller. Although I can see the JSON returned perfectly fine in Firefox debug tools, when I look at my javascript callback, I receive an undefined in my console.log ...

Using Css to Style an Image within Html.ActionLink

Html.ActionLink("", "ActionResult", new { CustomerId= DataBinder.Eval(c.DataItem, "CustomerId") }, new { target = "_blank", @style = "background-image:url(/Image/edit.png); width:50px; height:30px;" })); Hello there, I am facing an issue setting an image ...

Bootstrap table malfunctioning following completion of ajax request

I am currently facing an issue with my ajax call in my MVC project. Whenever the user clicks on a value using the select, it updates two tables in the project. However, I have noticed that on every other call, the button functionality on the tables breaks. ...

Conceal the loading spinner in JQuery once the image has finished loading

I am working with a jQuery function that captures the URL of an image link and displays the image. However, my issue lies in managing the loading process. I would like to display a LOADING message and hide it once the image has fully loaded, but I am strug ...

Is there a way to connect cell values to correspond in Google Sheets?

I have a spreadsheet on Google Sheets containing financial data from companies' statements. This includes both quarterly and annual information, with a dropdown cell at the top allowing me to choose between "Annual" and "Quarterly" data. However, scro ...

Obtain the total by adding values from additional radio buttons selected

Need assistance with summing values from multiple radio inputs dynamically. Want to calculate the total price ('$res' variable) after checking them. Despite trying to change 'settype' to int, it didn't work out as expected. Any hel ...

What strategies can we implement to ensure consistency in visual styles and templates across our microservices?

Our team is currently working on multiple microservices simultaneously. While traditional microservice architecture discourages code sharing and reuse between services, we are considering the benefits of consistent styling across all services that have Web ...

What is the process of duplicating an element using jQuery?

I have successfully implemented a drag and drop feature using jQuery, but now I want to replace the dragged element with another in its original position. Although I attempted to use the clone() method, being new to jQuery has resulted in the cloned eleme ...

Uploading files on a web page using AJAX technology

I'm attempting to perform a file upload on an ajax response page. The main.php file contains basic ajax code as shown below: <html> <head> <script type="text/javascript"> function loadXMLDoc() { var xmlhttp; if ...

Is there a way to utilize both 'require' and 'import' in a Node.js script simultaneously?

Successfuly set up a basic Node.js project with a package.json. The goal is to install a package and use both the require and import statements in the same file. So far, here is what I have attempted but it is not working as expected. First Attempt // pack ...