Enhance the design of a floating line menu for a dropdown navigation bar

I'm attempting to create a menu with a sliding line under the menu title that appears when hovering over it.

It seems to work fine for a "simple" menu, but not for the dropdown menu...

Here is the JSFIDLE

My code:

HTML

<div class="nav-wrap">
    <ul class="group" id="example-one">
        <li class="current_page_item">
            <a href="#">Home</a>
        </li>
        <li class=""><a href="#">Buy Tickets</a></li>
        <li class=""><a href="#">Group Sales</a></li>
        <li class=" dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown"><span>Test0</span> <b class="caret"></b></a>
            <ul class="dropdown-menu">
                <li><a href="#"><span class="fa fa-tasks"></span> Test1</a></li>
                <li><a href="#"><span class="fa fa-check"></span> Test2</a></li>
                <li><a href="#"><span class="fa fa-file"></span> Test3</a></li>
                <li><a href="#"><span class="fa fa-play-circle"></span> Test4</a></li>
            </ul>
        </li>
        <li class=""><a href="#">The Show</a></li>
        <li class=""><a href="#">Videos</a></li>
        <li class=""><a href="#">Photos</a></li>
        <li class=""><a href="#">Magic Shop</a></li>
    </ul>
</div>

CSS

.nav-wrap {
  background-color: rgba(0,0,0,0.6);
  border-top:    2px solid white;
  border-bottom: 2px solid white;
}

.group:after { visibility: hidden; display: block; content: ""; height: 0; }
*:first-child+html .group { zoom: 1; }


#example-one {
  margin: 0 auto;
  list-style: none;
  position: relative;
  width: 960px;
}
#example-one li {
  display: inline-block;
}
#example-one a {
  color: #bbb;
  font-size: 14px;
  float: left;
  padding: 6px 10px 4px 10px;
  text-decoration: none;
  text-transform: uppercase;
}
#example-one a:hover {
  color: white;
}
#magic-line {
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 100px;
  height: 2px;
  background: #fe4902;
}
.current_page_item a {
  color: white !important;
}
.ie6 #example-one li, .ie7 #example-one li {
  display: inline;
}
.ie6 #magic-line {
  bottom: -3px;
}

ul.dropdown-menu {
    min-width: auto;
}

li.dropdown:hover > ul {
    display: inline!important;
}

.current_page_item_two a {
  color: white !important;
}

JS

var $el, leftPos, newWidth;

$("#example-one").append("<li id='magic-line'></li>");

var $magicLine = $("#magic-line");

$magicLine
    .width($(".current_page_item").width())
    .css("left", $(".current_page_item a").position().left)
    .data("origLeft", $magicLine.position().left)
    .data("origWidth", $magicLine.width());

$("#example-one li").find("a").hover(function() {
    $el = $(this);
    leftPos = $el.position().left;
    newWidth = $el.parent().width();

    $magicLine.stop().animate({
        left: leftPos,
        width: newWidth
    });
}, function() {
    $magicLine.stop().animate({
        left: $magicLine.data("origLeft"),
        width: $magicLine.data("origWidth")
    });
});

$(".current_page_item_two a").mouseenter();

How can I make the JS work for the dropdown section and avoid underlining the "Test0" menu?

Thank you for your assistance!

Answer №1

To update your hover code, make the following changes:

