Creating a dynamic navigation bar with CSS and Javascript for maximum flexibility

I'm looking to create a menu that functions similarly to the image shown

  1. The red arrow signifies that when I click on the button, it will toggle the visibility of the menu

  2. The orange arrow indicates that when the menu is hidden, the box should expand in width and vice versa

https://i.sstatic.net/LP5cv.png

Currently, I have created the HTML structure, but I am facing a couple of issues

1) The sliding effect doesn't occur simultaneously with hiding/showing the content. I would like it to hide and slide at the same time.

            $("#menu_btn").on("click", function () {
                var menu = $("#left_menu #btn");
                if (menu.css('display') !== "none") {
                    $("#left_menu #btn").hide("slide", {direction: "left"}, 1000);
                } else {
                    $("#left_menu #btn").show("slide", {direction: "right"}, 1000);
                }
            });

2) How can I integrate code to adjust the width after the sliding effect? Additionally, the box is positioned absolutely within the container - how can I maintain the same top position after expanding/reducing the content?

$(".content #bg").css("width","600");
$(".content #bg").css("width","1000");

Thank you for any assistance provided.

Answer №1

If you need a solution, try something similar to this example: https://jsfiddle.net/7k4kdmxu/4/

CSS:

.container{width:100%; margin:0 auto;}
nav{
   width:20%;
    float:left;
     background:#efefef; 
}
.openNav{ width:20%;}
#content.openNavContainer{ width:80%!important;}
.closeNav{width:10%;}
#content.closeNavContainer{ width:90%!important;}
#content{
    width:80%;
    float:right;
    background:#e9e9e9; 
}
nav ul {list-style:none;}
nav ul li{list-style:none; display:block;}

HTML

<div class="container">
<nav>
    <a href="#" id="open">Open -></a>
    <ul>

        <li><a href="#">HOME</a></li>
 <li><a href="#">ABOUT</a></li>
 <li><a href="#">OUT VALUE</a></li>
 <li><a href="#">SEASONAL</a></li>
        <li><a href="#">CONTACT</a></li>
   </ul>
    <a href="#" id="close"><- Close </a>
</nav>
    <div id="content"><p>Body Content Can Go Here</p></div>
</div>

JS/JQ

$(function(){
$("#open").click(function(){

    $("nav").addClass("openNav");
     $("nav").removeClass("closeNav");
    $("#content").addClass("openNavContainer");
    $("#content").removeClass("closeNavContainer");
});
$("#close").click(function(){
        $("nav").addClass("closeNav");
      $("nav").removeClass("openNav");
    $("#content").addClass("closeNavContainer");
    $("#content").removeClass("openNavContainer");
    });
});

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

Remove the border when the menu is clicked on and add an overlay

Hey there, I'm Daniel and currently working on my portfolio website. My main goal is to implement an overlay effect when the hamburger menu is clicked on mobile devices. This overlay will darken the background to highlight the menu options. The overla ...

Creating styles with CSS to position text below an image

123.456.789.000/index.php <html> <head> <title>Unique Triathlon Club</title> <meta charset=iso-8859-1" /> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <ul id="nav ...

Tips for integrating jQuery AJAX with PHP arrays for efficient data handling

