我已经创建了我的下拉菜单以在悬停图标时打开,但我希望它在单击时实际打开,而不是在菜单外单击时悬停并关闭。该图标是导航栏左侧的简单汉堡菜单图标,可打开附加到页面左侧的下拉栏。如何以最少的代码更改实现我想要的目标?
.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>
我知道仅使用
CSS 执行此操作的唯一方法是使用复选框向上/向下转换菜单。 我看到您正在使用输入标签,所以也许还有另一种纯 CSS 方法可以做到这一点。
使用 jQuery,您可以使用 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>
你需要使用
$('.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>