The navigation menu in Javascript is flickering

I'm experiencing an issue with my navigation menu that has sublinks. Whenever I hover over a link with sublinks, it starts blinking as I move my mouse across the links.

Here is an illustration of the problem:

function myFunction(i) {
  document.getElementById("myDropdown" + i).classList.toggle("show");
}

/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
function yourFunction(i) {
  document.getElementById("myDropdown" + i).classList.toggle("show");

  document.getElementById("myDropdown" + i).children.classList.remove("show");
}
<li class="active"><a href="{{ route('comparateur') }}">{{__('header.Comparer')}}</a></li>
<div class="d_1">
  <button href="{{ route('vpn.index') }}" onmouseover="myFunction(1)" class="d_btn">{{__('header.Meilleurs-VPN')}} <i class="fa fa-angle-down"></i></button>
  <div id="myDropdown1" onmouseout="yourFunction(1)" class="d_content d_mobile_1">

    <a href="{{ route('vpn.index') }}">Tous les VPN</a>

    <a href="{{ route('vpn.windows') }}">Windows</a>
    <a href="{{ route('vpn.ios') }}">{{__('header.Mac')}}</a>

    <a href="{{ route('vpn.linux') }}">{{__('header.Linux')}}</a>
    <a href="{{ route('vpn.android') }}">{{__('header.Android')}}</a>
    <a href="{{ route('vpn.netflix') }}">{{__('header.Netflix')}}</a>
  </div>
</div>

The current setup works but the navigation feels clunky and unpleasant due to the submenu blinking between each link when moving the mouse within it.

If anyone could offer some guidance or assistance, I would greatly appreciate it! Thank you!

Answer №1

If I were to abandon javascript and opt for pure CSS instead, utilizing the :focus selector would allow me to target an element when clicked on, while x + y indicates "select y if it immediately follows x".

button:focus + div {
  display: flex;
}

button + div {
  display: none;
  flex-direction: column;
}

/* STYLING DETAILS BELOW THAT ARE NOT DIRECTLY RELATED TO THE PROBLEM */
button + div {
  padding: 0.5rem 0px;
  background: pink;
}

button + div > a {
  padding: 0.5rem;
}

button + div > a:hover {
  background: rgba(0, 0, 0, 0.13);
}
<div class="d_1">
  <button href="{{ route('vpn.index') }}" class="d_btn">{{__('header.Meilleurs-VPN')}} <i class="fa fa-angle-down"></i></button>
  <div id="myDropdown1" class="d_content d_mobile_1">

    <a href="{{ route('vpn.index') }}">Tous les VPN</a>

    <a href="{{ route('vpn.windows') }}">Windows</a>
  </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

What is the best way to display an alert when the button is clicked repeatedly?

Is it possible to keep displaying the alert every time we click the button, rather than just once after clicking it? I currently have an alert set to trigger when a button is clicked, but it disappears after 3 seconds. How can I make it show up again with ...

Issue with appending SVG elements in HTML

