The navigation bar fails to recognize its own open/closed status

My website's navigation bar is experiencing a logical error that I've identified. Despite no syntax errors being detected by the browser (I'm using Chrome) and partial functionality, there appears to be an issue.

The main problem with my navigation bar lies in its inability to determine whether it is open or closed. This results in scenarios where the menu button incorrectly displays as either close when the menu is already closed, or vice versa.

The current functioning of my navigation bar involves changing its shape every time it is clicked, causing the menu icon to change on each click rather than specifically when the menu opens.

This issue becomes noticeable when resizing the window/screen and opening the menu without closing it.

I embarked on making my navigation bar 'responsive' by adjusting its design based on screen size - side navigation for large screens and top navigation for small screens - which led to the emergence of this problem.

Unfortunately, due to my limited expertise in JavaScript and jQuery, I haven't been successful in resolving this. Are there alternative approaches that are more efficient and yield better results?

Thanks

Edit: To observe the effect mentioned, you need to copy my code, save it, and open it in an external browser (@media queries won't work in the snippet).

Here is the provided code:

function menuTransform(x) {
var y = document.getElementById("navbar");
var z = $(window).width();
x.classList.toggle("change");
if (z > 500) {
$('nav li:not(.menu)').show();
$('nav hr').show();
if (y.style.width === "300px") {
y.style.transition = "ease 0.5s";
$('nav li a span').addClass('link');
$('nav li a span').removeClass('opened');
y.style.width = "80px";
}

else {
y.style.transition = "ease 0.5s";
$('nav li a span').removeClass('link');
$('nav li a span').addClass('opened');
y.style.width = "300px";
}
}

if (z <= 500) {
$('nav li:not(.menu)').hide();
$('nav hr').hide();
if (y.style.height === "100%") {
y.style.height = "80px";
y.style.width = "100%";
y.style.transition = "ease 0.5s";
$('nav li a span').addClass('link');
$('nav li:not(.menu)').hide();
$('nav hr').hide();
}

else {
y.style.height = "100%";
y.style.width = "100%";
y.style.transition = "ease 0.5s";
$('nav li a span').removeClass('link');
$('nav li:not(.menu)').show();
$('nav hr').show();
}
}
}
#navbar {
  min-height: 100%;
  height: 100%;
}

nav {
z-index: 10;
position: fixed;
top: 0px;
background: linear-gradient(to right, rgb(0,0,0), rgb(13,13,13));
width: 80px;
list-style-type: none;
color: white;
box-shadow: 5px 0px 8px rgba(0,0,0,0.3);
overflow: auto;
}

nav li {
display: flex;
justify-content: center;
align-items: center;
}

nav li a {
display: flex;
width: 100%;
height: 80px;
color: white;
text-decoration: none;
justify-content: center;
align-items: center;
transition: 0.3s;
}

.link {
display: none;
}

.opened {
padding: 10px;
padding-top: 11px;
white-space: nowrap;
}

nav li a:hover:not(.active) {
background: rgb(42,42,42);
box-shadow: 0 1px 8px rgba(0,0,0,0.3);
transform: translateY(-1px);
}

.menu {
padding-top: 16px;
padding-bottom: 16px;
}

.hamMenu {
padding-top: -6px;
cursor: pointer;
}

.bar1, .bar2, .bar3 {
background: white;
width: 36px;
height: 5px;
margin: 6px 0;
transition: 0.5s;
}

.change .bar1 {
-webkit-transform: rotate(-45deg) translate(-8px, 7px);
    transform: rotate(-45deg) translate(-8px, 7px);
}

.change .bar2 {
background: rgba(0,0,0,0);
}

.change .bar3 {
-webkit-transform: rotate(45deg) translate(-8px, -8px);
    transform: rotate(45deg) translate(-8px, -8px);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<nav id="navbar">
<li class="menu">
<div class="hamMenu" onclick="menuTransform(this)">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
</li>
    
<li><a class="active" href="home.html"><i class="material-icons">home</i><span class="link">Home</span></a></li>
<li><a href="Videos.html"><i class="material-icons">play_circle_filled</i><span class="link">Videos</span></a></li>
<li><a href="#seminars"><i class="material-icons">tv</i><span class="link">Seminars</span></a></li>
<li><a href="#seminars"><i class="material-icons">school</i><span class="link">Personal Tuition</span></a></li>
</nav>

Answer №1

The menu on the right side of your screen seems to be causing some issues. It currently lacks any defined width or height, as shown in the code snippet below:

.rightMenu {
    padding-top: -6px;
    cursor: pointer;
}

As a result, when you try to interact with this menu, you might find it difficult to do so because it essentially has zero dimensions, despite containing elements that occupy space.

A quick way to visualize the size of this element is by adding an outline around it, as demonstrated here:

.rightMenu {
    padding-top: -6px;
    cursor: pointer;
    outline: 1px solid;
}

While I haven't verified this solution, you may need to adjust the code similar to the following:

.rightMenu {
    padding-top: -6px;
    cursor: pointer;
    width: 36px;
    height: 30px;
}

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

Guide to positioning an icon at the center of a material card

I am new to CSS and Angular Material, and I need help centering the icon on the card following the design from Figma. I tried copying the CSS from Figma, but it didn't center the icon as expected. Can someone please share techniques to achieve this? S ...

Having trouble with jQuery and Ajax POST requests

I designed a basic html page and utilized jquery/ajax to transmit a simple json file to my php server. The behavior of my code is quite puzzling. Initially, I followed suggestions from other Stack Overflow queries: Script1 var data = {"deviceUUID":"ab ...

What is the best way to include a select option in a Jquery Function?

I need to add a selection option for gender in my function, allowing users to choose between male and female. Below is the Function: function Add(){ $("#myTable tbody").append( "<tr>"+ "<td><input type='text'/ ...

What is the best way to create query strings for AJAX URL using jQuery or JavaScript?

At the moment, I am implementing ajax and jquery to dynamically update search results based on different filtering categories within a form. My challenge lies in the fact that I aim to pass varying variables to the URL used for ajax, derived from checkbox ...

Scrolling down a jQuery div after each new item is added.OR

I have a div acting as a chat feature, where messages are appended based on user actions. I am trying to make it so that the div automatically scrolls down after each message is added, but I'm encountering some issues with this functionality. You can ...

Advanced jquery selector looping functionality

I'm struggling to locate a specific list item with a matching ID that contains an anchor element with an href attribute equal to a variable within a <li> tag. Once found, I need to apply a specific class to it. <ul id="elementSelectionList"& ...

Transmit information to a webpage using jQuery AJAX and the .data() function as you navigate to the page

<script> function sendUserData(username){ $.post("userprofile.php", { username:username } ); document.location.href="userprofile.php"; } $(document).on('click','#userprofile',function(){ var username=$(this).data('id4&apo ...

Utilizing AJAX with JSON for passing parameters

function FetchAndSerialize() { $.ajax({ type: "POST", url: "GetDataPage.aspx/SerializeData", data: "{}", contentType: "application/json", dataType: "json", success: function (r ...

Rejuvenating your HTML content with AJAX over time

My HTML page contains links to charts that refresh every time the page is reloaded. A friend mentioned that AJAX can automatically refresh the chart at specified intervals without reloading the entire HTML page. I would appreciate any help with the HTML ...

Using jQuery, you can dynamically fill a field with data retrieved from two separate fields

Is there a way to automatically populate the 'Full Name' field based on what the user enters in the 'First Name' and 'Last Name' fields? For example, if the user types 'John' and 'Smith', the 'Full Nam ...

Display a div underneath a list item using JQuery

I need assistance with displaying each div individually on my list when the user hovers over and off of them. I also want the displayed div to slideUp upon exit. If you'd like to take a look, here is my code on http://jsfiddle.net/AZhgk/ <div id= ...

Updating Your Telerik MVC Grid Using a jQuery Method

Hello, I am facing an issue with two MVC Telerik Grids appearing in a single View. Each Grid contains a Custom Column with an Edit Link When the user clicks on the Edit Link, a dialog model will pop up with a form. After the user hits the save button, th ...

The combination of Bootstrap and Django does not support responsive design

My code is not working correctly, and I'm struggling to identify the errors. The website I'm working on has subpages, but the responsive design doesn't seem to be functioning properly. Could this issue be related to mistakes in my HTML/CSS c ...

Populate the textbox with data from the database when the dropdown selection changes

I am looking to automatically populate the text box with a value from the database when a selection is made from a drop-down list using php When an option is chosen from the drop-down list, the text box should display a corresponding record based on the s ...

Discover the method used to determine the vertical dimensions of a button

Check out my code snippet (here): <div class="modal-body"> <div class="container"> <div class="row"> <div class="col-8 p-0"> <form class="form-group"> <form> <input type="t ...

How do I access a specific child from an object/element retrieved by using the elementFromPoint() method in Javascript?

Is there a method to extract the child element from an element or object retrieved by the function elementFromPoint(x, y); Consider the following scenario: var elem = document.elementFromPoint(x, y); Let's assume that the element returned and saved ...

Javascript problem: Trouble with event.clientX position

I found a great resource on learning javascript at this website, I'm having trouble understanding the code snippets in function XY(e, v), specifically these two lines: event.clientX + document.documentElement.scrollLeft and event.clientY + document ...

Tips for optimizing search functionality in Angular to prevent loading all data at once

An exploration for information within vast datasets is triggered by AngularJS when the input contains more than 3 characters. var app = angular.module('test_table', []); app.controller('main_control',function($scope, $http){ $scope ...

Prevent body from scrolling when mouse is over a div, while keeping the position unchanged

Although I've come across a few discussions about this topic before (including one of my own), I'm still searching for an improvement on the only solution that has actually worked for me. My goal is to enable mouse wheel scrolling for a height-l ...

The background color should only cover a specific percentage of the element

Here is the current structure I have: <li class="myclass" ng-class="myAngularClass"> <div> div1 <div> div2 </div> </div> </li> The li element has dynamic dimensions and width. The class "myAngularClass" i ...