I have created my dropdown menu to open on hover of the icon, but I want it to actually open on click instead of hover and close when clicked outside the menu. The icon is a simple burger menu icon on the left hand side of the navbar which opens a dropbar that is attached to the left hand side of the page. How would I achieve what I want to with minimal change to my code?
.search {
display: block;
margin-left: 1300px;
margin-right: auto;
margin-bottom: auto;
margin-top: -30px;
cursor: pointer;
}
.burger-menu {
display: block;
margin-left: 100px;
margin-right: auto;
margin-bottom: auto;
margin-top: 1px;
cursor: pointer;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #000000;
min-width: 250px;
z-index: 1;
}
.dropdown-content a {
color: white;
padding: 30px 16px;
text-align: center;
text-decoration: none;
display: block;
box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
font-size: 24px;
font-family: "Gill Sans", "Gill Sans MT", "Myriad Pro", "DejaVu Sans Condensed", Helvetica, Arial, "sans-serif";
}
.dropdown-content a:hover {
background-color: #ffffff;
color: black;
}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown:hover .dropbtn {
background-color: #ffffff;
}
<nav>
<div class="dropdown">
<input type="image" width="30" height="30" src="images/burger-menu.png" class="burger-menu" />
<div class="dropdown-content">
<a href="#">HEADWEAR</a>
<a href="#">HOODIES/JACKETS/LONG SLEEVES</a>
<a href="#">SHIRTS</a>
<a href="#">BOTTOMS</a>
<a href="#">TOTE BAGS</a>
</div>
</div>
<img class="search" src="images/search-icon.png" width="30" height="30">
</nav>
you need to use
$('.burger-menu').click(function(){
$('.dropdown-content').toggleClass('show');
});
$(document).click(function(event) {
if(!$(event.target).closest('.burger-menu').length) {
if($('.dropdown-content').is(":visible")) {
$('.dropdown-content').toggleClass('show');
}
}
});
$(document).ready(function(){
$('.burger-menu').click(function(){
$('.dropdown-content').toggleClass('show');
});
$(document).click(function(event) {
if(!$(event.target).closest('.burger-menu').length) {
if($('.dropdown-content').is(":visible")) {
$('.dropdown-content').toggleClass('show');
}
}
});
});
.search {
display: block;
margin-left: 1300px;
margin-right: auto;
margin-bottom: auto;
margin-top: -30px;
cursor: pointer;
}
.burger-menu {
display: block;
margin-left: 100px;
margin-right: auto;
margin-bottom: auto;
margin-top: 1px;
cursor: pointer;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #000000;
min-width: 250px;
z-index: 1;
}
.dropdown-content a {
color: white;
padding: 30px 16px;
text-align: center;
text-decoration: none;
display: block;
box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
font-size: 24px;
font-family: "Gill Sans", "Gill Sans MT", "Myriad Pro", "DejaVu Sans Condensed", Helvetica, Arial, "sans-serif";
}
.dropdown-content a:hover {
background-color: #ffffff;
color: black;
}
.dropdown .dropdown-content.show {
display: block;
}
.dropdown:hover .dropbtn {
background-color: #ffffff;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<nav>
<div class="dropdown">
<i class="fa fa-bars fa-2x burger-menu" ></i>
<div class="dropdown-content">
<a href="#">HEADWEAR</a>
<a href="#">HOODIES/JACKETS/LONG SLEEVES</a>
<a href="#">SHIRTS</a>
<a href="#">BOTTOMS</a>
<a href="#">TOTE BAGS</a>
</div>
</div>
<img class="search" src="images/search-icon.png" width="30" height="30">
</nav>
The only way I know of to do this with just CSS is using a checkbox to transition up/down the menu. I see you're using an input tag, so maybe there's another pure CSS way to do this.
Using jQuery, you can get the desired effect using slideToggle()
$(".icon").on("click", function(){
$('.dropdown-content').slideToggle();
});
.search {
display: block;
margin-left: 1300px;
margin-right: auto;
margin-bottom: auto;
margin-top: -30px;
cursor: pointer;
}
.burger-menu {
display: block;
margin-left: 100px;
margin-right: auto;
margin-bottom: auto;
margin-top: 1px;
cursor: pointer;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #000000;
min-width: 250px;
z-index: 1;
}
.dropdown-content a {
color: white;
padding: 30px 16px;
text-align: center;
text-decoration: none;
display: block;
box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
font-size: 24px;
font-family: "Gill Sans", "Gill Sans MT", "Myriad Pro", "DejaVu Sans Condensed", Helvetica, Arial, "sans-serif";
}
.dropdown:hover .dropbtn {
background-color: #ffffff;
}
/*# sourceMappingURL= main.css.map */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
<div class="dropdown">
<h3 class="icon">MY ICON</h3>
<div class="dropdown-content">
<a href="#">HEADWEAR</a>
<a href="#">HOODIES/JACKETS/LONG SLEEVES</a>
<a href="#">SHIRTS</a>
<a href="#">BOTTOMS</a>
<a href="#">TOTE BAGS</a>
</div>
</div>
<img class="search" src="images/search-icon.png" width="30" height="30">
</nav>