Steps for adding a navbar that slides horizontally and overlaps other links on a webpage

I am attempting to design a sliding navigation menu using JQuery. The concept involves having multiple main sections, each with several subsections.

When a user hovers over a section, the subsections will expand horizontally. If another section is currently open and displaying its subsections, it will collapse back to show only the main section.

I have written some basic code to increase the width of a section:

jQuery(document).ready(function($) {

// Section 1
$('#S1Hover').mouseover(function() {
    $(this).css('cursor', 'pointer');
    if ($('#S2wrapper').is(":visible")){
        $('#S2wrapper').animate({width: 0})
    }
    $('#S1wrapper').animate({width: 400})
});

// Section 2
$('#S2Hover').mouseover(function() {
    $(this).css('cursor', 'pointer');
    if ($('#S1wrapper').is(":visible")){
        $('#S1wrapper').animate({width: 0})
    }
    $('#s2wrapper').animate({width: '300'});
});   
});

The HTML for this functionality is as follows:

<div id="main">
    <div id="S1Hover" class="event">Section 1</div>
    <div id="S1wrapper">
    <ul id="S1">
        <li id="SS1"><a href="#">SS1</a></li>
        <li id="SS2"><a href="#">SS2</a></li>
        <li id="SS3"><a href="#">SS3</a></li>
        <li id="SS4"><a href="#">SS4</a></li>
    </ul>
    </div>

    <div id="S2Hover" class="event">S2</div>
    <div id="S2wrapper">
    <ul id="projects-nav">
        <li id="SS5"><a href="#">SS5</a</li>
        <li id="SS6"><a href="#">SS6</a></li>
        <li id="SS7"><a href="#">SS7</a></li>
    </ul>
    </div>
</div>      

Currently, the functionality is not working as intended, so any assistance would be greatly appreciated. Additionally, are there any specific browser-related issues I should be aware of once this is functioning correctly?

EDIT: I forgot to mention that I would like a section to remain expanded until a different section is hovered over.

Thank you!

Answer №1

To implement this concept, the element you want to hover over must be enclosed within another element that triggers the hovering effect. For instance, if you want #S2Wrapper to reveal its contents on hover, you need to wrap it around that element.

<div id="S2Wrapper">
 <div class="hover-stuff">
  <ul>
   <li>This content is revealed on hover</li>
  </ul>
 </div>
</div>

Next, hide the .hover-stuff and only show it when S2Wrapper is hovered over. You should also set S2Wrapper's position property to relative, and hover-stuff's to absolute according to your desired dimensions.

The purpose of this setup is that S2Wrapper triggers the hover effect and will revert back to its original state once the hover action is no longer in place.

Answer №2

To maintain your DOM structure mostly unchanged, you may consider implementing the following approach:

HTML:

<div id="main">
<div id="S1Hover" class="event">Section 1</div>
<div class="wrapper">
<ul id="S1">
    <li id="SS1"><a href="#">SS1</a></li>
    <li id="SS2"><a href="#">SS2</a></li>
    <li id="SS3"><a href="#">SS3</a></li>
    <li id="SS4"><a href="#">SS4</a></li>
</ul>
</div>

<div id="S2Hover" class="event">S2</div>
<div class="wrapper">
<ul id="projects-nav">
    <li id="SS5"><a href="#">SS5</a</li>
    <li id="SS6"><a href="#">SS6</a></li>
    <li id="SS7"><a href="#">SS7</a></li>
</ul>
</div>

CSS:

.wrapper ul{
        margin:0;
        padding:0;
    }
    .wrapper ul li{
        float:left;
        list-style:none;
        margin-right:6px;
        width:0;
        height:20px;
        overflow:hidden;
    }

Javascript:

   jQuery(document).ready(function ($) {

        $('.event').hover(function () {
            $(this).next().find('li').animate({width:80}, 750);
        }, function () {
            $(this).next().find('li').animate({ width: 0 }, 1000);
        });
    });

Consider assigning a class to the wrapper instead of an id for each one. Additionally, note that specific values like 80px as width and 20px as height for <li> sections are hardcoded here, and can be adjusted based on your content.

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

Manipulating DropDownList Attributes in ASP.NET using JavaScript

I am facing an issue with populating a Dropdownlist control on my ASCX page. <asp:DropDownList ID="demoddl" runat="server" onchange="apply(this.options[this.selectedIndex].value,event)" onclick="borderColorChange(this.id, 'Click')" onblur="bo ...

How can you utilize jQuery's .post() method to send information as an object?

