how to make twitter bootstrap submenu to open on the left side?

前端 未结 12 2131
余生分开走
余生分开走 2020-12-13 05:12

I was trying to create twitter bootstrap submenu in dropdown menu, but I\'ve got a problem: I have dropdown menu in the top right corner of the page and that menu has one mo

12条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-13 06:11

    I have created a javascript function that looks if he has enough space on the right side. If it has he will show it on the right side, else he will display it on the left side

    Tested in:

    • Firefox (mac)
    • Chorme (mac)
    • Safari (mac)

    Javascript:

    $(document).ready(function(){
        //little fix for the poisition.
        var newPos     = $(".fixed-menuprofile .dropdown-submenu").offset().left - $(this).width();
        $(".fixed-menuprofile .dropdown-submenu").find('ul').offset({ "left": newPos });
    
        $(".fixed-menu .dropdown-submenu").mouseover(function() {
            var submenuPos = $(this).offset().left + 325;
            var windowPos  = $(window).width();
            var oldPos     = $(this).offset().left + $(this).width();
            var newPos     = $(this).offset().left - $(this).width();
    
            if( submenuPos > windowPos ){
                $(this).find('ul').offset({ "left": newPos });
            } else {
                $(this).find('ul').offset({ "left": oldPos });
            }
        });
    });
    

    because I don't want to add this fix on every menu items I created a new class on it. place the fixed-menu on the ul:

    I hope this works out for you.

    ps. little bug in safari and chrome, first hover will place it to mutch to the left will update this post if I fixed it.

提交回复
热议问题