jquery menu question

I have modified an old menu system of mine to work with jquery .

The html/jquery looks like this

<div class="menumain">
	<ul class="menu" id="menu">
		<li><a href="#" class="menulink">main 1</a></li>
		<li><a href="#" class="menulink">main 2</a>
			<ul>
				<li><a href="#">sub 1</a></li>
				<li><a href="#">sub 2</a></li>
				<li><a href="#">sub 3</a></li>
				<li><a href="#">sub 5</a></li>
				<li><a href="#">sub 6</a></li>
			</ul>
		</li>
		<li><a href="#" class="menulink">main 3</a>
			<ul>
				<li><a href="#">sub 1</a></li>
				<li><a href="#">sub 2</a></li>
				<li><a href="#">sub 3</a></li>
				<li><a href="#">sub 5</a></li>
				<li><a href="#">sub 6</a></li>
			</ul>
		</li>
		<li><a href="#" class="menulink">main 4</a></li>
	</ul>
	<script type="text/javascript">
		$(document).ready(function(){
			$(".menulink").bind("mouseleave mouseenter", function() {
				$(this).parent().children('ul').stop(true, true).animate({
					height:"toggle", opacity:"toggle" 
				},250);
			});
		}) 
	</script>
</div>

Open in new window


The CSS is:
.menumain {
	width: 980px;
	margin: 0px auto 0px auto;
	padding: 0px 0px 0px 0px;
	display: block;
	height: 46px;
	background-image: url(../images/background-menu.png);
	background-repeat: repeat-x;
	text-shadow: 1px 1px 1px #000000;
}
.menu .menulink {
	padding:14px 20px 0px 20px;
	font-size: 15px;
	height:32px;
	margin:0px 0px 0px 0px;
}
.menu .menulink:hover {
	color: #898989;
}
.menu {
	list-style:none;
	margin:0;
	padding:0;
}
.menu a {
	display:block;
	color:#eeeeee;
	text-decoration:none;
	text-shadow: 1px 1px 1px #000000;
}
.menu a:hover {
	color: #898989;
}
.menu ul {
	list-style: none;
	display: none;
	position: absolute;
	top: 46;
	left: 0;
	margin: 0px;
	padding: 0px;
	background-image: url(../images/background-menu.png);
	background-repeat: repeat-x;
	background-position: 0px -46px;
	border-left: 2px solid #ddddff;
	border-right: 2px solid #ddddff;
	border-bottom: 2px solid #ddddff;
	border-bottom-left-radius: 5px;
	border-bottom-right-radius: 5px;
}
.menu li {
	position:relative;
	float:left;
}
.menu ul li {
	position: relative;
	width: 200px;
	margin: 0px;
	padding:6px 0px 0px 6px;
}
.menu ul li:last-child {
	padding:6px 0px 10px 6px;
}
.menu ul li a {
	padding:0px 0px 0px 10px;
}

Open in new window


The problem I have is that although the <ul> fades and slides correctly, as the binding is for the .menulink class when move the mouse off .menulink element to select a sub menu item it closes down the <ul> element.

What is the best way round this. Can I bind to two objects at once? am I doing this the correct (best) way in the first place. Should I just use <divs> instead?

Advice would be greatly appreciated.
LVL 2
splantonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Probably your height value in the .menulink style - as soon as you go outside of the 32px the mouseleave is firing.

Do you have a link?
0
splantonAuthor Commented:
No live link as yet. Tried taking the 32px out and no go. :(
0
Alexandre SimõesManager / Technology SpecialistCommented:
Before having a look at your code I just wanted to ask if you know this:
http://users.tpg.com.au/j_birch/plugins/superfish/

It's a time proven jquery menu and it looks like it does what you want.
The CSS is pretty easy to customize.
0
Alexandre SimõesManager / Technology SpecialistCommented:
Now actually for your problem...

The logic you're using is the one for CSS. I bulit a working sample that works without javascript: http://jsfiddle.net/ATrAK/
The only think I did was removing the javascript code and add this css style:
.menu li:hover ul {
    display: block;
}

Open in new window

The difference between CSS and javascript here is that CSS actually cares that you are hover a child element and javascript mouseleave event doesn't...

To make it work with your code you must move the menulink class to the <li> thet is actually the one that contains the child elements. See the working code here: http://jsfiddle.net/ATrAK/2/

Here's the changed code for reference:
<div class="menumain">
    <ul class="menu" id="menu">
        <li class="menulink"><a href="#">main 1</a>

        </li>
        <li class="menulink"><a href="#">main 2</a>

            <ul>
                <li><a href="#">sub 1</a>

                </li>
                <li><a href="#">sub 2</a>

                </li>
                <li><a href="#">sub 3</a>

                </li>
                <li><a href="#">sub 5</a>

                </li>
                <li><a href="#">sub 6</a>

                </li>
            </ul>
        </li>
        <li class="menulink"><a href="#">main 3</a>

            <ul>
                <li><a href="#">sub 1</a>

                </li>
                <li><a href="#">sub 2</a>

                </li>
                <li><a href="#">sub 3</a>

                </li>
                <li><a href="#">sub 5</a>

                </li>
                <li><a href="#">sub 6</a>

                </li>
            </ul>
        </li>
        <li class="menulink"><a href="#">main 4</a>

        </li>
    </ul>
</div>

Open in new window

			$(".menulink").bind("mouseleave mouseenter", function () {
			    $(this).children('ul').stop(true, true).animate({
			        height: "toggle",
			        opacity: "toggle"
			    }, 250);
			});

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
splantonAuthor Commented:
Well done. Possible a case of not seeing the wood for the trees on my part. :)
Many thanks again - so simple with an extra (fresh) set of eyes.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.