Here is my jQuery AJAX request: $.ajax({ type: 'POST', url: 'processor.php', data: 'data1=testdata1&data2=testdata2&data3=testdata3', cache: false, success: function(result) { if(result){ ...

Save a JSON object from a URL into a JavaScript variable using jQuery

I am trying to figure out how to use the .getJSON() function to store a JSON object as a JavaScript variable. Can someone guide me through this process? var js = $.getJSON(url, function(data) {...} ); Is it necessary to include the function(data) { ...

Buttons in Highcharts

Is there a way to incorporate a button within a highcharts graph? I am working with a bar chart and would like to include a button on the left side of the label, similar to the example shown in this bar chart demo. I want the button to appear before the ...

The disadvantages of utilizing multiple Ajax requests

I am in the process of developing a mobile application for Android using PhoneGap and jQuery Mobile. One key feature of the app involves sending messages to a server-side script using Ajax requests. To ensure a smooth user experience, I have implemented a ...

What is the proper way to utilize Vue and Buefy Tab components?

I'm currently designing a Vue page that includes the Buefy b-tab element. My request is quite simple, as outlined below: The page consists of a Buefy Tab element and a button. https://i.sstatic.net/TkiVYfJj.png Upon clicking the tick button, it sho ...

Implement dynamic routes within your Nuxt single page application once it has been generated

I'm currently exploring the possibility of implementing dynamic routes in a Nuxt SPA. While I am familiar with using dynamic routes in Universal Mode and generating them during build time through functions, I am searching for a solution that does not ...

``I am having trouble getting the Bootstrap carousel to start

I am having trouble getting the Bootstrap Carousel to function as expected. Despite including all the necessary code, such as initializing the carousel in jQuery.ready, the images are stacking on top of each other with navigation arrows below them instea ...

What is the reason behind the necessity of having event triggers created within the AJAX request?

What's the reason behind this functioning: $.ajax( { url: "/some/url.php", data: { s:"stuff" }, success: function(result) { // Result is <button id="clickme">Click me!</button> $("#container").html(result); ...

Loss of Content Loaded into ViewModel in ASP.NET MVC 3 After Callback

I've created a callback jQuery function to populate my ViewModel with values from some collections. However, I'm facing an issue where upon calling the function again, the data populated in the first callback seems to get lost. // -------------- ...

jQuery dialog unexpectedly expanding to full page in web browser

My issue involves a WebGrid dialog that showcases a list of items: @if (Model.ItemsByLocation != null) { @grid.GetHtml( tableStyle: "table", fillEmptyRows: true, headerStyle: "header", footerStyle: "footer", mode: WebGridPagerMode ...

How can I add data to a relational table that includes a foreign key reference?

There are two tables that are related with a one to many relationship: envelopes: CREATE TABLE envelopes ( id integer DEFAULT nextval('envelope_id_seq'::regclass) PRIMARY KEY, title text NOT NULL, budget integer NOT NULL ); transact ...

Unable to prevent a touchend event from being canceled due to the event being non-cancelable, such as when scrolling is currently ongoing and cannot be disrupted

const SlideSettings = { dots: false, arrows: false, infinite: false, speed: 500, slidesToShow: 1, slidesToScroll: 1, draggable: true, // swipe: false, currentSlide: activeLocation, afterChange: (newIndex: number) => { setActiveLo ...

Unable to access GLB model after building it in Three.js

Using GLTFLoader, I successfully imported a model that works flawlessly with npm run dev. However, when I attempt to build it using npx vite build, the model file seems inaccessible. The file is located at /public/models/miku_pde.glb. When running npm run ...

discord.js: Bot keeps sending duplicate embeds

I recently set up my discord bot to respond with a message when I enter a specific command, but I've run into an issue where it's sending the same embed twice. I've tried troubleshooting, but so far I haven't been able to pinpoint the c ...

Creating a Masonry Slider with varying heights and widths of images is a simple and effective way to showcase diverse content in a visually

I am looking to implement a unique grid image slider that accommodates images of varying heights and widths. I envision something similar to the example shown below. The image showcases a pattern where the sliders alternate between square images and two r ...

What could be causing my Angular to malfunction?

I've encountered some challenges while trying to run angular on my computer. I have been studying angular through demos on w3 schools that showcase various components. Currently, I am experimenting with this one: http://www.w3schools.com/angular/try ...

Ensure that the search input field is in focus whenever the showSearch state is true

Having difficulty focusing on the input field whenever the showSearch state is true. Utilizing useRef and useEffect for this purpose. When the showSearch flag changes, the useEffect hook is triggered to check if showSearch is true, and if so, it should foc ...

Encountering a challenge while rendering an Ajax partial using link_to to specify a controller action

I am facing an issue with rendering different partials into a div (id="content") on a "proposition" show page. The partial to be rendered depends on the selected tab at the top of the div. To achieve this, I have been attempting to use AJAX and the follow ...