0

I'm using this currently:

$(document).ready(function () {
 $('ul#nav > li').hover(function () {
 $('ul:first', this).show();
 },
 function () {
 $('ul:first', this).hide();
 });
 $('ul#nav li li').hover(function () {
 $('ul:first', this).each(function () {
 $(this).css('top', $(this).parent().position().top);
 $(this).css('left', $(this).parent().position().left + $(this).parent().width());
 $(this).show();
 });
 },
 function () {
 $('ul:first', this).hide();
 });
});

..can it be improved/compacted further?

Thanks.

asked Jan 13, 2010 at 13:42

2 Answers 2

1

You could store $(this).parent in variable and chain functions, other than that it looks pretty straightforward (and i write anonym single line functions on one line)

$(document).ready(function () {
 $('ul#nav > li').hover(function () { $('ul:first', this).show(); },
 function () { $('ul:first', this).hide(); }
 );
 $('ul#nav li li').hover(function () {
 $('ul:first', this).each(function () {
 var p = $(this).parent();
 $(this).css('top', p.position().top)
 .css('left', p.position().left + p.width())
 .show();
 });},
 function () { $('ul:first', this).hide(); }
 );
});
answered Jan 13, 2010 at 13:52
Sign up to request clarification or add additional context in comments.

Comments

0

I would definitely replace:

$(this).css('top', $(this).parent().position().top);
$(this).css('left', $(this).parent().position().left + $(this).parent().width());
$(this).show();

with:

var element = $(this);
var parent = element.parent();
var position = parent.position();
element.css('top', position.top);
element.css('left', position.left + parent.width());
element.show();
answered Jan 13, 2010 at 13:52

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.