Dropbox menu within an extended webpage

I am looking to create a dropdown menu that behaves like the one on this website. The goal is for the dropdown to cover the entire webpage, hide the scroll bar, and "unmount" the other elements of the page, while still displaying them during the transition. How can I achieve this effect? Is there a way to remove elements from view without actually removing them from the DOM temporarily? Thank you! Please refer to the code snippet below for a similar setup that I currently have in place.

class Comp extends React.Component {
  constructor() {
    super();
    this.state = {
      clicked: false,
      divId: "off"
    }
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick() {
    this.setState({clicked: !this.state.clicked});
    if (this.state.divId == "on") {
      this.setState({divId: "off"});
    } else {
      this.setState({divId: "on"});  
    }
  }

  render() {
    return (
      <div>
        <div id={this.state.divId}></div>
        <button onClick={this.handleClick}>Click Me</button>
        <div id="div1"></div>
        <div id="div1"></div>
        <div id="div1"></div>
        <div id="div1"></div>
        <div id="div1"></div>
      </div>
    );
  }
}

ReactDOM.render(<Comp />, app);
#div1 {
  background-color: red;
  height: 100px;
  width: 100px;
  margin-bottom: 15px;
}

button {
  position: absolute;
  z-index: 2;
}

#on {
  background-color: green;
  position: absolute;
  transition: height ease 0.5s;
  z-index: 1;
  width: 100%;
  height: 100%;

}

#off {
  background-color: green;
  position: absolute;
  z-index: 1;
  transition: height ease 0.5s;
  width: 100%;
  height: 0%;

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="app"></div>

Answer №1

If you want to toggle the scrollbar visibility, refer to the comments within the code.

class ToggleScrollbar extends React.Component {
  constructor() {
    super();
    this.state = {
      clicked: false,
      divId: "off"
    }
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick() {
    this.setState({clicked: !this.state.clicked});
    if (this.state.divId == "on") {
      this.setState({divId: "off"});
      document.body.classList.remove("scrollbar-hide");//Remove CSS class
    } else {
      this.setState({divId: "on");  
      document.body.classList.add("scrollbar-hide"); //Add CSS class
    }
  }

  render() {
    return (
      <div>
        <div id={this.state.divId}></div>
        <button onClick={this.handleClick}>Click Me</button>
        <div id="div1"></div>
        <div id="div2"></div>
        <div id="div3"></div>
        <div id="div4"></div>
        <div id="div5"></div>
      </div>
    );
  }
}

ReactDOM.render(<ToggleScrollbar />, app);
#div1 {
  background-color: red;
  height: 100px;
  width: 100px;
  margin-bottom: 15px;
}

button {
  position: absolute;
  z-index: 2;
}

#on {
  background-color: green;
  position: absolute;
  transition: height ease 0.5s;
  z-index: 1;
  width: 100%;
  height: 100%;

}

#off {
  background-color: green;
  position: absolute;
  z-index: 1;
  transition: height ease 0.5s;
  width: 100%;
  height: 0%;

}

