Maintain the visibility of the section while it is being hovered over

I have successfully created a slidedown function that utilizes data in links to execute actions on specific sections. The script works well, however, there is an issue when trying to access the content within the section. Whenever I hover over the link, the section appears but it disappears when I try to interact with the content inside. How can I make the section stay visible as long as my mouse is hovering over the content without needing to change the HTML structure? Additionally, how can I keep the link underlined while the section is being hovered over?

HTML

<ul class="main-navbar-right">
    <li><a href="#" data-navigation="home">Home</a></li>
    <li><a href="#" data-navigation="explore">Explore</a></li>
    <li><a href="#" data-navigation="news">News</a></li>
</ul>

<div class="navigation-content">
    <section id="-navbar-home" data-section="home">
        <p>Home, some content with images, videos, and more and <a href="#">Link</a></p>
    </section>

    <section id="-navbar-explore" data-section="explore">
        <p>Explore home <a href="#">Link</a></p>
    </section>

    <section id="-navbar-news" data-section="news">
        <p>News about home <a href="#">Link</a></p>
    </section>
</div>

CSS

#-navbar-home,
#-navbar-explore,
#-navbar-news {
    display: none;
}

a {
    text-decoration: none;
}

ul {
    width: 100%;
    display: block;
}

li {
    float: left;
    margin: 0 15px 0 0;
}

.active, a:hover {
    text-decoration: underline;
}

JavaScript

var element;
$('li > a').hover(function() {
        var navigation = $(this).data("navigation");
        var section = $("section[data-section="+navigation+"]");
        element = $(section).slideDown();
    }, function() {
        element.hide()
});

Check out the fiddle here: http://jsfiddle.net/66qz2by8/6/

Answer №1

Here is a solution that might suit your needs. The hiding action should only occur when you move away from the larger div I inserted.

$('.myapp').hover(function() {}, function() {
  $('section[data-section]').hide();
});

$('.main-navbar-right li > a').hover(
  function() {
    $('section[data-section]').hide();
    $("section[data-section=" + $(this).data("navigation") + "]")
      .slideDown();
  }, function(e) {}
);
#-navbar-home,
#-navbar-explore,
#-navbar-news {
    display: none;
}

a {
    text-decoration: none;
}

ul {
  height: 20px;
    width: 100%;
    display: block;
  margin: 0px;
}
.navigation-content {
  height: 50px;
}
li {
  float: left;
  margin: 0 15px 0 0;
  margin-left: 10px;
}

