我想你所追求的是:
http://jsfiddle.net/aSr3J/20/
本质上,您的 mouseleave 函数必须看起来像这样
$('#lava').mouseleave(function () {
left = Math.round($(".selected").offset().left - $('#lava').offset().left);
width = $(".selected").width();
//Set the floating bar position, width and transition
$('#box').stop(false, true).animate({left: left},{duration:1000, easing: style});
$('#box .head').stop(false, true).animate({width:width},{duration:1000, easing: style});
});
请注意,我还为样式表中的链接添加了颜色定义:
#lava ul a li { color:#fff; }
(你知道像li
inline-elements 这样的封闭块级元素a
只在 HTML5 中有效吗?)
至于菜单文本的颜色,我也修改了 $('#lava li').hover(function ())
:
$('#lava li').hover(function () {
//Get the position and width of the menu item
left = Math.round($(this).offset().left - $('#lava').offset().left);
width = $(this).width();
$(this).css("color","black");
//Set the floating bar position, width and transition
$('#box').stop(false, true).animate({left: left},{duration:1000, easing: style});
$('#box .head').stop(false, true).animate({width:width},{duration:1000, easing: style});
//if user click on the menu
},function() { $(this).css("color","white");}).click(function () {
//reset the selected item
$('#lava li').removeClass('selected');
//select the current item
$(this).addClass('selected');
});