Sliding Vertical Navigation Panel

Hey, I'm working on creating a vertical sliding effect and I've managed to get it almost right with simple code. However, there are three issues that I need help with:

  • I want the sub menus to be closed by default when the page loads
  • When quickly hovering over the links, the sub menus start sliding erratically. I need the animation to be smoother and more controlled
  • Currently, if you hover anywhere aligned with the links that slide down, they will trigger the animation. I want the sliding effect to occur only when directly mousing over the link itself.

Below is the code. Any help would be appreciated. Thanks!

<script type='text/javascript'>
$(document).ready(function(){
$("nav.main_menu li").hover(function(){
    $(this).children("ul").slideDown(500);   
},function(){
 $(this).children("ul").slideUp(300);      
});
});
</script>


<nav class="main_menu">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">News</a>
<ul class="sub-menu">
<li><a href="#">Events</a></li>
<li><a href="#">Updates</a></li>
</ul>
</li>
<li><a href="#">People</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Gallery</a>
<ul class="sub-menu">
<li><a href="#">2009</a></li>
<li><a href="#">2010</a></li>
<li><a href="#">2011</a></li>
<li><a href="#">2012</a></li>
<li><a href="#">2013</a></li>
<li><a href="#">2014</a></li>
<li><a href="#">2015</a></li>
</ul>
</li>
<li><a href="#">Contact</a></li>
<li><a href="#">Bio</a></li>

                </ul>
            </nav>

Answer №1

  • If you want to hide the submenus, you can either use CSS to set them as display:none;, or run $("li ul").hide(); within the .ready() function before implementing the hover effect.
  • This issue is a common drawback of hover menus like this. I have tried using a jQuery plugin called HoverIntent in the past to mitigate it, but ultimately decided to avoid animation altogether due to the complexity involved.
  • It seems like the li elements may be taking up more space on the page than intended. Double-check your CSS settings for the elements and consider restricting their height or width. As long as overflow:hidden is not enabled, the submenus should still display properly.

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

How to align CSS counters to the right within the ::before pseudo-element

Utilizing CSS counters and the <code> element to display syntax highlighted code snippets with automatic line numbering: JSFiddle HTML: <code> <div class="line"><span>line 1</span></div> <div class="line">< ...

Trouble getting CSS to load in Webpack

I'm having some trouble setting up Webpack for the first time and I think I might be overlooking something. My goal is to use Webpack's ExtractTextPlugin to generate a CSS file in the "dist" folder, but it seems that Webpack isn't recognizi ...

Animate the coloring process with dynamic motion

Is there a way to dynamically fill a canvas with color and apply animation? I have successfully created a canvas cylinder that fills with color, but I'm hoping for a slow animation effect when the color is filled. I've tried searching on Google ...

The process of enriching an ASP.NET hyperlink by making it both bold and under

I'm currently working on a webpage in asp.net. I have two hyperlinks and I want to make them active by applying a style sheet that makes them bolder and underlined, but for some reason it's not working. Here is the HTML: <li style="margin-le ...

How can I customize the border color in Bootstrap with a specific hex code?

Being new to the world of web design, I have a burning question that may have an easy solution that has evaded my notice. In my current project, I am attempting to create a div with a colored border using bootstrap. Specifically, I want to use a specific h ...

What could be causing the Grid Child, which has a fixed width and a max-width of 90%, to overflow the

Question: Could you please explain why the .header element overflows its grid track when the viewport width reaches 1500px? Code:: * { box-sizing: border-box; padding: 0; margin: 0; } body { background: #6e28d9; color: white; } .container { ...

Can the Bootstrap grid be arranged vertically?

I'm finding it difficult to work on a project with Bootstrap's horizontal grid system. What I want to achieve is to have my page divided into 4 sections, with the left side blocks having equal height and the right side blocks having varying heigh ...

How to design an HTML table with columns of a set width using CSS

I need a table that has a fixed width but dynamic height to accommodate varying content lengths. Take a look at this example: Despite setting a fixed width, my content is overlapping. What am I doing incorrectly? Here is my current code: <table width= ...

List items containing Bootstrap grid layout

My idea is to incorporate Bootstrap 3 rows and col-*s into my numbered list. Specifically, I have a form where users can add multiple addresses. Each address includes form fields for street, city, etc., with these fields aligned in col-*s to ensure consist ...

The transition feature is not functioning properly in Firefox

I have a basic HTML and CSS setup below. It seems to be working fine in Chrome, but I'm having trouble with the transition effect in Firefox. I am currently using Firefox version 18. I tried looking up solutions on Google and Stack Overflow, but none ...

Mobile responsiveness issue with Bootstrap image heights

Creating a website with an image positioned at the top below the navigation bar. Utilizing Bootstrap 4 and CSS for this project. To ensure that the image spans the full width of the screen on larger devices, I employed background-image. For mobile devices, ...

Tips for ensuring that a span element outside of an anchor functions as though it were inside of it

I'm facing a challenge with my markup that I need help solving. Here is the scenario: I have provided a link within an anchor tag, but I want users to be able to click anywhere on the li tag in order to be redirected to the link specified in the ancho ...

Adding a background color with a background image layered underneath it in CSS - here's how!

Looking to set a background color for a div, with a background image on the bottom of the same div. However, I need the background color to stop once the background image begins. Check out the example image below: ...

How to Utilize a Google Map Embed on Your Website?

Can I incorporate a Google Map into my website using only CSS? <div class="map"></div> .map{ background: url("https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d4450.37576469368!2d47.31305345548099!3d56.101968401211494!2m3! ...

Column of Flexbox showcasing items that have been translated

I'm having an issue with positioning items in a div of 11 elements divided into 2 columns. The first row and the last item are positioned correctly, but the items in between are shifted to the left. I've been using flexbox for a while, but I can& ...

Button positioned in the upper right corner

Trying to create a navigation menu like the one shown in the image below. https://i.sstatic.net/N9BQ5.png I've successfully styled the right corner, but I'm struggling to extend the top left corner. https://i.sstatic.net/cL0ZK.png Below is th ...

Is it a:first-child or a:first-child aside?

I'm currently working on designing a sidebar and I'm trying to figure out how to remove text decoration using the first-child selector. Here is a snippet of the HTML code I am working with: <aside> <nav> <div id="guid ...

The final picture exceeding the boundaries in the Bootstrap photo album

After taking inspiration from the bootstrap album, I decided to create my own album. I encountered a problem where the last image in each row appears larger than the first two. I have examined the css code but have been unable to pinpoint the issue. You ...

Display all elements on a webpage using Javascript without the need for scrolling

I'm looking for a way to ensure all items on a webpage load immediately, without having to scroll down multiple times before running a script to find a specific item. Is there a method to have all items load at once without the need to manually scroll ...

What is the best way to set a stationary background that scrolls on a responsive webpage?

I'm working on a responsive website with alternating sections featuring fixed background images where the content is meant to scroll over the image. However, I'm facing an issue on iOS where the background image zooms into a small section and sca ...