CSS: Hover below the designated target to reveal an expanding dropdown menu

My initial solution involved toggling the visibility on and off when hovering. However, this approach is not optimal as the menu should smoothly transition into view. When the visibility is toggled, the user does not experience the intended transition effect when approaching the "hover element" from below.

Based on my testing, it appears that the issue is primarily related to CSS. Nevertheless, I have included the JavaScript code for reference. The visibility toggle function that was previously mentioned has been commented out.

let items = document.getElementsByClassName('items');
let menuBtn = document.getElementById('menuBtn');
let menuList = document.getElementById('menuList');
let menuClass = document.getElementsByClassName('menu');

menuBtn.addEventListener('mouseenter', func, false);
menuList.addEventListener('mouseleave',func2, false);


//visibilityToggle('hidden', 'none');

function func() {
    console.log('out');
    //visibilityToggle('visible', 'visible');
}

function func2() {

   //visibilityToggle('hidden', 'none');
   console.log('in');
}

function visibilityToggle(vis, point) {
    for (let i = 0; i < items.length; i++){
        items[i].style.visibility = vis;
        items[i].style.pointerEvents = point;
    }
}
.trigger {
  width: 200px;
  height: 53px;
  /*
    border: 1px solid black;
    background: green;
    */
}

.menu {
  width: 200px;
  pointer-events: visible;
}

.menu a {
  background-color: #eee;
  color: black;
  display: block;
  padding: 12px;
  text-decoration: none;
}

.menu a:hover {
  background-color: #ccc;
}

.menu a.active {
  background-color: #04AA6D;
  color: white;
}

.items {
  font-size: 50px;
  opacity: 0;
  position: relative;
  top: -20px;
  cursor: pointer;
  pointer-events: none;
  transition: opacity 400ms ease-in-out, transform 400ms ease-in-out;
}

.trigger:hover .items {
  opacity: 1;
  cursor: pointer;
  transform: translate(0px, 20px);
}

#menuBtn {
  border: 5px solid black;
}
<p>I am lost</p>
<div class="trigger">
  <div class="menu" id="menuList">
    <a href="#" id="menuBtn">Menu</a>
    <div class="items">
      <a href="#" class="items">One</a>
      <a href="#" class="items">Two</a>
      <a href="#" class="items">Three</a>
      <a href="#" class="items">Four</a>
    </div>
  </div>
</div>

Answer №1

It seems like the issue can be resolved without using JavaScript. Simply update the hover event to target #menuBtn and .items, then add pointer-events: initial to reset it. Also, consider changing the link class from items to item as you are applying styles to both the group of links and individual links.

.trigger {
  width: 200px;
  height: 53px;
}

.menu {
  width: 200px;
  pointer-events: visible;
}

.menu a {
  background-color: #eee;
  color: black;
  display: block;
  padding: 12px;
  text-decoration: none;
}

.menu a:hover {
  background-color: #ccc;
}

.menu a.active {
  background-color: #04aa6d;
  color: white;
}

.items {
  font-size: 50px;
  opacity: 0;
  position: relative;
  top: -20px;
  cursor: pointer;
  pointer-events: none;
  transition: opacity 400ms ease-in-out, transform 400ms ease-in-out;
}

#menuBtn:hover + .items,
.items:hover {
  opacity: 1;
  cursor: pointer;
  transform: translate(0px, 20px);
  pointer-events: initial;
}

#menuBtn {
  border: 5px solid black;
}
<div class="trigger">
  <div class="menu" id="menuList">
    <a href="#" id="menuBtn">Menu</a>
    <div class="items">
      <a href="#" class="item">One</a>
      <a href="#" class="item">Two</a>
      <a href="#" class="item">Three</a>
      <a href="#" class="item">Four</a>
    </div>
  </div>
</div>

Answer №2

There was an issue with your items' positioning - they were set to position:relative, which means they stayed in the same position as they are visible. To fix this, make them absolutely positioned by default and only change to relative when hovered over.

.item-div{
  position: absolute;
}
.trigger:hover .items {
  position:relative;
}

let items = document.getElementsByClassName('items');
let menuBtn = document.getElementById('menuBtn');
let menuList = document.getElementById('menuList');
let menuClass = document.getElementsByClassName('menu');

menuBtn.addEventListener('mouseenter', func, false);
menuList.addEventListener('mouseleave',func2, false);


//visibilityToggle('hidden', 'none');

function func() {
    console.log('out');
    //visibilityToggle('visible', 'visible');
}

function func2() {

   //visibilityToggle('hidden', 'none');
   console.log('in');
}

function visibilityToggle(vis, point) {
    for (let i = 0; i < items.length; i++){
        items[i].style.visibility = vis;
        items[i].style.pointerEvents = point;
    }
}
.trigger {
  width: 200px;
  height: 53px;
  /*
    border: 1px solid black;
    background: green;
    */
}

.menu {
  width: 200px;
  pointer-events: visible;
  position:relative;
}

.menu a {
  background-color: #eee;
  color: black;
  display: block;
  padding: 12px;
  text-decoration: none;
}

.menu a:hover {
  background-color: #ccc;
}
.item-div{
position: absolute;
width: 100%
}
.menu a.active {
  background-color: #04AA6D;
  color: white;
}

.items {
  font-size: 50px;
  opacity: 0;
  
  top: -20px;
  cursor: pointer;
  pointer-events: none;
  transition: opacity 400ms ease-in-out, transform 400ms ease-in-out;
}

.trigger:hover .items {
  opacity: 1;
  cursor: pointer;
  transform: translate(0px, 20px);
  position:relative;
}

