jQuery Superfish align sub-menu under main menu
Hi,
I used Superfish to display menu and submenus in horizontal style. As you know in Superfish all the submenus are left align. I want to align the sub-menus centered according to the main menu. I thought to find out the width of a sub-menus item. As in super-fish there is delay in appearance of sub-menus so I can't get the width of submenu item becuause I am using '
onShow'.
Then in superfish
onBeforeShow
I cloned the item in DIV and tried to find out its width. But the problem is the width of item is different in
onBeforeShow and
onShow.
I could not figure out what I am doing wrong.
-
- jQuery(document).ready(function () {
-
- jQuery('ul.sf-menu').superfish({
- pathClass: 'current',
- onBeforeShow: function () {
- getItem($(this), 'onBeforeShow');
- },
-
- onShow: function () {
- getItem($(this), 'onShow');
- }
- });
-
-
-
- //Real Width of Element even if element/parents are hidden
- function getItem(item,triggerBy) {
- var lastSub = (item).closest('li').attr('id');
- var $subMenuFirstItem = $('#' + lastSub + ' li:Last');
- console.log('RealWidth: ' + $subMenuFirstItem.elementRealWidth() + ' Width: ' + $subMenuFirstItem.width() + ' Trigger: ' + triggerBy);
-
- }
-
- $.fn.elementRealWidth = function () {
- /*var $clone = this.clone()
- .css("visibility", "hidden")
- .appendTo($('body'));
- */
- var $clone = this.clone().appendTo($('#EmptyDummy'));
-
- var $width = $clone.children().width();
- // $clone.remove();
- return $width+' - '+this.text();
- };
-
- });