Expand a div in navigation using JQuery

Check out this code snippet:

.container {
  width: 150px;
  height: 100px;
}

.test {
  color: white;
  
  background-color: red;
  width: 100%;
  height: 25%;
  transition: height ease 1s;
}

.test:hover {
  height: 100%;
}
<div class="container">
  <div class="test">Hover Here</div>
</div>

This code shows a simple div inside a container that expands to 100% when hovered over. I'm looking to do something similar in a navigation menu, like the one on .

My goal is to have the main div expand from 0% to 100% in height when a user hovers over the container div (not the main box itself).

I've tried using JQuery by adding a ".hovered" class, but haven't had any success. Does anyone know how to code this effect?

I would greatly appreciate any help. Thank you!

Answer №1

Check out this presentation:

Commonalities between the two code snippets:

  • The containers utilize flex display to create a responsive navbar container, with each item spanning a width of 20% (modifiable).
  • Each item (with relative positioning) contains two sub-containers (with absolute positioning), one for the blue transitioning background (z-index:1) and the other for the fixed text in the front (z-index:2).
  • The z-index ensures that the background transitions behind and the text remains fixed in front. When transitioning from bottom to top, we set the bottom:0 and height:0%; on the overlay class.
  • On hover, the height transitions from 0% to 100%.

Divergence between the code snippets:

  • In the first code snippet, individual items expand on hover using .items:hover .overlay.

  • Contrastingly, in the second snippet, all items expand together when the container is hovered, using

    .container:hover > *.items> .overlay
    (">" indicates a direct child selector).

First: Hovering over each individual item expands the overlay.

.container {
  width: 100%;
  display: flex;
  height: 80px;
  background: gray;
  justify-content: center;
  align-items: center;
}

.items {
  flex: 0 1 20%;
  height: 100%;
  margin-right: 5px;
  position: relative;
  overflow: hidden;
}

.overlay {
  position: absolute;
  background: blue;
  z-index: 1;
  width: 100%;
  height: 0%;
  bottom: 0;
  transition: all 0.5s ease-in-out;
}

.item-text {
  position: absolute;
  text-align: center;
  color: white;
  z-index: 2;
  width: 100%;
  height: 100%;
  top: 0;
}

.items:hover .overlay {
  height: 100%;
}
.container {
  width: 100%;
  display: flex;
  height: 80px;
  background: gray;
  justify-content: center;
  align-items: center;
}

.items {
  flex: 0 1 20%;
  height: 100%;
  margin-right: 5px;
  position: relative;
  overflow: hidden;
}

.overlay {
  position: absolute;
  background: blue;
  z-index: 1;
  width: 100%;
  height: 0%;
  bottom: 0;
  transition: all 0.5s ease-in-out;
}

.item-text {
  position: absolute;
  text-align: center;
  color: white;
  z-index: 2;
  width: 100%;
  height: 100%;
  top: 0;
}

.container:hover > *.items> .overlay {
  height: 100%;
}
<div class="container">
  <div class="items">
    <div class="overlay"></div>
    <div class="item-text">Home</div>
  </div>
  <div class="items">
    <div class="overlay"></div>
    <div class="item-text">About</div>
  </div>
  <div class="items">
    <div class="overlay"></div>
    <div class="item-text">Contact</div>
  </div>
  <div class="items">
    <div class="overlay"></div>
    <div class="item-text">Other</div>
  </div>
</div>

Answer №2

ul{
  list-style-type: none;
  margin-left: 0;
  padding-left: 0;
  display: flex;
}

ul li{
  border: 1px solid #d3d3d3;
  text-align: center;
  height: 100px;
  width: 100px;
  margin-right: 4px;
}

ul li a{
  color: #000000;
  text-decoration: none;
  position: relative;
  height: 100%;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  
}

ul li a:after{
  content: '';
  position: absolute;
  background: lightblue;
  left: 0;
  bottom: 0;
  height: 0%;
  width: 100%;
  z-index: -1;
  
}
ul li a:hover:after{
  animation: bounce 1s ease-in-out forwards;
}
@keyframes bounce {
  0% {height: 0%}
  20% { height: 100%}
  55% { height: 95%}
  100% {height: 100%}
}
<ul>
  <li><a href="#">Lorem, ipsum.</a></li>
  <li><a href="#">Saepe, asperiores!</a></li>
  <li><a href="#">Vitae, expedita?</a></li>
  <li><a href="#">Dicta, quo.</a></li>
  <li><a href="#">Sed, et.</a></li>
</ul>

Answer №3

Here is some code I created:

//my HTML code

<ul>
   <li><a href="">Products</a></li>
   <li><a href="">Services</a></li>
   <li><a href="">Support</a></li>
</ul>

//My custom CSS using Sass

ul {
  list-style:none;
  background:green;

  li {
    display:inline-block;
    padding:15px;
    position:relative;

    &:before {
      position: absolute;
      content: "";
      width: 100%;
      height: 0%;
      left: 0;
      bottom: 0;
      background:purple;
      transition: height ease-in-out 0.7s;
    } 

    a {
      z-index:2;
      position:relative;
      color:black;
    }

    &:hover {
       &:before {
          height: 100%;
       }
    }
  }
}

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

Can you please explain what shape-id denotes and provide guidance on locating it within a CSS file?