When I send a .post() request like this var data = $(this).serialize(); $('form').on('submit', function(event) { event.preventDefault(); $.post('server.php', data, function(data) { $('#data').append( ...

Tips for preventing a bootstrap modal from being dragged beyond its parent container

I need help figuring out how to prevent my bootstrap modal from being dragged outside of its parent container. Is there a way to constrain the modal within its parent? $(document).ready(function() { ShowValidationResult(); }); function ShowValidation ...

What is the most effective method for developing HTML with PHP or Jquery?

My JavaScript Object contains some important information that I need to display. I have two possible options for generating the HTML from this object. I am unsure which method is the most appropriate, or if it is simply a matter of personal preference. 1 ...

Setting dynamic minimum and maximum datetimes on an input field with type 'datetime-local' or text mode 'Datetimelocal' using jQuery or C#

Seeking help to implement dynamic minimum and maximum date time setting, where the last three days are enabled and remaining days are disabled. The below C# code is not producing the desired output. TextBox1.Attributes["min"] = DateTime.Now.AddDa ...

Tips for wiping clean and restarting data in a modal?

After closing and reopening this modal, both the old information and new data remain. I aim to reset everything within the modal, wiping out details from both the header and body. Expected scenario - Actual situation - Code- .html.erb file <div cla ...

Unable to define the color of icons path using CSS in Vue 3

When using the iconify library for VueJS, the icons' paths automatically have "currentColor" as their fill color. The issue arises when trying to set a path's color via CSS, as it seems to be ineffective. Even with "!important", the color won&apo ...

Create a stylish bottom border exclusively for the text that has been wrapped

My goal is to create an underline that perfectly fits the width of the text on the bottom row, without extending beyond it. The desired effect is shown in Figure 1. Figure 1 Here is the HTML I am currently using: <h2><span class="inline-block"& ...

In IE8, a border is applied to the max-width property when the width is set to

I'm attempting to design a box that adjusts in size according to the parent container, utilizing a mix of max-width and width:100%. With this setup, the box's width is determined by the max-width property, ensuring it shrinks within its boundari ...

Ways to access the files attribute in an input tag in AngularJS without relying on getElementById

I am currently working on file uploads using AngularJS and I have a question regarding how to retrieve input files similar to regular JS. What I want to achieve: HTML: <input type="file" name="file" id="fileImg" accept="image/*"> JS: var file ...

As the height is expanded, the background color gradually infiltrates the body of the page

I am currently working on an angular application that utilizes angular-pdf. The controller and view function perfectly, and the PDF is displayed correctly, except for one issue. The height of the PDF exceeds the min-height of the module, causing it to expa ...

Issue with CSS causing dropdown to open underneath modal

I am facing an issue with my bootstrap modal that contains multiple dropdowns. To accommodate the content, I have set overflow: auto to enable a scroll bar on the right side of the modal. The problem arises when I click on a dropdown - it opens under the ...

What is causing the container to overlap with my sidebar-like <nav>?

Why is the fluid-container overlapping in this way, while others are not? Does this look correct to you, or am I overlooking something? It seems incorrect to me. <nav class="nav flex-column float-left"> {links} </nav> <div class="conta ...

Retrieve every HTML element that is currently visible on the screen as a result

I have a dynamic HTML table that updates frequently, with the potential for over 1000 rows. Instead of replacing the entire table each time it updates, I am exploring options to only update the visible rows. My initial approach involved iterating through ...

Set the first link in a menu to be highlighted as active using jquery

In order to mark the first link in my menu as active, I need it to have specific CSS settings applied. Using jQuery to add these settings to every link when clicked on makes it a bit tricky (simple menu = clicking changes color). So, I want the first link ...

Customizing Material-UI: A guide to overriding inner components

Looking to customize the styles of a Material UI Switch for the small variation? I have applied global styles but now want to override the styles of elements like track and thumb. Targeting the root element with the sizeSmall class is working, but unsure o ...

Maintain font kerning for span elements that are not displayed inline

I am looking to add some transform effects to individual letters in a word. To achieve this, I have enclosed each letter within a span element. However, when setting the display of these spans to inline-block, the font kerning gets disrupted. I have exper ...

Guide on determining the total count based on a specific condition with the help of jQuery

Using AJAX, I have successfully retrieved all status values such as Active, ACTIVE_DRAFT, and Pending. However, I only want to select the statuses that are equal to ACTIVE. To do this, I am using an if condition which is working fine. After filtering by st ...

Error in TypeScript while running the command "tsd install jquery" - the identifier "document" could not be found

Currently, I am facing an issue with importing jQuery into my TypeScript project. In order to achieve this, I executed the command tsd install jquery --save, which generated a jquery.d.ts file and added /// <reference path="jquery/jquery.d.ts" /> to ...

How can I position text below a ticked checkbox?

Having an issue with my HTML and jQuery code. Everything is working fine, but when I click on a checkbox, the text "FINISHED" appears below all checkboxes instead of just the one I clicked on. This is my html code: $('.label__checkbox').cli ...