Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Simple animated dropdown menu confusion

Featured Replies

I've been following a tut on creating a simple animated dropdown menu with CSS and JQuery.

 

I've emulated it to the best of my knowledge but I'm having problems with the JQuery animation. Instead of dropping the menu down when you hover over it, the menu folds up instead of down. Similarly when you don't hover the mouse over the menu, it drops down! The complete opposite of what is needed. Attempting to hover over a listed element within the dropdown menu keeps folding it up and down, up and down etc. in an infinite loop.

 

Could anyone perhaps shed some light on it, please?

 

With animated JQuery: http://www.infekted....enu-jquery.html

 

Without animated JQuery: http://www.infekted....u-nojquery.html

 

CSS:

body {

background-color: #000000;

}

#menubar {

width: 600px;

margin-right: auto;

margin-left: auto;

margin-top: 50px;

}

#menu,

#menu ul {

list-style: none;

}

#menu {

float: left;

}

#menu > li {

float: left;

}

#menu li a {

display: block;

height: 1.5em;

line-height: 1.5em;

padding: 0 1.5em;

text-decoration: none;

color: white;

}

#menu ul {

position: absolute;

display: none;

z-index: 999;

}

#menu ul li a {

width: 80px;

}

#menu li:hover ul {

display: block;

}

#menu > li:hover > a {

color: #87c540;

}

#menu li:hover ul.noJS {

display: block;

}

 

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link href="css/reset.css" rel="stylesheet" type="text/css"/>

<title>Dropdown development</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>

<script type="text/javascript" src="js/scripts.js"></script>

<style type="text/css">

<!-- as shown in above quote -->

</style>

</head>

 

<body>

 

<div id="menubar">

<ul id="menu">

<li><a href="#">About</a></li>

<li><a href="#">Services</a>

<ul class="noJS">

<li><a href="#">Weddings</a></li>

<li><a href="#">Sympathy</a></li>

<li><a href="#">Events</a></li>

</ul>

</li> <!-- End Services List -->

<li><a href="#">Gallery</a>

<ul class="noJS">

<li><a href="#">Weddings</a></li>

<li><a href="#">Sympathy</a></li>

<li><a href="#">Events</a></li>

</ul>

</li> <!-- End Gallery List -->

<li><a href="#">Contact</a></li>

</ul> <!-- End Menu List -->

</div> <!-- End Menubar -->

 

</body>

</html>

 

JQuery:

 

$(function(){

$('#menu').find('> li').hover(function(){

$(this).find('ul')

.removeClass('noJS')

.stop(true, true).slideToggle('fast');

});

});

 

For some reason the forum destroys my neatly nested text formatting when I insert it. Inserting as code instead of quotes is even worse.

 

Thanks for any pointers.

Edited by Timo

Your CSS and jQuery are getting in each others way.

 

This CSS makes your dropdown list visible:

#menu li:hover ul {
   display: block;
}

 

While this jQuery is toggling him away again:

.stop(true, true).slideToggle('fast');

 

Try removing the CSS ( just the line at the top of my post ) and it will probably work.

 

Léon

  • Author

My bad, I forgot to add the noJS class to that CSS element to invoke it only when Javascript is unsupported.

 

#menu li:hover ul.noJS {
 display: block;
}

 

Thanks Leon :)

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.