/* CSS class to remove scrollbar. */
.scrollbar-hide {
  overflow: hidden;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="app"></div>

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

Tips for creating Material UI elements using React and Typescript

I am looking to extend a component from the Material UI library by creating a custom component that encapsulates specific styles, such as for a modal wrapper. However, I feel that my current solution involving the props interface may not be ideal. Is the ...

Tips for reducing noise on your website during the deployment process

Currently in the process of creating a website and incorporating fonts from Google Webfonts. While these fonts appear sleek and flawless on Google's own site, once implemented onto my own site they seem quite noisy. Any suggestions for how to prevent ...

JQuery may be successfully loaded, but it is not functioning as intended

Just started dabbling in JQuery (I'm a newbie) but I'm having trouble getting it to work! Initially, it worked a couple of times but then suddenly stopped working (pretty strange). I made some changes and now it doesn't work at all. JQuery a ...

Steps for deleting a game objectWould you like to learn how to

What is the process of creating an object that can eliminate itself automatically? var item = []; function selfDestruct() { this.destroy = () => { this = false; } } function initialization() { item[0] = new SelfDestruct(); item[0].destroy( ...

Tips for aligning inline elements in the center of a webpage

My goal is to center the title in the middle of the page, taking into account its alignment with the search bar. However, I want it to be centered independently and not affected by the search bar's position. Is there a way to achieve this? To better ...

The theme color in mui is not being implemented as expected

I have been working on a new component and trying to style it using the makeStyles() function. // Welcome.js import { makeStyles } from "@mui/styles"; const useStyles = makeStyles((theme) => { return { root: { width: "100%", maxWidth ...

Identify the moment when the SPAN element reappears in sight

I have a question that may have been asked before, but I haven't found a satisfactory answer yet. Within my HTML code, I have a SPAN element with an onclick event, like so: <span onclick='loadDiv()'>Text</span> When a user cli ...

I am attempting to assign a default value to a TextField by retrieving data from a GetMapping call in React, however, the value is not being successfully set

I am facing an issue with setting a default value for a TextField in my code. Even though I am trying to populate it with data from a GetMapping call, the value is not being set as expected. Here is the JSON response I receive from the API call: { "id": 1 ...

Ways to avoid the cascading of CSS styles onto multiple Three.JS scenes

It seems like I might have to take the longer route to solve this issue, but let's give it a shot... I'm facing a challenge when applying CSS to control the opacity of a specific HTML element that acts as a container for a Three.JS scene. In thi ...

The content section sits discreetly behind the sidebar

Upon loading my Bootstrap 5 webpage, the toggle button successfully moves the navbar and body section to show or hide the full sidebar. However, an issue arises where the body section goes behind the sidebar when using the toggle button. Below is a portio ...

Caution with React Material-ui Server-Side Rendering: The 'd' property did not align. Server side rendering displayed as "M 0 0 h 24 v 24 H 0 Z" while client side rendering showed as "M0 0h24v24H0z". Double

I've been developing a React website with server-side rendering and Material-ui, and everything was running smoothly, including the mui JSS styles. However, when I added an SVG icon from @material-ui/icons, Edge and IE11 started showing warnings: Wa ...

What is the best way to present retrieved JSON data from a database in Node.js without using the JSON format?

Here is the code snippet: var http=require("http"); var fs = require("fs"); var express = require("express"); var app = express(); var path = require("path"); var mysql = require('mysql'); var ejs = require("ejs") var bodyParser = require(&apos ...

Is it feasible for nested div to remain unaffected by bleeding when the container is positioned relatively and has an overflow hidden

Is it possible to bleed a nested div even when the container is positioned relative with hidden overflow? In this case, giving the nested div a fixed position is not an option. Please check out this unique example: http://jsfiddle.net/s7nhw/11/. If you ...

I am a newcomer to Stack Overflow and I am facing difficulty in getting my form to submit successfully

Excuse any errors as I am new to this. I am facing an issue where my form is not submitting anything in the console. Could it be because my entire HTML file is within a return statement from a JavaScript function? I assumed it would work since I imported a ...

Obtaining JSON data from a PHP script using AngularJS

I've been exploring an AngularJS tutorial on a popular website called w3schools. Check out the tutorial on w3schools After following the tutorial, I modified the script to work with one of my PHP scripts: <!DOCTYPE html> <html > <sty ...

Dealing with API fetch errors using dynamic URLs in Axios

When attempting to retrieve data from an API with a URL containing a user-input string, the application crashes if the entered string is invalid. This crash results in the error message "TypeError: Cannot read properties of null (reading 'map')". ...

What is the best method for locating the innermost element of an HTML tree that has a specific class assigned

I am working on a project that involves a menu system. One of the features I am implementing is that when a tree within the menu is opened, it receives the "active" class. My goal now is to dynamically retrieve the deepest sub-menu within this structure ...

When the user clicks, position a div directly below another div

Can anyone help me figure out how to center a div underneath another one when a specific div is clicked using programming? I've tried multiple solutions but nothing seems to work. Below is the code snippet that I have written. You can also check out t ...

Expand horizontally using React

Struggling with React horizontal expansion. I can't seem to get my Div container to smoothly go from 500px to 900px or full width on button click. It currently only appears and disappears. What am I missing? I'm using React hooks. Thanks for any ...

Issue with JQuery form submission causing controller method not to be executed

I am facing an issue with two click methods in my JavaScript code. Method 1 is successfully hitting the controller method, but when I try to trigger Method 2 by clicking on a button, it does not seem to call the controller. I have tried various solutions ...