The content of my dropdown menu is closing too fas

2019-07-08 06:01发布

I am building a site and I am stuck into this problem... when I mouse over a li element and the submenu is displayed, I can't click in nothing inside it because it closes really fast :/ I found similar problems and solutions over here already, tried to implement them but didn't worked :/ (JSFiddle Link; http://jsfiddle.net/qbNLN/1)

HTML code:

<ul class="main_navigation" role="menubar">
<li role="menuitem">
    <a href="#" class="link">Compre por Marcas</a>
    <div class="balloon balloon_big">
        <span class="balloon_top" style="left: 38px;"></span>
        <p class="base-title"><strong>Compre por Marcas</strong></p>
        <div class="menucolumns">
            <div class="column1">
                <ul class="submenuitems column1">
                    <li><a href="#" title="4 Plus Nutrition">4 Plus Nutrition</a></li>
                </ul>
            </div>
            <div class="column1">
                <ul class="submenuitems column1">
                    <li><a href="#" title="Hayabusa">Hayabusa</a></li>
                </ul>
            </div>
            <div class="column1">
                <ul class="submenuitems column1">
                    <li><a href="#" title="Pacific Health">Pacific Health</a></li>
                </ul>
            </div>
        </div>
    </div>
</li>
<li role="menuitem">
    <a href="#" class="link">Compre por Objetivo</a>
    <div class="balloon">
        <span class="balloon_top" style="left: 38px;"></span>
        <p class="base-title"><strong>Compre por Objetivo</strong></p>
        <ul class="submenuitems">
            <li><a href="/" class="main-submenu-lnk">Camisas</a></li>
        </ul>
    </div>
</li>
<li role="menuitem">
    <a href="#" class="link">Compre por Categoria</a>
    <div class="balloon">
        <span class="balloon_top" style="left: 38px;"></span>
        <p class="base-title"><strong>Compre por Categoria</strong></p>
        <ul class="submenuitems">
            <li><a href="/" class="main-submenu-lnk">Camisas</a></li>
        </ul>
    </div>
</li>

CSS code:

            .main_navigation {
            margin: 0 auto;
            padding: 0;
            vertical-align: top;
            background: #304e89;
            border-left: rgba(0,0,0,0.4) 1px solid;
            border-right: rgba(255,255,255,0.1) 1px solid;
            font-size: 0;
            min-width:960px;
            width:1170px;
        }
        .main_navigation>li, .main_navigation .main_navigation-item {
            font-size: 12px;
            position: relative;
            vertical-align: top;
            float:left;
            list-style:none;
            list-style-type:none;
            font-weight:bold;
        }
        .main_navigation>li>a {
            color: #fff;
            padding: 11px 10px 9px;
            display: block;
            font-size: 13px;
            line-height: 20px;
            min-height: 20px;
            text-decoration: none;
            font-family: "DIN", sans-serif;
            background-color: #304e89;
            background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #304e89), color-stop(100%, #253e6f));
            background-image: -webkit-linear-gradient(top, #304e89, #253e6f);
            background-image: -moz-linear-gradient(top, #304e89, #253e6f);
            background-image: -ms-linear-gradient(top, #304e89, #253e6f);
            background-image: -o-linear-gradient(top, #304e89, #253e6f);
            background-image: linear-gradient(top, #304e89, #253e6f);
            border-left: rgba(255,255,255,0.1) 1px solid;
            border-right: rgba(0,0,0,0.4) 1px solid
        }
        .main_navigation>li> .link {
            text-transform:uppercase;
        }
        .main_navigation .balloon {
            display:none;
            position:absolute;
            top: 45px;
            left:0;
            z-index: 100;
        }
        .main_navigation li:hover .balloon_top {
            width: 0px;
            height: 0px;
            border: 0;
            padding: 0;
            display: block;
            position: absolute;
            top: 0
        }
        .main_navigation .balloon_top:after {
            border-bottom-color: #ececec
        }
        .balloon .base-title {
            margin: -15px -15px 0 -15px;
            padding: 10px 15px;
            background: #ececec;
            border-bottom: #ddd 1px solid
        }
        .main_navigation .balloon {
            top:50px;
            border: 1px solid #ccc;
            -webkit-border-radius:3px;
            -moz-border-radius:3px;
            border-radius:3px;
        }
        .balloon, .balloon_top, .balloon-right, .balloon-left, .balloon-bottom, .mini-modal {
            padding: 15px;
            position: relative;
            background: #fff;
            text-align: left;
            border: #ddd 1px solid;
            color: #333;
            font-size: 12px;
            font-family: Arial, sans-serif;
            text-transform: none;
            -moz-border-radius: 3px;
            -webkit-border-radius: 3px;
            border-radius: 3px
        }
        .balloon_top:after {
            width: 0;
            height: 0;
            content: " ";
            border: 7px solid rgba(255,255,255,0);
            border/*\**/: 7px solid transparent\9;
            border-bottom-color: #fff;
            border-top-width: 0;
            position: absolute;
            pointer-events: none;
            bottom: 100%;
            left: 50%;
            margin-left: -7px
        }
        .balloon_top:before {
            width: 0;
            height: 0;
            content: " ";
            border: 9px solid rgba(221,221,221,0);
            border/*\**/: 9px solid transparent\9;
            border-bottom-color: #ddd;
            border-top-width: 0;
            position: absolute;
            pointer-events: none;
            bottom: 100%;
            left: 50%;
            margin-left: -9px
        }
        .balloon-right:after {
            width: 0;
            height: 0;
            content: " ";
            border: 7px solid rgba(255,255,255,0);
            border/*\**/: 7px solid transparent\9;
            border-left-color: #fff;
            border-right-width: 0;
            position: absolute;
            pointer-events: none;
            left: 100%;
            top: 50%;
            margin-top: -7px
        }
        .balloon-right:before {
            width: 0;
            height: 0;
            content: " ";
            border: 9px solid rgba(221,221,221,0);
            border/*\**/: 9px solid transparent\9;
            border-left-color: #ddd;
            border-right-width: 0;
            position: absolute;
            pointer-events: none;
            left: 100%;
            top: 50%;
            margin-top: -9px
        }
        .balloon-left:after {
            width: 0;
            height: 0;
            content: " ";
            border: 7px solid rgba(255,255,255,0);
            border/*\**/: 7px solid transparent\9;
            border-right-color: #fff;
            border-left-width: 0;
            position: absolute;
            pointer-events: none;
            top: 50%;
            right: 100%;
            margin-top: -7px
        }
        .balloon-left:before {
            width: 0;
            height: 0;
            content: " ";
            border: 9px solid rgba(221,221,221,0);
            border/*\**/: 9px solid transparent\9;
            border-right-color: #ddd;
            border-left-width: 0;
            position: absolute;
            pointer-events: none;
            top: 50%;
            right: 100%;
            margin-top: -9px
        }
        .balloon-bottom:after {
            width: 0;
            height: 0;
            content: " ";
            border: 7px solid rgba(255,255,255,0);
            border/*\**/: 7px solid transparent\9;
            border-top-color: #fff;
            border-bottom-width: 0;
            position: absolute;
            pointer-events: none;
            top: 100%;
            left: 50%;
            margin-left: -7px
        }
        .balloon-bottom:before {
            width: 0;
            height: 0;
            content: " ";
            border: 9px solid rgba(221,221,221,0);
            border/*\**/: 9px solid transparent\9;
            border-top-color: #ddd;
            border-bottom-width: 0;
            position: absolute;
            pointer-events: none;
            top: 100%;
            left: 50%;
            margin-left: -9px
        }
        .main_navigation .balloon_big {
            width:1000px;
        }
        .main_navigation .balloon .submenuitems {
            list-style:none;
            list-style-type:none;
            padding:10px 0 0 0;
            margin:0;
        }
        .main_navigation .balloon .menucolumns {
            border:1px dashed #000;
            clear:both;
            overflow:hidden;
        }
        .main_navigation .balloon .menucolumns .column1 {
            float:left;
            min-width:150px;
            max-width:200px;
        }

Thank you guys, any help will be appreciated!

标签: html css menu
2条回答
▲ chillily
2楼-- · 2019-07-08 06:34

Pure-CSS solution: Fiddle

Use .main_navigation>li:hover .balloon instead of .main_navigation>li a:hover + .balloon, ensures balloon stays open even if mouse is over balloon.

Then add,

.balloon:before {
    display: block;
    content: "";
    width: 100%;
    height: 10px;
    position: absolute;
    top: -10px;
    left: 0;
}

Adds an invisible block between the menu and the balloon.

查看更多
走好不送
3楼-- · 2019-07-08 06:47

You have a gap between your menu and the dropdown, when the mouse hits that you lose hover.

If you want to keep that visual feel, use https://github.com/briancherne/jquery-hoverIntent

Or if you just want to try a smaller bit of js, adapt How to delay hiding of a menu with Jquery Dropdown Menu? like the following (you can change the 1000 to a larger or smaller value).

$('.main_navigation a').bind('mouseout', function()
{
    var menu = this;
    setTimeout(function()
    {
        $(menu).parents('li').children('ul').hide();
    }, 1000);
});
查看更多
登录 后发表回答