In my application, I am utilizing d3js charts as widgets. When a widget is added, a function is triggered to create and draw the widget (svg element) in a specific div. The issue arises when the same widget is added twice, causing the d3js chart (svg elem ...

function for clicking on mobile navigation bars

I could use a bit of assistance with understanding this JavaScript function. I am trying to create an onclick function for the mobile navigation "li" that will cause it to slide up. Can someone help me figure this out? Thank you. <!-- /#js --> < ...

Mastering the Kendo Grid: Managing data sources for updates, creations, and deletions

Due to limitations with the MVC-wrapper of Kendo grid, I am opting to construct the Kendo grid using JavaScript instead. There are a couple of key issues when attempting to update or create records on the grid. 1-) All operations (destroy, update, create ...

The calculator program fails to compute the width

My table has 4 columns with different widths: column 1: fixed width of 500px; column 2: calc(33% - 165px); column 3: calc(33% - 165px); column 4: calc(33% - 165px); Despite having the same value for the last 3 columns, their actual widths are different. ...

Store image selection in state

I am currently working on building an imagePicker in ReactNative, but I am running into an issue when trying to select the image. The error message I am receiving is: TypeError: this.setState is not a function. (In 'this.setState ({ avatar: data}) &a ...

Is there a way to export ng-grid data to a PDF file using jsPDF or a

Hey there, I'm currently working with an AngularJS ng-grid and I'm facing a challenge. I've been looking for a way to use jsPDF to render it as a PDF. While I've had success rendering PDFs of simple text and complicated SVGs through th ...

clearing the value of an option in a select dropdown that is constantly updating

Is there a way to customize this code snippet: https://jsfiddle.net/nn83qf3y/ to display the '--None--' selections as <option value="">--None--</option> instead of <option value="--None--">--None--</option> Appreciat ...

Enhancing Web Interactivity: Jquery AJAX and PHP

Hey there, I've posted a similar question before but wanted to provide more details this time. I am currently attempting to execute a Jquery Ajax Post call to a PHP file in order to store an email address in a table. Following a successful ajax call, ...

Creating a JavaScript object and retrieving the values of numerous input fields with identical classes

I have encountered an issue that I need assistance with: <input title="1" type="text" class="email"> <input title="2" type="text" class="email"> <input title="3" type="text" class="email"> The HTML code above shows my attempt to extract ...

Using Laravel to set cookies with Ajax

I am facing difficulties in setting cookies through laravel using ajax. Despite reading several questions and posts, I have not been able to find a solution. My issue involves a dropdown that triggers a javascript function to send its value to a controlle ...

Tips for integrating error management in Angular with RXJS

Implementing error handling in the use case method by using subscriptions is my goal. When an error occurs in the adapter, the handling should be done in the use case. However, in the code snippet provided below, the catch block does not seem to work pro ...

Ways to add values to a database through modal window

There are two buttons on the interface: login and register. Clicking the login button opens the login modal, while clicking the register button should open the register modal. My objective is to validate the form and insert the values into a database aft ...

Which method is better for presenting data: PHP or JavaScript?

Currently, I am diving into vue.js on laracasts.com where Jeffrey Way demonstrates 2 ways to showcase data on a webpage. One method involves displaying data using Laravel foreach loops, while the other utilizes vue.js. This has led me to ponder: is there ...

How can I embed certain returned values into JavaScript for display on a webpage using PHP's PDO?

I recently started learning about MVC and I'm working on retrieving an array of image paths from a MySQL database to create a grid using JavaScript. The controller needs a model that can fetch the paths from the database. Before, I had an ajax call ...

Retrieve the background image by calling $('#id').attr('src')

How can I access the background image of a div using jQuery? I attempted: var img = $('#id').attr('src'); however, alert(img) displays as null / undefined. I also experimented with document.getElementById('elementId').src ...

"Upon clicking, toggle the addition or removal of the overflow hidden

I'm currently working on a function to add and remove the CSS property .css('overflow-y','hidden') using an onclick event, which I have successfully implemented. However, I am encountering an issue when attempting to remove this CS ...

Every time Tailwind compiles, it seems to generate a glitchy class

There was a time when I accidentally wrote 2 classes without a space max-w-[412px]mb-[36px]. This caused an error, so I added a space between the classes. However, the error persisted even after making this correction. In a last attempt to resolve the issu ...

Using Python with Selenium to extract information through hover actions

driver.get(link) time.sleep(2) lol = driver.find_element_by_xpath("//*[@id='table_div']/div/div[1]/table/tbody/tr[2]/td[10]/div/div") hover = ActionChains(driver).move_to_element(lol) hover.perform() I have a code snippet here where I am attempt ...

Error: The function scrollIntoView is invalid and cannot be found

I'm currently new to working with react-testing-library / jest and trying to create a test that checks if the route navigation (using react-router-dom) is functioning correctly. I've been following the guidance from the README as well as this hel ...