Hide the navigation menu when a page link is selected

Within my Angular 8 application, a fixed navigation bar is positioned at the top of the screen. Upon hovering over a dropdown nav link, a menu will appear for the user to explore.

However, when a user clicks on one of these menu links, Angular Routing is used to open up the corresponding page:

<div class="nav-left">
  <div class="dropdown">
    <a class="menu" routerLink="/home">
      {{'HOME.TITLE' | translate}}
      <span class="icon"><i class="fa fa-fw fa-angle-down"></i></span>
    </a>
    <div class="dropdown-content">
      <a routerLink="/home/welcome">
        <span class="icon"><i class="fa fa-fw fa-home"></i></span>
        {{'HOME.TITLE' | translate}}
      </a>
      <a routerLink="/home/news-blog">
        <span class="icon"><i class="fa fa-fw fa-newspaper-o"></i></span>
        {{'HOME.NEWS_BLOG' | translate}}
      </a>
      <a routerLink="/home/features">
        <span class="icon"><i class="fa fa-fw fa-cubes"></i></span>
        {{'HOME.FEATURES' | translate}}
      </a>
    </div>
  </div>
</div>

In my SCSS file, there's a .dropdown:hover selector that displays the menu only upon hover:

.dropdown {
  .dropdown-content {
    display: none;
  }

  &:hover {
    .dropdown-content {
      display: block;
    }
  }
}

The objective is for the navigation menu to close automatically once a menu link has been clicked. Currently, the menu stays open as Angular Router doesn't reload the page but only updates the contents with the selected subpage.

I believe there should be a CSS / SCSS solution to this issue or potentially some TypeScript code could help. My initial thoughts include:

  • Implementing a "click" or "selected" CSS selector to set display: none after a link has been clicked
  • Activating an Angular function (if available) to reset the "CSS state" post-click event
  • Developing a method to close all navigation menus when a routerLink has been clicked (although it may be complex)

Are there any recommendations on which approach would be most suitable and how best to achieve it?

Answer №1

To create an interactive dropdown menu in your template, consider adding a listener like this:

<div class="dropdown" [ngClass]="{'visible': menuVisible}" (mouseover)="toggleMenu()" (mouseout)="toggleMenu()">

In your TypeScript file:

menuVisible = false;
toggleMenu() {
  this.menuVisible = !this.menuVisible;
}

This is a straightforward approach. Alternatively, you could use Observables by having a Subject emit for each mouse event.

UPDATE: I forgot to mention the click listener part! But you probably understood the concept!

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

Modify only the visual appearance of a specific element that incorporates the imported style?

In one of my defined less files, I have the following code: //style.less .ant-modal-close-x { visibility: collapse; } Within a component class, I am using this less file like this: //testclass.tsx import './style.less'; class ReactComp{ re ...

What are the dimensions for maximum picture width and height in the Bootstrap 4 grid class col-4? Additionally, can you provide some tips on minimizing image bl

Hey there! I am trying to arrange 3 screenshots in 3 separate columns in one row, resembling steps 1, 2, and 3. I want them all to have the same size. Can you advise on the maximum width and height I should use before they start getting stretched or comp ...

When utilizing webpack in Angular 5, the :host selector does not get converted into a component entity

Initially, I set up Angular with webpack following the configuration in this guide, including configuring webpack sass-loader. Everything was working smoothly until I encountered an issue today: app.component.ts @Component({ selector: 'ng-app&ap ...

Generating a dynamic clickable div using Angular 6

How can I create a user-friendly interface that displays an image with clickable divs around detected faces, allowing users to view specific attributes for each face? I'm looking for a solution where I can have divs or buttons around each face that t ...

aligning an image in the center of a webpage

I have created a simple HTML code to center an image inside the body of an HTML page. It works perfectly on Windows with various browsers, and on phones vertically when rotated. However, the issue arises when I open the site on a Mac as the centering doe ...

Achieving text center alignment and adding color to text within a header using Bootstrap

I am looking to center the text in a header and apply color to it using Bootstrap. Unfortunately, there is no direct option for font-color or text-color in Bootstrap. Below is my HTML and CSS code: #header { width: 800px; height: 50px; color :whi ...

Testing an Angular Service method that involves a window.location.replace operation

I'm currently working on an Angular 4 application and I am facing a challenge with testing a method of a Service. This method calls a private method within the same service that includes the following line of code: window.location.replace(url); Howe ...

Issue with transition duration not affecting bootstrap button functionality

I am attempting to incorporate a transition effect on specific bootstrap buttons. The issue lies with the height and width properties. They change instantly without taking into account the transition duration property. Keep in mind that all other propert ...

Picture that perfectly matches the height of the window

I am looking to create a website design similar to this where an image fills the window until you scroll down. I am not sure how to accomplish this. Would using jQuery be the best option? I haven't been able to find much information on this. While my ...

Tips for creating CSS styles for a selected input field

I seem to be stuck in a situation where my screen looks similar to the screenshot provided. There are four input elements that I would like to have bordered just like in the image, complete with a circled tick mark. I've managed to create these four i ...

Exploring the functionality of LazyLoading and configuring Angular's NgModule

I am currently working on a module called 'DevicePreview' that contains various routes and can be accessed both from the root and from another module called 'Content'. When accessed from the root, DevicePreview displays routes /a, /b, ...

How to center align a <p> element along with a <select> element

Having some trouble while redesigning a simple survey page. I want to center-align the p tag with the select tag without using display:flex inside the .combobox class. The goal is to have them on the same line and still be centered. The current layout loo ...

The program encountered an error: Unable to locate the module 'chart.js' in the directory '/node_modules/ng2-charts/fesm2015'

Having trouble with this error message: ERROR in ./node_modules/ng2-charts/fesm2015/ng2-charts.js Module not found: Error: Can't resolve 'chart.js' in /node_modules/ng2-charts/fesm2015' I ran the command "npm install --save ng2-chart ...

Creating a dynamic row in an HTML table with elements inside

I have an array of numbers in my angular application that I need to display in an HTML table without truncating them and ending with a comma. Each row should display a maximum of 20 values. How can this be achieved? The array looks like this - let arr ...

The see-through background causes my text to blend in seamlessly as well

While creating a text box with a black transparent background, I encountered an issue where the text also became transparent. Can someone please point out where I went wrong? style.css #headertxt { position: absolute; right: 20px; bottom: 100px; backgr ...

Issue with a "hover" effect on a specific input[type="submit"] button

I'm having trouble getting my CSS styles to apply correctly to the input field. Can anyone help me understand why this is happening? If you'd like to take a look at the code, here are the links to the .html file and the .CSS styles. The goal is ...

Angular auto-suggestion using Observable

I'm encountering an issue with Angular's autocomplete feature, where I'm having trouble displaying names instead of IDs. You can see the problem in the screenshot here: https://i.sstatic.net/uqk79.png The data is retrieved as an Observable ...

What is the best way to reorganize an object's properties?

Looking for a way to rearrange the properties of an existing object? Here's an example: user = { 'a': 0, 'b': 1, 'c': 3, 'd': 4 } In this case, we want to rearrange it to look like this: user = { &a ...

Encountering challenges while integrating Angular with a Laravel forum creation project

Currently, I am working on building a forum application that involves users, posts, and comments using Laravel. However, the next step in my project requires integrating Angular, which is new territory for me and I'm not sure where to start. I have a ...

Dynamic JavaScript tool

Does anyone know which library is being used on the website linked here? I am working on a project similar to this and would appreciate if anyone can identify this library for me. Thank you in advance. ...