Angular Navbar Hover DropDown Expansion

I have been struggling to make my bootstrap 4 navbar in my angular code base expand into a dropdown menu when I hover over the menu items. Despite trying various options all night, I haven't been able to find a solution that works.

Below is the HTML, TS, and CSS code for your reference.

HTML:

<nav class = "navbar navbar-expand-md navbar-fixed-top nav-border" role="navigation">
    <ul class="navbar-nav mx-auto" *ngFor="let menu of menus">
        <li class="nav-item dropdown">
            <a class="nav-link" data-toggle="dropdown" data-target="{{menu.heading}}">{{menu.heading}}</a>
            <div class="dropdown-menu" aria-labelledby="{{menu.heading}}">
                <a *ngFor="let option of menu.options" href="{{option.link}}" target="_blank" class="dropdown-item">{{option.name}}</a>
            </div>
        </li>
    </ul>
</nav>

TS:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-top-menu',
  templateUrl: './top-menu.component.html',
  styleUrls: ['./top-menu.component.css']
})
export class TopMenuComponent implements OnInit {

  menus : any = [
    {
      
        heading: "MENU1",
        options: 
        [
          {name: "GOOGLE",link: "https://google.com/", order: 1},
          {name: "GitHub",link: "https://github.com", order: 2},
        ]
      
    }
  ];
  
  isDropdownOpen: boolean = false;
  constructor() { }

  ngOnInit(): void {
  }

}

CSS:

.navbar{
    padding:0px;
}
.nav-border {
    border-bottom:1px solid #ccc;
}
li { cursor: pointer; }

li:hover{
    background-color: black;
}

a {
    color: black;
}

.navbar-nav {
    padding: .9rem 0 0;
}

.nav-link:hover {color:white;}

.widthd{
    width: 100%;
}

Snapshot of the menu: https://i.sstatic.net/MB8mZ.png

Answer №1

For those utilizing bootstrap 4, consider implementing mouseover and mouseleave events on nav links along with ngclass on dropdown menus, as demonstrated below:

<a class="nav-link" data-toggle="dropdown"  (mouseover)="isDropdownOpen = true" (mouseleave)="isDropdownOpen = false"  data-target="{{menu.heading}}">{{menu.heading}}</a>
            <div class="dropdown-menu"  [ngClass]="{show: isDropdownOpen}"  aria-labelledby="{{menu.heading}}">
                <a *ngFor="let option of menu.options"  href="{{option.link}}" target="_blank" class="dropdown-item">{{option.name}}</a>
            </div>

If you are using bootstrap 3, utilize the open class instead of show class, and modify the nav link as shown below:

<a class="nav-link" data-toggle="dropdown" data-target="{{menu.heading}}"  (mouseover)="isDropdownOpen = true" (mouseleave)="isDropdownOpen = false"  [ngClass]="{open: isDropdownOpen}">{{menu.heading}}</a>
            <div class="dropdown-menu" aria-labelledby="{{menu.heading}}">
                <a *ngFor="let option of menu.options" href="{{option.link}}" target="_blank" class="dropdown-item">{{option.name}}</a>
            </div>  

isDropdownOpen must be a boolean property in your TypeScript file

Answer №2

Just simply paste this block of code.

<div class="container">
  <header>
    <nav class="navbar navbar-expand-md">
      <ul class="navbar-nav ml-auto">
        <li class="nav-item" *ngFor="let item of items">{{item}}
          <ul class="submenu">
            <li *ngFor="let subItem of item.subItems">{{subItem}}</li>
          </ul>
        </li>
      </ul>
    </nav>
  </header>
</div>
.navbar {
  padding: 0px 70px 0 70px;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.5);
  height: 50px;
  background: #fff !important;
}

.navbar-nav .nav-item {
  line-height: 50px;
  margin: 0px 15px;
  font-size: 14px;
  cursor: pointer;
}

.navbar-nav .nav-item a {
  text-decoration: none;
  color: #000;
}

.navbar-nav .nav-item .submenu {
  visibility: hidden;
  opacity: 0;
  list-style: none;
  width: 200px;
  position: absolute;
  background: #fff;
  z-index: 99;
  border-top: 2px solid #000080;
  transition: all 0.3s ease;
  transform: translateY(25px);
  box-shadow: 0 8px 24px -5px #ccc;
}

.navbar-nav .nav-item:hover .submenu {
  visibility: visible;
  opacity: 1;
  transform: translateY(0);
}

.navbar-nav .nav-item .submenu li {
  line-height: 35px;
  font-size: 14px;
  padding: 0 15px;
}

.navbar-nav .nav-item .submenu li:focus {
  outline: none;
  border: none;
  background: #ddd;
}

.navbar-nav .nav-item .submenu li a {
  text-decoration: none;
  font-size: 14px;
  color: #000;
}

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

The issue of CSS style and inline style not functioning properly in Outlook 2007

I created an HTML newsletter page that I need to send to a specific email address. However, when I view the email in Outlook, my styled HTML page does not display correctly. The CSS and inline styles work perfectly in the browser, so why do they not work ...

What is the best way to bring in css-mediaquery into a Typescript project?

