简体   繁体   中英

Multiple drop-downs on a single container without interfering with each other

I created a drop-down in a navigation bar, If I have one of them it's fine of course, but if I have multiple drop-downs It doesn't work as intended. If I click on the first drop-down, it opens, but then if I also click on the second drop-down after it opened, both of them opens without closing the other one that is not clicked, how do I implement such a functionality?

Here's my current code:

HTML:

<nav class="navbar">
    <ul class="navbar-links">
        <li class="navbar-link"><a href="#">Home</a></li>
        <li class="navbar-link"><a href="#">About</a></li>
        <li class="navbar-link dropdown">
            <a href="#">
            Projects
            <i class="fa fa-caret-down" aria-hidden="true"></i>
            </a>
            <div class="dropdown-content">
                <a href="#">Shanary</a>
                <a href="#">Physics Solver</a>
                <a href="#">A simple blog</a>
            </div>
        </li>
        <li class="navbar-link dropdown">
            <a href="#">
            Projects
            <i class="fa fa-caret-down" aria-hidden="true"></i>
            </a>
            <div class="dropdown-content">
                <a href="#">Something else</a>
                <a href="#">Text Editor</a>
                <a href="#">A social Network</a>
            </div>
        </li>
    </ul>
</nav>

CSS:

.navbar {
    background-color: #3A506B;
    border-bottom: 1px solid #cccccc;
    box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2);
}
.navbar-links {
    margin: 0;
    font-size: 16px;
    list-style-type: none;
}
.navbar-link,
.dropdown-content a {
    display: inline-block;
    padding: 20px 0px;
    transition: background 0.1s ease-in-out;
}
.navbar-link:hover,
.dropdown-content a:hover {
    background: #3f5775;
}
.navbar-link a {
    padding: 20px 10px;
    color: #5DD39E;
    text-decoration: none;
}
.dropdown {
    position: relative;
    display: inline-block;
}
.dropdown-content {
    display: none;
    position: absolute;
    top: 100%;
    min-width: 160px;
    padding: 10px;
    z-index: 1;
    background-color: #3A506B;
}
.dropdown-content a {
    display: block;
}
body {
    font-family: 'Open Sans', Arial, sans-serif;
    background: #FFFFFF;
    color: #16302B;
    margin: 0px;
}
.visible {
    display: block;
}

and so far this is my JavaScript file:

$('.dropdown').click(function() {
    $(this).find('.dropdown-content').toggleClass('visible');
});

Also here's the JSFiddle .

The problem is that, both of them open without closing the other one, here's an image of what is happening:

情况形象

You will have to loop through all targets to determine whether they are the current target or not and remove visibility

See snippet below

 $('.dropdown').click(function(e) { $(this).find('.dropdown-content').toggleClass('visible'); var that = this; $('.dropdown').each(function() { if (that != this) { $(this).find('.dropdown-content').removeClass('visible'); } }) }); 
 .navbar { background-color: #3A506B; border-bottom: 1px solid #cccccc; box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2); } .navbar-links { margin: 0; font-size: 16px; list-style-type: none; } .navbar-link, .dropdown-content a { display: inline-block; padding: 20px 0px; transition: background 0.1s ease-in-out; } .navbar-link:hover, .dropdown-content a:hover { background: #3f5775; } .navbar-link a { padding: 20px 10px; color: #5DD39E; text-decoration: none; } .dropdown { position: relative; display: inline-block; } .dropdown-content { display: none; position: absolute; top: 100%; min-width: 160px; padding: 10px; z-index: 1; background-color: #3A506B; } .dropdown-content a { display: block; } body { font-family: 'Open Sans', Arial, sans-serif; background: #FFFFFF; color: #16302B; margin: 0px; } .visible { display: block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <nav class="navbar"> <ul class="navbar-links"> <li class="navbar-link"><a href="#">Home</a></li> <li class="navbar-link"><a href="#">About</a></li> <li class="navbar-link dropdown"> <a href="#"> Projects <i class="fa fa-caret-down" aria-hidden="true"></i> </a> <div class="dropdown-content"> <a href="#">Shanary</a> <a href="#">Physics Solver</a> <a href="#">A simple blog</a> </div> </li> <li class="navbar-link dropdown"> <a href="#"> Projects <i class="fa fa-caret-down" aria-hidden="true"></i> </a> <div class="dropdown-content"> <a href="#">Something else</a> <a href="#">Text Editor</a> <a href="#">A social Network</a> </div> </li> </ul> </nav> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM