横向子菜单栏ul根据其子元素li个数动态获取宽度,并与父li绝对垂直居中的jquery代码段

时间:2023-03-09 07:07:02
横向子菜单栏ul根据其子元素li个数动态获取宽度,并与父li绝对垂直居中的jquery代码段

;(function(window){
    $('.menuitem').hover(function(){
        $('>a',this).css('background-color','#60AEEC');

$('.subnav',this).show();

$('.subnav',this).width(function(){
           return $('.subnav').children("li:visible").width()*$('.subnav').children("li:visible").length;
       });
       var sleft = $(this).offset().left-($('.subnav').children("li:visible").width()*$('.subnav').children("li:visible").length-150)/2;
       $('.subnav',this).offset({left:sleft});
    },function(){
        if( !$(this).hasClass('selected') )
            $('>a',this).css('background-color','transparent');
        $('.subnav',this).hide();
    });
     /*$('.subnav').hover(function(){},function(){
        $('>a',this).css('background-color','transparent');
        $(this).hide();
    });*/

})(window);