When the height of the navigation bar is adjusted, the content on the page is shifted

I am facing a challenge that I need help with. I have a button that, when clicked, should expand the navigation bar to cover the entire screen without pushing down the content below it. However, despite setting the height of the navigation bar to 100vh, the page remains scrollable and the content is not fully covered. Here is the code snippet I have been working on:

<div class="wrapper">
  <div class="top-nav">
    <nav id="nav">
      <div class="nav-bar">
        <ul>
          <li><button onclick="expandNav()"><i class="fal fa-bars"></i></button></li>
          <li><a href="#"><i class="fab fa-connectdevelop"></i></a></li>
          <li><a href="#">Ban</a></li>
          <li><a href="#">Warn</a></li>
          <li><a href="#">Gift</a></li>
          <li><a href="#">Records</a></li>
          <li><a href="#">Moderator</a></li>
          <li><a href="#">News</a></li>
          <li><a href="#">Edit</a></li>
          <li><a href="#"><i class="fas fa-user-circle"></i></a></li>
          <li><a href="#"><i class="fal fa-sign-out-alt"></i></a></li>
        </ul>
      </div>
    </nav>
  </div>
  <div class="main-content">

  </div>
</div>
<script>
  function expandNav() {
    document.getElementById("nav").style.height = "100vh";
  }
</script>

My CSS code is as follows:

body {
  margin: 0;
  padding: 0;

  font-family: sans-serif;
  -webkit-font-smoothing: antialiased;
}

.wrapper {
  height: 100%;
}

.top-nav .nav-bar {
  width: 82%;
  transition: 0.5s;
}

/* START -- Disabling resizing affects*/

...

/* END -- Disabling resizing affets*/

... 

Essentially, my issue lies in the fact that the navigation bar does not cover the entire screen even after setting the height to 100vh. I want it to behave like Apple's website menu where it occupies the whole page without pushing the content down. For clarity, you can visit the Apple website, resize the window horizontally, and click on the menu to see how it expands.

Please refer to this screenshot for a visual representation of my current progress.

Any assistance would be greatly appreciated!

Answer №1

To resolve the issue of navbar height being set to 100vh, add overflow: hidden on the body element.

document.getElementsByTagName("body")[0].style.overflow = "hidden";

function expandNav() {
  if (document.getElementsByTagName("body")[0].style.overflow === "hidden") {
    document.getElementById("nav").style.height = "";
    document.getElementsByTagName("body")[0].style.overflow = "";
  } else {
    document.getElementById("nav").style.height = "100vh";
    document.getElementsByTagName("body")[0].style.overflow = "hidden";
  }
}
body {
  margin: 0;
  padding: 0;
  font-family: sans-serif;
  -webkit-font-smoothing: antialiased;
}

.wrapper {
  height: 100%;
}
/* Other CSS rules for navigation bar and content */
<script src="https://use.fontawesome.com/eed659c9d4.js"></script>

<div class="wrapper">
  <div class="top-nav">
    <nav id="nav">
      <div class="nav-bar">
        <ul>
          <li><button onclick="expandNav()"><i class="fa fa-bars"></i></button></li>
          <li><a href="#"><i class="fab fa-connectdevelop"></i></a></li>
          <li><a href="#">Ban</a></li>
          <li><a href="#">Warn</a></li>
          <li><a href="#">Gift</a></li>
          <li><a href="#">Records</a></li>
          <li><a href="#">Moderator</a></li>
          <li><a href="#">News</a></li>
          <li><a href="#">Edit</a></li>
          <li><a href="#"><i class="fas fa-user-circle"></i></a></li>
          <li><a href="#"><i class="fal fa-sign-out-alt"></i></a></li>
        </ul>
      </div>
    </nav>
  </div>
  <div class="main-content">

  </div>
</div>
<script>
</script>

For an alternative solution, you can use the following script:

function expandNav() {
  if (document.getElementsByTagName("body")[0].classList.contains("openNavbar")) {
    document.getElementsByTagName("body")[0].classList.remove("openNavbar");
  } else {
    document.getElementsByTagName("body")[0].classList.add("openNavbar");
  }

}
body {
  margin: 0;
  padding: 0;
  font-family: sans-serif;
  -webkit-font-smoothing: antialiased;
}

.wrapper {
  height: 100%;
}
/* Other CSS rules for navigation bar and content */
<script src="https://use.fontawesome.com/eed659c9d4.js"></script>

<div class="wrapper">
  <div class="top-nav">
    <nav id="nav">
      <div class="nav-bar">
        <ul>
          <li><button onclick="expandNav()"><i class="fa fa-bars"></i></button></li>
          <!-- Other list items for the navigation menu -->
        </ul>
      </div>
    </nav>
  </div>
  <div class="main-content">

  </div>
</div>
<script>
</script>

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

Efficiently determine optimal menu item dimensions using CSS automatically

Is it possible to automatically position menu items using CSS so that the position is recalculated when the menu item text changes? Currently, I have a menu created with ul li's and I am using padding for positioning. However, if the text becomes long ...

