Allow click on twitter bootstrap dropdown toggle link?

后端 未结 8 1337
眼角桃花
眼角桃花 2020-11-28 04:27

We have setup the twitter bootstrap dropdown to work on hover (as opposed to click [yes we are aware of the no hover on touch devices]). But we want to be able to have the m

相关标签:
8条回答
  • 2020-11-28 04:36

    This can be done simpler by adding two links, one with text and href and one with the dropdown and caret:

    <a href="{{route('posts.index')}}">Posts</a>
    <a href="{{route('posts.index')}}" class="dropdown-toggle" data-toggle="dropdown" role="link" aria-haspopup="true" aria- expanded="false"></a>
    <ul class="dropdown-menu navbar-inverse bg-inverse">
        <li><a href="{{route('posts.create')}}">Create</a></li>
    </ul>
    

    Now you click the caret for dropdown and the link as a link. No css or js needed. I use Bootstrap 4 4.0.0-alpha.6, defining the caret is not necessary, it appears without the html.

    0 讨论(0)
  • 2020-11-28 04:37

    I'm not sure about the issue for making the top level anchor element a clickable anchor but here's the simplest solution for making desktop views have the hover effect, and mobile views maintaining their click-ability.

    // Medium screens and up only
    @media only screen and (min-width: $screen-md-min) {
        // Enable menu hover for bootstrap
        // dropdown menus
        .dropdown:hover .dropdown-menu {
            display: block;
        }
    }
    

    This way the mobile menu still behaves as it should, while the desktop menu will expand on hover instead of on a click.

    0 讨论(0)
  • 2020-11-28 04:38

    An alternative solution is just to remove the 'dropdown-toggle' class from the anchor. After this clicking will no longer trigger the dropwon.js, so you may want to have the submenu to show on hover.

    0 讨论(0)
  • 2020-11-28 04:47

    For those of you complaining about "the submenus don't drop down", I solved it this way, which looks clean to me:

    1) Besides your

    <a class="dropdown-toggle disabled" href="http://google.com">
         Dropdown <b class="caret"></b>
    </a>
    

    put a new

    <a class="dropdown-toggle"><b class="caret"></b></a>
    

    and remove the <b class="caret"></b> tag, so it will look like

    <a class="dropdown-toggle disabled" href="http://google.com">
    Dropdown</a><a class="dropdown-toggle"><b class="caret"></b></a>
    

    2) Style them with the following css rules:

    .caret1 {
        position: absolute !important; top: 0; right: 0;
    }
    
    .dropdown-toggle.disabled {
        padding-right: 40px;
    }
    

    The style in .caret1 class is for positioning it absolutely inside your li, at the right corner.

    The second style is for adding some padding to the right of the dropdown to place the caret, preventing overlapping the text of the menu item.

    Now you have a nice responsive menu item which looks nice both in desktop and mobile versions and that is both clickable and dropdownable depending on whether you click on the text or on the caret.

    0 讨论(0)
  • 2020-11-28 04:48

    You could use a javascript snippit

    $(function()
    {
        // Enable drop menu clicks
        $(".nav li > a").off();
    });
    

    That will unbind the click event preventing url changing.

    0 讨论(0)
  • 2020-11-28 04:48

    Here's a little hack that switched from data-hover to data-toggle depending the screen width:

    /**
     * Bootstrap nav menu hack
     */
    $(window).on('load', function () {
        // On page load
        if ($(window).width() < 768) {
            $('.navbar-nav > li > .dropdown-toggle').removeAttr('data-hover').attr('data-toggle', 'dropdown');
        }
    
        // On window resize
        $(window).resize(function () {
            if ($(window).width() < 768) {
                $('.navbar-nav > li > .dropdown-toggle').removeAttr('data-hover').attr('data-toggle', 'dropdown');
            } else {
                $('.navbar-nav > li > .dropdown-toggle').removeAttr('data-toggle').attr('data-hover', 'dropdown');
            }
        });
    });
    
    0 讨论(0)
提交回复
热议问题