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
eozzy
69.4k111 gold badges287 silver badges448 bronze badges
2 Answers 2
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
Adam Kiss
11.9k10 gold badges51 silver badges82 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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
Darin Dimitrov
1.0m277 gold badges3.3k silver badges3k bronze badges
Comments
Explore related questions
See similar questions with these tags.
default