The events for tabs are not triggering properly

 

Hey guys, I'm encountering an issue while creating mega menu tabs. Here's what I'm doing: I have tabs that trigger a 'mouseenter' event in the background when hovered over. This event adds an 'active' CSS class to both the tabs and the tab panes. Everything seems to be working fine up until this point. However, on triggering the 'mouseleave' event on the tabs, users are unable to move their mouse towards the tab pane. I am looking for a way to hide the tab pane when the mouse position moves out of the boundary defined by the tabs and tab pane. Can someone verify if my events are set up correctly?

$(document).ready(function () {
        // Select all tabs nav li items.
        var navs = $('.megamenu-tabs').children('.megamenu-tabs-nav').children('li');
        // div panes array which is all tabs pane that will show on hover
        var panes = $('.megamenu-tabs').children('.megamenu-tabs-pane');
 
        navs.on('mouseenter', function (e) {
            e.stopPropagation();
            e.preventDefault();
            // remove active class from all tabs
            navs.removeClass('active');
            // add class active on current tab
            $(this).addClass('active');
            // remove active class from all tabs pane
            panes.hide(0).removeClass('active');
            // add class on current tab pane
            $(panes[$(this).index()]).show(0).addClass('active');
        });
 

        navs.on('mouseleave', function (e) {
            e.stopPropagation();
            e.preventDefault();
            // remove active class from all tabs
            navs.removeClass('active');
            // remove active class from all tabs pane
            panes.hide(0).removeClass('active');
        });

    });
 * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            background-image: linear-gradient(135deg, #FDEB71 10%, #F8D800 100%);
        }

        .container {
            width: 80%;
        }
 
        .megamenu-tabs {
            width: 100%;
            float: left;
            display: block;
            margin: 30px;

        }

        .megamenu-tabs-nav {
            width: 20%;
            margin: 0;
            padding: 0;
            float: left;
            list-style: none;
        }

...
</body>

Answer №1

To maintain the tabs open when navigating, you can eliminate the show(0) and hide(0) functions and incorporate a CSS code snippet for hover effect:

.megamenu-tabs-pane:hover {
display: block;
  opacity: 1;
}

Here is the complete modified code:

$(document).ready(function() {
  // Select all tabs nav li items.
  
// rest of the JavaScript code...
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  background-image: linear-gradient(135deg, #FDEB71 10%, #F8D800 100%);
}

.container {
  width: 80%;
}

.megamenu-tabs {
  width: 100%;
  float: left;
  display: block;
  margin: 30px;

// remaining CSS styles...
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<head>
</head>

<body>
  <div class="container">
    <div class="megamenu-tabs">
      <ul class="megamenu-tabs-nav">
        <li><a href="#">Clothing</a></li>
        <li><a href="#">Gadgets</a></li>
        <li><a href="#">Mobiles & Smart Phones</a></li>
// additional HTML content...

Answer №2

Include the following line "

var navs2 = $('.megamenu-tabs,.megamenu-tabs-pane');
" and update the event to mouseleave on navs2. This adjustment ensures that the tabs won't immediately disappear when you move your cursor away from them, but rather when you hover out of either
.megamenu-tabs, .megamenu-tabs-pane.

$(document).ready(function() {
  // Select all tabs nav li items.
  var navs = $('.megamenu-tabs').children('.megamenu-tabs-nav').children('li');
  var navs2 = $('.megamenu-tabs,.megamenu-tabs-pane');
  // div panes array which is all tabs pane that will show on hover
  var panes = $('.megamenu-tabs').children('.megamenu-tabs-pane');

  navs.on('mouseenter', function(e) {
    e.stopPropagation();
    e.preventDefault();
    // remove active class from all tabs
    navs.removeClass('active');
    // add class active on current tab
    $(this).addClass('active');
    // remove active class from all tabs pane
    panes.hide(0).removeClass('active');
    // add class on current tab pane
    $(panes[$(this).index()]).show(0).addClass('active');
  });


  navs2.on('mouseleave', function(e) {
    e.stopPropagation();
    e.preventDefault();
    // remove active class from all tabs
    navs.removeClass('active');
    // remove active class from all tabs pane
    panes.hide(0).removeClass('active');

  });

});
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  background-image: linear-gradient(135deg, #FDEB71 10%, #F8D800 100%);
}

.container {
  width: 80%;
}

.megamenu-tabs {
  width: 100%;
  float: left;
  display: block;
  margin: 30px;
}

.megamenu-tabs-nav {
  width: 20%;
  margin: 0;
  padding: 0;
  float: left;
  list-style: none;
}

.megamenu-tabs-nav>li>a {
  width: 100%;
  padding: 10px 16px;
  float: left;
  word-wrap: break-word;
  font-size: 13px;
  text-decoration: none;
  color: #666;
  border: 1px solid #f0f0f0;
  outline: 0;
  background-color: #fff;
}

.megamenu-tabs-nav>li.active a,
.megamenu-tabs-nav>li:hover a {
  background-color: #f3f3f3;
}

.megamenu-tabs-pane {
  width: 80%;
  min-height: 30px;
  padding: 20px;
  float: right;
  display: none;
  opacity: 0;
  font-size: 13px;
  color: #666;
  border: 1px solid #f0f0f0;
  background-color: #fff;
}

.megamenu-tabs-pane.active {
  display: block;
  opacity: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<head>
</head>

<body>
  <div class="container">
    <div class="megamenu-tabs">
      <ul class="megamenu-tabs-nav">
        <li><a href="#">Clothing</a></li>
        <li><a href="#">Gadgets</a></li>
        <li><a href="#">Mobiles & Smart Phones</a></li>
      </ul>

      <div class="megamenu-tabs-pane">

        <p>Clothing Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen
          book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently
          with desktop publishing software like Aldus PageMaker including versions of <a href="#">Lorem
                    Ipsum</a> .</p>
      </div>
      <!-- close megamenu-tabs-pane 1 -->

      <div class="megamenu-tabs-pane">

        <p>Gadgets Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen
          book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently
          with desktop publishing software like Aldus PageMaker including versions of <a href="#">Lorem
                    Ipsum</a>.</p>
      </div>
      <!-- close megamenu-tabs-pane 2 -->

      <div class="megamenu-tabs-pane">

        <p>Mobiles & Smart Phones Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make
          a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
          and more recently with desktop publishing software like Aldus PageMaker including versions of
          <a href="#">Lorem Ipsum</a>.</p>
      </div>
      <!-- close megamenu-tabs-pane 3-->

    </div>
    <!-- close megamenu-tabs -->
  </div>
  <!-- close container div -->
</body>

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

Challenges with spacing and unique characters in posts

Encountering an issue with white space and special characters when performing a POST action using the following AJAX function: function handleComments(id, postId, user_id) { var user_comments = encodeURIComponent($("#commentsId_"+id).val()); ...

Responsive issue identified with Bootstrap navbar hamburger menu upon clicking

My website has an issue on mobile where the hamburger menu appears but doesn't respond when clicked. I'm unsure what's causing this problem in my code. <nav class="navbar navbar-expand-lg navbar-light bg-light"> <a ...

Controlling the order of class specification prioritization

I have a situation with DOM elements that possess both the foo and bar classes: <div class="foo bar">...</div> I am looking to manage the priority between them. Referring to the W3C specification on this matter, I considered that appending ad ...

MySQL long polling technique in PHP

I stumbled upon a script that utilizes PHP long polling. It uses the code below to detect changes in a text file and returns its contents to the browser. Is there a way to modify this script so it can check a table for new events instead? $filename = dir ...

Using CSS files in the web directory in CakePHP and connecting them to HTML

Could someone offer some help with cakephp? I'm experiencing an issue related to routes that I can't seem to resolve. I've organized all my css files in webroot/css within my app. In my default layout, I included a html link <link href=" ...

Troubleshooting a JQuery and Ajax code problem

I am encountering some issues with my code and I wanted to seek help from the knowledgeable individuals on this platform. I am working on an app using JQuery and PhoneGap which includes a login function that sends credentials to a webservice via JQUery aja ...

What is the easiest way to import data into a MySQL database table?

Is there a way to move data from a Mysql table or from DataTables to another Mysql table in Laravel? The following code snippet demonstrates the deletion of data from the original table once the transfer is complete. My question is regarding how to incorp ...

Outlook creates unnecessary spacing within tables in emails

Despite all my efforts, I am facing an issue with Outlook adding an unwanted gap after one of my tables in my responsive email design. I have tried various solutions such as adding border-collapse:collapse, dividing tables, and using block styling for imag ...

Finding a jQuery DOM element without using a loop

Can the element in variable d be identified directly instead of looping through each function? Take a look at the DOM below: <!DOCTYPE html> <html lang="en"> <head> <title></title> <script src="../jquery-ui ...

When clicking on a dropdown option, the OnClick event does not seem

When selecting an option from the dropdown, it should retrieve a value from the database and display it in a text field. I implemented a click function which worked in Firefox but not in Google Chrome. HTML: <select id="classi" name="classi"> & ...

Monitoring web traffic on an AJAX website using Google Analytics

Is it possible for Google Analytics to track pageviews if the URL of a page changes dynamically using Javascript? For instance, let's say I visit the page example.com/about(1 pageview) in my browser. Then, I click on the "contacts" link, but instead ...

Get an MP4 on your iPhone (iOS) by downloading it from a link on a webpage

Despite trying various methods such as the HTML5 download tag, JavaScript, JQuery, and other techniques, I have not been able to successfully download a file to the Album. While photos can be saved by long clicking and selecting "Save Image", videos do n ...

How can I make a clickable button or link within an anchor tag that is still able to be right-clicked?

Hey there, I'm currently working on a notification dropdown menu where each <li> contains a link or an <a> tag. I'm aiming to create something similar to Facebook's notification system. However, the challenge lies in trying to ...

Calling Ajax to Flask for fetching MySQL query results

My goal is to populate a drop-down list in my HTML using AJAX by calling my flask app app.py from app.js. I am attempting to load the data when the page initially loads and triggering the AJAX within $(document).ready as shown below: Here is the content o ...

My Rails application is having trouble rendering the .js.erb file

Within my create.js.erb file, the following code is present: pollingAJAX(); function pollingAJAX() { $.ajax({ method: "POST", url: "/status", data: {uuids: '<%= @uuid_hash.to_json %>'}, }, succ ...

Identify support for the :first-child pseudo-class

Is there a way to determine with JavaScript whether the browser is compatible with the CSS :first-child selector? ...

Tips for getting rid of the glowing effect on MUI slider thumbs when they are in focus

import * as React from "react"; import Box from "@mui/material/Box"; import Slider from "@mui/material/Slider"; function valuetext(value) { return `${value}°C`; } export default function RangeSlider() { const [value, se ...

When generating a docx file with html-docx-js, it lacks the capability to incorporate external CSS class styles into the document

I have been utilizing the html-docx-js module to convert html content into docx format. However, I am facing an issue where most of my CSS is externally loaded and html-docx-js does not apply any styles. Here is a simplified code sample: const html = &ap ...

jQuery with various onClick event listeners

I have successfully implemented a calendar feature with a "More" button on each day to display additional events if there are more than three. However, I am facing an issue where clicking the button triggers the dropdown for all days instead of just the se ...

The webpage continues to load endlessly following the retrieval of JSON data from the server

I'm currently utilizing this code to retrieve JSON data from the server: $(function () { $.ajax({ url: 'fetch.php', data: "", dataType: 'jso ...