#menuBtn {
  border: 5px solid black;
}
p:first-child {
  text-decoration: line-through;
}
<p>I am lost</p>
<p>Your are Alive</p>
<div class="trigger">
  <div class="menu" id="menuList">
    <a href="#" id="menuBtn">Menu</a>
    <div class="items item-div">
      <a href="#" class="items">One</a>
      <a href="#" class="items">Two</a>
      <a href="#" class="items">Three</a>
      <a href="#" class="items">Four</a>
    </div>
  </div>
</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

Is it possible to effortlessly associate a personalized string with an identifier within an HTML element utilizing Angular2?

Check out this cool plunker import {Component} from 'angular2/core' @Component({ selector: 'my-app', template: ` <div *ngFor="#option of myHashMap"> <input type="radio" name="myRadio" id="{{generateId(option[& ...

The term "Cardlist" has not been defined and is therefore causing an

I created a CardList and attempted to add cards into the list using map, but encountered an error import React from 'react'; import Card from './Card'; const CardsContainer = ({robots}) => { const cardComponents = robots.map((r ...

Styling the background of the endAdornment in a material-ui textfield using React

I tried following the instructions from this source: Unfortunately, the example code provided doesn't seem to be functioning properly now. Is there a way to achieve the same result without having that right margin so that it aligns better with the r ...

Enhance the dropdown menu by incorporating a caret icon

Click here for the image, I am trying to incorporate the Bootstrap caret class into my select dropdown menu. .select_menu{ font-size: 12px; outline: none; border: thin #ddd solid; -webkit-appearance: none; -moz-appearance: none; appearance: ...

Unable to open modal using a button in PHP

<?php $result = mysqli_query($con, $sql); while ($row = mysqli_fetch_array($result)) { echo '<tr><td>'; echo '<h5 style="margin-left:6px; color:black;">' . $row['id'] . '</h5> ...

Utilizing Vue Js and Query to retrieve data from a Jira Rest API endpoint

Trying to utilize the JIRA REST API within a Vue.js application has presented some challenges. After generating the API key, I successfully ran an API request using Postman. Initially, I attempted to use the axios client, but encountered issues with cross ...

Issue with overlapping positioning of child components in React

I've come across multiple inquiries addressing this issue, yet none of them have provided a solution that fits my problem. In my scenario, there's a Parent component containing three Child components. import React, { Component } from 'react ...

Guide to setting up npm openlpr in a Node.js environment

After attempting to install the npm node-openalpr package, I encountered an error. What steps can I take to resolve this issue? > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cba5a4afaee6a4bbaea5aaa7bbb98bfae5fae5fa">[e ...

Incorporate a hanging indent within the text enclosed by the <li> element

(revised multiple times to enhance clarity) Disclaimer: I did not create the HTML code. Below is the structure of the HTML (please note that the links and text following them are on the same line): <li> <strong>Heading of section</str ...

Creating a User Authentication System using Node.js and Express

I am currently working on developing an application with node.js and expressJs, even though I come from a PHP background. In PHP, we typically use the session to handle logins, so naturally, I thought that in the case of node.js it would be similar. After ...

A difference in the way content is displayed on Firefox compared to Chrome, Edge, and Safari

Recently, I encountered an issue with a tool I had developed for generating printable images for Cross-Stitch work. The tool was originally designed to work on Firefox but is now only functioning properly on that browser. The problem at hand is whether th ...

Arrange a div within a list element to create a grid-like structure

My current structure is as follows: <ul class="wrap-accordionblk"> <li class="accordionblk-item"> <div class="accordionblk-header"> <div class="row-fluid"> <div class="infoblk"> <label>SESSION ID ...

What could be causing my issue with the if-else condition not functioning properly?

Why does the code only work for adding and removing styles in Part (else), but not returning the class when clicked again? var navDropDown = document.querySelectorAll('.menu-item-has-children > a'); for (let i = 0; i < navDropDown.length; i ...

React Router Error: Hook call invalid. Remember to only use hooks inside the body of a function component

I am having an issue with my React app that uses react router. In Box.js, I am attempting to access the URL parameters but I am encountering the following error message: Invalid hook call. Hooks can only be called inside of the body of a function component ...

The styling of divIcons in React Leaflet Material UI is not applied as expected

When using divIcon in React Leaflet to render a custom Material UI marker with a background color prop, I noticed that the background style is not being applied correctly when the marker is displayed in Leaflet. Below you can find the code for the project ...

Retrieve a JSON object from a JSON array using a specific key

I am facing a situation where I have a json array containing multiple json objects. Let's take the example of a course object with the following structure: {"name": "Math", "unit": "3"} The json array looks something like this: [{"name": "Math", "u ...

What is the correct way to customize colors for specific components in Material-ui?

Struggling with theming in Material-UI, particularly when it comes to customizing element colors. Some elements default to 'theme.palette.main.dark' and I want to override this behavior. For example, the TextField and SpeedDial components automa ...

What is the method for retrieving a value from my Node.js module once it has been modified by an asynchronous function?

Apologies, but as a beginner developer, I'm struggling to see how this relates directly to the questions already mentioned. I have no understanding of ajax and I'm unsure how to adapt the solutions provided to my own situation. Currently, I&apos ...

Steps to installing npm on Ubuntu without root access:1. First, download

I have successfully installed node in a custom directory within my home folder called "local" following the instructions provided here: https://gist.github.com/isaacs/579814 Here is the output: Creating ./icu_config.gypi * Utilizing ICU in deps/icu-sma ...

How can one HTML form be used to request a unique identifier from the user, retrieve a record from the database, parse it into JSON format, and then populate an HTML page form using the

As someone who isn't an expert in any specific coding language, I have a knack for piecing things together to make them work. However, my current challenge is stretching my technical abilities a bit too far. Here's what I'm trying to achieve ...