Currently, I am attempting to import css-mediaquery in Typescript using the following syntax: import { match } from 'css-mediaquery' Unfortunately, I encountered an error message: Cannot locate module 'css-mediaquery' from '../../ ...

Error: setPosition function only accepts values of type LatLng or LatLngLiteral. The property 'lat' must be a numerical value in agm-core agm-overlay

Currently, I am utilizing Angular Maps powered by Google @agm-core and agm-overlay to implement custom markers. However, when using the (boundsChange) function on the agm-map component, an error occurs with the message "invalidValueError: setPosition: not ...

Extension Overlay for Chrome

I'm currently working on developing a Chrome extension, but I'm encountering some confusion along the way. Despite researching online, I keep receiving conflicting advice and haven't been able to successfully implement any solutions. That&ap ...

Adding a fresh element to an object array in TypeScript

How can we add a specific value to an array of objects using TypeScript? I am looking to insert the value 1993 into each "annualRentCurrent" property in the sample object. Any suggestions on how to achieve this in TypeScript or Angular? Thank you! #Data ...

"Implementing responsive design with Bootstrap's carousel through media queries

I'm having some trouble creating a basic carousel using bootstrap because the images are not displaying full screen. Do I need to use media queries to achieve this and if so, how can I implement them? <div id="myCarousel" class="carousel slide" da ...

Receiving real-time updates on all devices from the server

Utilizing Angular 2, I have set up a system to send requests to my Laravel PHP API and display the results on my client. To continuously receive updates from the server and refresh my data, I have implemented the following code snippet which calls the API ...

Ways to match a string against a numeric value

I have a span id with textContent which have their own time(hour/minutes) <div class="here"> <span class="time" >8 min</span> </div> <div class="here"> <span class="time" >22 min&l ...

Please input two words in the textbox using AngularJS

I am currently facing a challenge that requires me to make the user input both their first and last name into a single text box. I am using AngularJS for this task, and my goal is to validate the text field using ng-pattern. The requirement is that the fie ...

Troubleshooting problem of opacity rendering with gsap animation on SVG files

Having an issue with this website: This site is primarily built using SVG and animated with the GSAP library. Unfortunately, I'm experiencing some rendering issues on Chrome, including the latest version. You can see an example image here: As seen ...

Enhance the CSS Transition for a Seamless Bootstrap Toggle Menu Experience

I attempted to modify the Bootstrap Toggle Menu slide effect from top to bottom to left to right. You can view the website here: After experimenting with various CSS codes (I prefer using pure CSS for this effect), I finally discovered this one: .collaps ...

It seems that every time you save data in Angular, the local storage array gets overwritten

While using Angular, I encountered an issue with saving to local storage. The code works fine for saving items initially, but on refreshing the page and trying to add more objects to the local storage array, it overwrites instead of appending. Can you help ...

Ways to soften a section of a canvas component?

What is the simplest way to apply a blur effect to a specific portion of my canvas element? I am utilizing the JavaScript 3D Library known as three.js. This library simplifies the use of WebGL. While they offer examples of blur and depth of field effects, ...

The drop down feature in Bootstrap is experiencing issues when used online, however, it functions perfectly when tested in VS Code

After migrating my website to a new hosting service, Web Host Canada, from the previous host, Hostpapa.ca, I encountered an issue where the dropdown menus are not functioning properly. The website in question is www.FeaterX.com. Interestingly, when I view ...

Confirming class Value with Selenium IDE

I am looking to confirm the value of a specific class within an HTML code using Selenium IDE. Specifically, I want to retrieve the value of: data-val located under the class named tgl. In my example, this value can be either 0 or 1. How can I achieve thi ...

Combine a fully operational webpage (html, css, js) within a React framework

Currently, I have a standalone HTML, CSS, and JS page that functions independently. The HTML file loads JS and CSS from CDNs as well as local files. Now, I am looking to integrate this standalone page into a Next.js/React application. At the moment, I hav ...

To utilize RxJS 6+, the 'Observable' type needs to include a 'Symbol.iterator' function that generates an iterator

I encountered an issue when attempting to filter an array of values from an observable: The error message I received was: 'Type 'Observable' must have a 'Symbol.iterator' method that returns an iterator' Here is the code s ...

Is TypeScript the new replacement for Angular?

After completing a comprehensive tutorial on Angular 1.x, I decided to explore Angular 2 on angular.io. However, upon browsing the site, I noticed references to Typescript, Javascript, and Dart. This left me wondering if my knowledge of Angular 1.x is now ...

A guide on activating the Bootstrap v5 Offcanvas with toggle buttons

How can I trigger the Bootstrap V5 Offcanvas using Bootstrap switches? I want the Offcanvas to open when the switch is checked and close when the Offcanvas is closed. Sample Code : <!-- Using checkbox switches to open the offcanvas --> <div class ...

Which specific part of this jQuery code is causing issues with compatibility on IE6 and IE7?

This JavaScript function duplicates an element within a form. It performs well across most browsers but encounters issues with Internet Explorer versions 6 and 7. The problem arises from the fact that it fails to increment the name attribute of the duplica ...