Struggling to integrate a Bootstrap theme into your Ruby on Rails application?

Having trouble importing a bootstrap theme into my rails application. https://github.com/puikinsh/sufee-admin-dashboard I've been struggling to import this template for two days now with no success. It should be simple, but I can't figure out w ...

Utilize Angular2's input type number without the option for decimal values

Is there a way to prevent decimals from being entered in number inputs for Angular 2? Instead of using patterns or constraints that only invalidate the field but still allow typing, what is the proper approach? Would manually checking keystrokes with the ...

When the Escape key is pressed on the modal, it activates the escape event on the parent component

Welcome to the main page. "use client"; import React, { useEffect, useState } from "react"; import TestModal from "./TestModal"; const App = () => { const [isOpen, setIsOpen] = useState(false); const [isDiscardModalOp ...

Steps for modifying the CSS style of a div element following an onclick event:

<html> <head> <style type="text/css"> .step_box { border: 1.0px solid rgb(204, 204, 204); border-radius: 10.0px 10.0px 10.0px 10.0px; } .step_box:hover{ background: rgb(184, 225, 252); } .selected { background-color : #fff000; ...

I am currently encountering a problem in my attempts to retrieve data from mongodb

I'm having trouble retrieving some data from my database, and I'm not sure why it's not displaying correctly. Any help or feedback would be greatly appreciated! // Below is the code for my router: router.get('/add', (req, re ...

Struggling to locate the module 'firebase-admin/app' - Tips for resolving this issue?

While working with Typescript and firebase-admin for firebase cloud functions, I encountered the error message "Cannot find module 'firebase-admin/app'" when compiling the code with TS. Tried solutions: Reinstalling Dependency Deleting node_modu ...

Having difficulty executing a function inside a JavaScript file

Within my index.php file, the following lines of code are present: <!doctype html><html class=""><head><title>a title</title> <link href="_css/boilerplate.css" rel="stylesheet" type="text/css"> <script type="text/jav ...

AngularJS, perform an action on an HTML element within an ng-repeat directive once the user interface has been rendered

When using AngularJS to add a row to an HTML table by clicking an "Add row" button and using $scope.photos.push({}), you may encounter an issue when trying to trigger the file dialog of the new row's file input field. Is it possible to do this and if ...

The method of displaying milliseconds in a JavaScript countdown timer

I have a piece of javascript code that is responsible for showing a countdown timer starting from 5 minutes. Here is the code snippet: var mins var secs; function startCountdown() { mins = 1 * m("05"); // set the minutes here secs = 0 + s(":00") ...

Validation data not being transmitted in AJAX request

My PHP code is almost working perfectly, but I'm facing an issue with receiving the output of the validate function through AJAX. Can someone help me troubleshoot this problem? $(document).ready(function(){ $("#d").click(function(){ valid ...

Notification of Exceeding File Size Limit

Seeking assistance with implementing a 'filesize' error message into a script used for uploading BLOB files to a mySQL server. if (!mysql_connect($db_host, $db_user, $db_pwd)) die("Can't connect to database"); if (!mysql_select_db($dat ...

Using the input method in JavaScript cannot extract an object property

Recently, I have been working with this JavaScript code provided below. It is essential for me to retrieve the votecount for a game based on user input. function Game(gamename,votes) { this.gamename = gamename; this.votes = votes; }; var lol = ne ...

Tips for adjusting the size of a background image using zoom

Currently, I am in the process of creating my first website. At this stage, it consists of a background image, an image, and a non-functional password input section. Everything seems to be going well so far. The background image fits perfectly on the scree ...

The initial result from reactQuery may start off as undefined, but eventually it will provide the data as

Currently, I am working on fetching data using reactQuery and storing the response in a variable when clicked. const { data: response, refetch } = useQuery({ queryKey: ['get-response'], queryFn: async() => { return await Axios ...

Adjust the clarity of the elements within my HTML document

I have been working on creating a login page that will appear in front of the main webpage. Through my online research, I discovered a technique to blur the main webpage background until the user logs in. Below is the code snippet: HTML: <div id="logi ...

The transparent overlay div does not activate when clicking on a button in Internet Explorer 10

While tidying up my CSS and JavaScript, I may have gone a bit overboard. The code seems to work fine on Chrome, Safari, and Firefox, but it's causing chaos in IE10. Check out the code here <div id="bgmbutton1"> <img id="button1" src ...

Having trouble with the form parsing not functioning properly

I have been utilizing Express.js along with the body-parser module for form parsing on the server. However, I am facing an issue where the content received appears as an empty object under res.body. This is how my app.js looks: var express = require("exp ...

Setting up an Angular CLI project

In our Angular 2 application, we utilize native CSS components that contain some JavaScript files. These components have wrappers to convert them into Angular 2 components. Now, I am looking to create a new Angular 4 project using Angular CLI and incorpora ...

The Jquery ajax request fails to function properly once the webpage has been published live

We've implemented a basic jQuery ajax call. A "more" button triggers a call to another PHP file when clicked. While it worked fine on my local server and initially on the live server, it suddenly stopped working live. The code functions perfectly o ...