$("#example-one li").hover(function() {
    $el = $(this);
    leftPos = $el.position().left;
    newWidth = $el.width();

It's strange that using an 'a' element with the class "dropdown-toggle" returns a left position of 0. However, substituting it with an 'li' element seems to resolve the issue.

Check out this fiddle for reference: http://jsfiddle.net/7hvrxw2c/42/

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 process for locating the src value of an image that has been uploaded utilizing fileReader?

I am currently working on a program that empowers the user to select an image for uploading. Once the user chooses an image, it activates the previewFile() function, which makes use of FileReader to display the selected image. Now, I'm in the process ...

Diving Div Floating

I've been working on this code snippet for quite some time, but I can't seem to find a solution that keeps the two div elements horizontal when resizing the browser window. <!DOCTYPE html> <head> <style> #ONE { ...

AXIOS: Is it possible to send multiple data items in a single POST request to an API?

My form collects data, which is then sent to an API using the new FormData(). However, one of the items in the model contains multiple attributes in the MongoDB model. Since the form requires a 1-to-1 key-pair relationship, it can't be sent as an obje ...

Show the page information directly on the current page instead of redirecting it to a different page

On my website (For example, Link1: http://localhost:8086/MyStrutsApp/login.do), there are multiple links available. Whenever a user clicks on one of these links, they are directed to another page (For instance, link2: http://localhost:8086/MyStrutsApp/AddB ...

Checkboxes display on a separate line when rendered inlines

I'm having an issue with the rendering of my checkbox in CSS. The checkbox is currently displaying on a separate line from the text, but I would like it to be inline with the rest of the content. For reference, here is the full CSS and HTML code: htt ...

What is the best way to display images one by one from a map using a random fade effect?

I am in the process of creating a logo wall for a website. I successfully managed to display them randomly using a map, but now I want to make them appear one by one in a random order (for example: image 1, image 6, image 3, ...) and keep them visible once ...

Applying CSS Styles to a Single Class Only

I have been using Zurb's Foundation to customize the navbar with my own styles, which has been successful so far. However, I encountered an issue with the responsive behavior of the navbar when the viewing container size changes. The navbar adds a sec ...

"Modifying the query string in AngularJS: A step-by-step guide

For instance, let's say I have the following URL: http://local.com/. When I invoke a function in my SearchController, I want to set the parameter text=searchtext and obtain a URL like this: http://local.com/?text=searchtext. What is the correct way t ...

What is the best way to maximize vertical space in every element on a page?

How can I achieve a layout similar to the image shown? I want three distinct sections: a navigation bar fixed at the bottom, and left and right sides each taking up 50% of the screen. Additionally, all boxes on the left side should have equal height. I am ...

Incorporating VueJS with a sleek material design aesthetic

Currently, I am in the process of developing a web application using VueJs and am in need of a CSS framework to aid in designing without starting from scratch! After some research, I came across material-design-lite (www.getmdl.io) but unfortunately faced ...

Struggling to display a cylinder using three.js, encountering error message "Unable to access property 'type' of an undefined object"

I am facing an issue with my three.js code where adding a simple cylinder is causing the renderer to crash: const lineGeometry = new Three.CylinderBufferGeometry(1.0, // radiusTop 1.0, // radiu ...

Simulating dynamic route parameters in the Next 13 application directory

I am currently working with Jest and testing library to conduct unit tests on my NextJS application. I am facing difficulties in rendering a page on a dynamic path. Here is the code for my page/component: export default async function MyPage({ params }: { ...

Adjusting layout to second row based on screen width using CSS media query

My div contains the following elements: <div> <a class="postLink" href="{{post.authorLink}}" target="_blank">{{post.author}}</a> <span class="">Published: {{ post.published}}</span> <a class="postLink" href="{ ...

Controller unable to update AngularJS view

As the title suggests... I'm facing a challenge with this code snippet: (function(angular) { 'use strict'; angular.module('app', []) .controller('ctrl', ['$scope', function($scope) { $scope.ini ...

Leveraging Vue properties within CSS styling

I am looking to utilize Vue data/prop variables within the <style> tag located at the bottom of my component. For example, suppose I have a component structured like this: <template> <div class="cl"></div> </template> < ...

Is there a way to customize the timepicker pop-up so that it extends the entire width of the parent form control

<FormControl fullWidth> <LocalizationProvider dateAdapter={AdapterDayjs}> <TimePicker views={["hours", "minutes", "seconds"]} label={t("StrategyManager.Select_Time")} value={timer} ...

Update the div using jq_form_remote_tag

Take a look below to see two images depicting a problem. The code snippet in my template, courtesy of j0k: <div id="listdiv"> echo jq_form_remote_tag(array( 'update' => 'listdiv', 'url' => 'shoppingl ...

Consistently retrieving the same array value from the database even after refreshing the page

I am having an issue with the arr variable in my code. It seems to hold the time elements I capture whenever a user pauses a video, but the value of arr remains unchanged every time I refresh the page or perform the activity differently. Does anyone know ...

Navigating through a complex array structure and having the capability to both search and filter its elements

hi there! I'm reaching out for some assistance with a challenge that I've encountered. Currently, I am working on developing a dashboard that iterates through criminal charges. I have structured the database table as follows: category charges ...

Mobile view div failing to fill remaining space

When viewing my website on a mobile device, the heading in the div tag is not occupying the remaining space like it does on desktop. The heading is an h4 tag inside a div tag for mobile view. If you want to check out my website in inspect mode, here is the ...