"Discover the magic of creating a navigation menu with jQuery toggle - let me show

Recently, I managed to create a side navigation using the target function in CSS3. However, I am curious about how I can achieve the same result using jQuery without disrupting the layout. Any assistance would be greatly appreciated. Here is the code snippet:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html" charset="utf-8">
<style type="text/css">
@-webkit-keyframes nav {
    from {left:-100px;opacity: 0;}
    to {left:0px;opacity:1;}
}
@keyframes nav{
    from {left:-100px;opacity: 0;}
    to {left:0px;opacity:1;}
}

body {
margin:0;
padding:0;
background-color:#EFEBE9;
}
.navigate {
width:100%;
height:100px;
background-color:#FFF;
padding-bottom:1px solid #F6F6F6;
display:inline-block; 
position:fixed;
}
.navigate > .navicon {
width:2em;
height:100px;
background-color:#1565C0;
color:#FFF;
font-size:4em;
text-align:center;
}
.navigate > .navicon > a {
text-decoration:none;
color:#FFF;
}
.menu {
display:none;
}
.menu:target {
display:table;
background-color:#0D47A1;
}
nav {
width:40%;
height:100%;
background-color:#1565C0;
position:absolute;
-webkit-animation-name:nav;
  -webkit-animation-duration: 0.5s; 
  animation-name:nav;
  animation-duration: 0.5s;
}
.closebtn {
color:#FFF;
font-size:2.5em;
margin:.3em;
float:right;
text-decoration:none;
}
.closebtn > a {
text-decoration:none;
font-size:1em;
color:#FFF;
}



</style>
</head>
<body>
<div class="navigate">
<div class="navicon" onclick="window.location.href='#menu';" >☰</div>
</div>
<div class="menu" id="menu">
<nav>
<div class="closebtn"><a href="">✖</a></div>
</nav>
</div>
</body>
</html>

Answer №1

I made some adjustments to your CSS and included additional jQuery code.

$('.navicon').click(function(){
    $('#menu nav').addClass('nav-open');
});
$('#menu .closebtn').click(function(){
    $('#menu nav').removeClass('nav-open');
});

Check out the updated version here: http://jsfiddle.net/btgqh3oe/1/

Answer №2

$('#menu').hide();
$('#navicon').click(function () {
    $('#menu').toggle('slow');
})

Start by using the hide() function to hide the menu.

Then, employ the toggle() method to display and conceal the menu upon button click.

Check out the demonstration here.

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

Access and expand a bootstrap accordion with a hyperlink

Here is a concept I want to make happen: For my project, I am utilizing Bootstrap v3.4.1 and have set up two columns next to each other. My goal is to link the elements in the hotspot banner (left column) to trigger and open the corresponding Accordions ( ...

Present XML data on an HTML page to generate interactive form features

I have an application that features a checkbox tree. I would like to automatically pre-select those checkboxes if the user had previously checked them. To achieve this, I receive XML data from my backend Perl script which contains information on which che ...

"Exploring the world of CSS3: Arrow shapes and animated effects

Looking at the image provided, my task is to create and animate it. I was considering using CSS3 border-radius instead of an actual image. Below are the HTML and CSS code that I have so far. The animation I envision involves the arrow gradually appearing ...

I am unable to achieve negative X degree rotation of the image while using mousemove to rotate it

I need assistance with moving a picture in 3D. I want the offsetX of the mouse to be positive when it's past half of the picture, and negative otherwise. How can I achieve this effect for rotation degrees? This is what I have tried: $('#img ...

Utilize PHP to input and swap information within SQL databases

TABLE1: table1ID | studentnum | lname | fname | mname TABLE2: table2ID | studentnum | remarks | others //FORM 1 CODE $sql = "INSERT INTO TABLE1 (studentnum, lname, fname, mname) VALUES ('$studentnum','$lname','$fname','$ ...

Arranging data into columns using HTML and CSS

I'm currently working on a CSS solution to organize various elements like buttons and text in a tabular column format. My main challenge lies in controlling the layout effectively, especially since CSS columns are not compatible with IE9 and earlier v ...

Guide to automatically dismiss calendar popup after selecting a date

After selecting a date, how can I hide the calendar? I am currently utilizing Date-time-picker by DanyelYKPan. Is there a specific function that I should be using? Here is the code snippet: <div class="col-3"> <div class="form-group calenderF ...

When utilizing jQuery post to send data to a node.js server, the request is abruptly terminated before completion

When sending a post request to my node.js server, I use the following code: $.post("/admin/google",{result: result, testName: testName}, function(data){ console.log(data); if(data.status === 200) { alert ...

How can I create a new PHP table using data from an existing table?

I have a table displayed on my website with the code snippet providedview the table image here Here is the code for generating this table: <?php $query = $db->query("SELECT * FROM bit_exchanges ORDER BY id DESC LIMIT 20"); if($query-> ...

Apply a border to the div that has been selected

I have a tool for storing information and I am using *ngFor to display each instance in a line. Is there a way to add a border when clicking on a line? The border should only appear on the clicked line, disappearing from the previous one if another line i ...

The rules for prioritizing CSS selectors

Struggling with CSS selector rules and specificity? I've already checked this for specifics. Even tried using firebug and inspecting elements in Chrome to copy the CSS path, but still can't get it to work. This is my first time creating a website ...

Show images aligned in the center with proper positioning

img { vertical-align: middle; } .right { ??? } <img align='right' class='right' src='images/online_icon.png' border="0" width=8/> I am trying to position images on the right side while keeping them vertically aligned ...

The issue of HTML email background color not showing up in Gmail, mobile, iOS, or dark mode

Introduction I created the email using MJML code and then converted it to HTML. Everything is functioning correctly, except for the background color not displaying under specific conditions: If the email client is Gmail If the mobile version is being use ...

Adjusting the margin on an element causes a shift in the entire page's

Why is the margin of an h2 element on my page displacing the entire body downward? This seems abnormal since the h2 element is within a div container. Is there a way to address this issue? ...

Steps for displaying innerHTML values conditionally with a pipe

Currently working with Angular 8 and looking to conditionally implement the innerHTML feature using a translation pipe. .html <button type="button" mat-flat-button // utilizing translate module internally [innerHTML] = "display ? (HIDE ...

Angular select automatically saves the selected option when navigating between views

I would like the selected option in my dropdown menu to stay selected as I navigate through different views and then return back. Here is the view: <select ng-model="selectedSeason" class="form-control" ng-options="season as 'Season '+ seas ...

Tips on how to retrieve the current value of a scope variable in jQuery

When making a $http request to save data on the server and receiving a json response, I want to show an Android-style message using Toast for either success or failure based on the response. Initially, I set a scope variable to false $scope.showSuccessToa ...

Extract content within Python pre tags

A Python code snippet is being used to extract content between PRE tags: s = br.open(base_url+str(string)) u = br.geturl() seq = br.open(u) blat = BeautifulSoup(seq) for res in blat.find('pre').findChildren(): seq = res.string ...

Having trouble getting custom tabs in jQuery to function properly?

I am facing an issue with a simple script I have created. The script is supposed to display a specific div when a user clicks on a link, and hide all other divs in the container. However, when I click on the link, nothing gets hidden as expected. Even afte ...

Troubleshooting on Safari for iOS

As I work on my portfolio website using only HTML 5 without any JavaScript, I encountered an issue with the about page not functioning properly on iPhones. There seems to be a problem with scrolling as some elements are fixed and do not move along with t ...