I have a question that may seem trivial to experienced web developers - what exactly is "shape-id"? For example, in the code <hr shape-id="2">. I'm currently attempting to change the color of the horizontal lines (hr) on my website, but I can& ...

What are the methods for choosing various boxes using the UP/DOWN/RIGHT/LEFT arrow keys?

This code snippet displays a 3x3 matrix where boxes can be hovered over and selected. The goal is to navigate around with keys and select boxes using ENTER. Can anyone provide guidance on how to achieve this functionality? <link rel="stylesheet" href ...

Passing the results of sequelize through express after the completion of a forEach iteration

In an express route, I am running a somewhat complex sequelize query that requires me to run a second query afterward to append necessary data to the result: //Get inboxes based on passed params router.get('/:data', function(req, res, next) { ...

Use jQuery's `on` method in conjunction with `load` to dynamically execute a function

Is there a way to automatically trigger a function every time a specific div with a particular class is added to an HTML page? Here's an example: If I dynamically add the following code: <div class="myform" id="myID"></div> I want the ...

When attempting to specify the path in the angular.json file, Angular encounters difficulty accessing Bootstrap from the node_modules directory

I have been attempting to integrate Bootstrap into my Angular project. Firstly, I used npm install --save bootstrap to add Bootstrap to my project. Following that, I installed jQuery as well. I then specified the path for Bootstrap. Displayed below is an ...

Configuring rows in React datagrid

I am working on a component where I receive data from the backend and attempt to populate a DataGrid with it. Below is the code for this component: export const CompaniesHouseContainer: React.FC<Props> = () => { const classes = useStyl ...

As soon as I inserted the image, the text vanished into thin air

The phrase "welcome" is not displaying after I included the av tag <!DOCTYPE html> <html> <style> @font-face { font-family: OpenSans; src: url(OpenSans-Bold.ttf); } * { ...

Vue.js not responding to "mousedown.left" event listener

I am trying to assign different functionalities to right and left click on my custom element. Within the original code, I have set up event listeners for mouse clicks in the element file: container.addEventListener("mousedown", startDrag); conta ...

Issues with Jquery Div sliding transitions from right to left are not functioning correctly

Creating page transitions in jQuery similar to "http://support.microsoft.com/." Encountering an issue where after the page transitions are completed, they start from the left instead of the expected right direction. Referencing this fiddle (Working Code) ...

The absence of multiple lines on the x-axis in the linear chart was noticeable

Currently, I am facing an issue with loading a single axis line chart on my Dashboard.vue. The functionality involves users selecting a 'year' and a 'loan_type' from dropdown menus, after which the chart should display a 12-month record ...

The image is being loaded onto the canvas and is being resized

Currently, I am facing an issue with loading images into a canvas element as the image seems to be scaled in some way. To provide some context, I have multiple canvases on my webpage and I want to load images into them. Since the dimensions of the canvas ...

Using type annotations is restricted to TypeScript files only, as indicated by error code ts(8010)

I encountered an issue with React.MouseEvent<HTMLElement> const handleOpenNavMenu = (event: React.MouseEvent<HTMLElement>) => { setAnchorElNav(event.currentTarget); }; const handleOpenUserMenu = (event: React.MouseEvent<HTMLElement ...

Troubleshooting Problem with the JQuery Cycle Plugin

I decided to give JQuery a try for the first time because I was having trouble with the AJAX Slideshow. Unfortunately, it's not working as expected - the images are just stacking on top of each other. My setup includes ASP.NET 2.0 and Visual Studio ...

Updating Angular model remotely without relying solely on the controller

I am struggling to call the addRectangleMethod method from my Javascript code in order to retrieve server-side information into the Angular datamodel. However, I keep encountering an error stating that the method I'm trying to call is undefined. It&ap ...

"What is the significance of the .default property in scss modules when used with typescript

When dealing with scss modules in a TypeScript environment, my modules are saved within a property named default. Button-styles.scss .button { background-color: black; } index.tsx import * as React from 'react'; import * as styles from ' ...

Angular causing alignment issues for items not centered

I'm having trouble centering the contents of a div with my code. The properties I am applying don't seem to work. Any idea why this is happening? <div class='center'> <form (ngSubmit)="loginUser()" style="background: steel ...

The CSS styling is not being rendered correctly on the AngularJS HTML page

Encountering a puzzling situation here. Our angular controller is successfully delivering data to the page, but we are facing an issue with rendering a table due to an unknown number of columns: <table border="1" ng-repeat="table in xc.tables"> ...

The function is missing from the object, leading to a script error with jQuery

When using different versions of jQuery, I encountered some issues with saving changes in my application. Initially, with jquery-1.4.4.min.js, everything worked except for the save function due to an error I made. However, when switching to jquery-1.7.1.mi ...

Show a table containing dynamic information, featuring 10 rows for each column. The header should be present for each additional column that is added

I have incoming dynamic data that I need to display in columns, with 10 records in each row. Currently, I am able to display 10 rows as expected. However, I want the header to repeat each time an additional column is added. How can I achieve this using the ...

The function generalChannel.send does not exist

I've recently started working on a discord bot and I'm facing an issue with getting it to greet everyone when it's turned on. Using the bot.channels.get method, I am able to locate the channel successfully, but when it comes to sending a mes ...