.active, a:hover {
    text-decoration: underline;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="myapp">
<ul class="main-navbar-right">
    <li><a href="#" data-navigation="home">Home</a></li>
    <li><a href="#" data-navigation="explore">Explore</a></li>
    <li><a href="#" data-navigation="news">News</a></li>
</ul>
<div class="navigation-content">
    <section id="-navbar-home" data-section="home" data-expanded="0">
        <p>Home, some content images, videos and more and <a href="#">Link</a></p>
    </section>

    <section id="-navbar-explore" data-section="explore" data-expanded="0">
        <p>Home explore <a href="#">Link</a></p>
    </section>

    <section id="-navbar-explore" data-section="news" data-expanded="0">
        <p>Home news <a href="#">Link</a></p>
    </section>
</div>
</div>

Answer №2

To replace the use of .hover, try using .mouseenter instead. Ensure to hide the previously displayed section before displaying the new one.

Check it out here: https://jsfiddle.net/svArtist/vttjbbLy/

declare variable elem;

$('li > a').mouseenter(function () {
    var navigation = $(this).data("navigation");
    var section = $("section[data-section=" + navigation + "]");
    $("section").each(function () {
        if ($(this).data("section") !=  navigation) {
            $(this).fadeOut();
        }
    });
    elem = $(section).slideDown();
});

Answer №3

One way to achieve a similar effect using just CSS is by enclosing the "dropping-down" element within the li itself, initially setting its height to 0px and then adjusting it on hover. This can be done with a clever trick involving overflow:hidden to hide the contents and position:absolute to prevent the element from shifting the links:

Check out this fiddle for a live example.

.dropdown {
    width:200px;
    max-height:0px;
    transition:all 0s ease;
    -moz-transition:all 0s ease;
    -ms-transition:all 0s ease;
    -o-transition:all 0s ease;
    -webkit-transition:all 0s ease;
    overflow:hidden;
    position:absolute;
}
li:hover .dropdown, .dropdown:hover {
    max-height:200px;
    transition:all .4s ease;
    -moz-transition:all .4s ease;
    -ms-transition:all .4s ease;
    -o-transition:all .4s ease;
    -webkit-transition:all .4s ease;
}

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

Can the looping feature be applied to CSS?

I've been reusing the same CSS code multiple times, just changing the names each time. It feels repetitive to call the same CSS repeatedly with different names. I'm considering looping through it instead. Any suggestions? #block1{display:block; ...

Step-by-step guide to dynamically load Bootstrap tab panels through tab clicks

I have implemented Bootstrap tab panels on my website, as shown below: <!-- Nav tabs --> <ul class="nav nav-tabs" role="tablist"> <li role="presentation" class="active"><a href="#chartcontainer1" aria-controls="chartcontainer1" role ...

Adding a Fade Effect in jQuery: A Step-by-Step Guide

I have a set of 3 images and I want to achieve the following: When image1 is clicked, the background color/opacity of image2 and image3 will change. If image2 is clicked, the background color/opacity of image1 and image3 will change. If image3 is clicked ...

Tips for extracting JSON data from multiple websites using Python

Having recently delved into Python, I am currently engaged in a project that involves extracting data from numerous websites containing JSON information. While I have successfully scraped data from one website, I am uncertain about how to scrape multiple s ...

Endless Google Locations Instances

My database entries are being displayed in a table using input fields. I want the Location field for each entry to be automatically completed using Google's API. HTML: <input name="edit_airplane[1][location]" type="text" class="airplane_location" ...

Retrieve the data attribute from a specific dropdown menu using jQuery

Here is the code snippet I am currently working with: $('.tmp-class').change(function() { $('.tmp-class option:selected').each(function() { console.log($('.tmp-class').data('type')); }) ...

What is the best way to retrieve and display a well-structured JSON object in a Node

I am currently working on setting up a nodejs server with the following code: var http = require('http'); var port = 1337; http.createServer(function(req, res) { var statusList = { "hasErrors": false, "list" : [ { "id": " ...

Why does `overflow-wrap: break-words` behave differently from `word-break: break-words` and which one is more effective in certain situations?

What is the distinction between overflow-wrap: break-words and word-break: break-words? While many sources claim that there is no difference, my personal experiments suggest otherwise: <!-- truly breaks words --> <table style="width: 100px; ...

What is the best way to retrieve the URL from a specific HTML tag?

I am attempting to extract all URLs that have id='revSAR' from the provided HTML tag using a Python regex: <a id='revSAR' href='http://www.amazon.com/Altec-Lansing-inMotion-Mobile-Speaker/product-reviews/B000EDKP8U/ref=cm_cr_dp ...

I'm looking to customize my d3.js Bar Graph by changing the color of each bar individually and adding a Scale for them. How can I

I am currently working on constructing a graph that illustrates the data for the Amount of Resources utilized Per Project by creating a bar graph. I am aiming to customize the colors of the bars so that each one has its own unique color. Currently, the col ...

Adjusting webpage zoom based on different screen sizes

Hey there, I ran into an issue with my web page design. It looks great on my desktop monitors, but when I checked it on my laptop, things went haywire. Strangely, adjusting the zoom to 67% seemed to fix the problem. Both screens have a resolution of 1920 ...

Attempting to divide the given web address

Is there a way to divide the URL below into components and extract only "store" from it? http://www.store.com/products.aspx/Books/The-happy-donkey If so, what would be the best method to achieve this? ...

Is it possible for the variables in my php website to be overwritten if two users from different locations are accessing it simultaneously?

I am creating a PHP quiz where users can choose a category and see questions from that specific category in the database. I have a couple of concerns about this: Is it possible to show questions individually in PHP without the need for JavaScript? If tw ...

How can you determine if a specific ID is present on a page using jQuery?

Can anyone assist me with this issue? I need help executing #home.hide() if the page contains id = "item1". My frustration level is high. Here's my code: <tr> <td id="item1"> </tr> if($("body:has(#item1)")) { $('#home').h ...

Footer not disappearing

Need assistance! My footer isn't showing up properly at the bottom even after applying clear:both. Can someone help please? If anyone can provide guidance, it would be greatly appreciated. Thanks for all your help in resolving the issue. ...

Next.js is throwing an error: "Module cannot be found: Unable to resolve 'canvg'"

I am facing an issue in my next.js project where I keep encountering the following error message: error - ./node_modules/jspdf/dist/jspdf.es.min.js:458:25 Module not found: Can't resolve 'canvg' I'm confused because I have not included ...

Iterate through each line of code and modify the integer in order to target elements by their class

Originally shared on the MrExcel forum www.mrexcel.com/board/threads/change-integer-in-code-line-for-htmldoc-getelementsbyclassname.1146814/ The initial line of code I used was Set DogRows1 = HTMLDoc.getElementsByClassName("rpb-greyhound rpb-greyhou ...

Tips for integrating the legacy Twitter API method into an ASP.Net website

In the past, I utilized a method to display tweets from my Twitter account on a feed for my website customers: <ul id="twitter_update_list"><li>Twitter feed loading</li></ul> <script type="text/javascript" src="http://twitter.co ...

"Repeating SignalR Messages: Issue of Duplication when Stopping and Restarting

Whenever I stop and start the connection, messages sent to the client by the hub are duplicated. If I follow this sequence: $.connection.hub.stop() $.connection.hub.start() {...} and a message is sent from the server hub to the client, it is initially re ...

Troubleshooting issues with browser scrollbars in a Silverlight application (HTML)

I am currently working on a simple silverlight application and I need to incorporate web browser scroll bars into it (scroll bars should not be inside my silverlight app). This is the HTML code I have: <style type="text/css"> html, body { heigh ...