单击另一个菜单项时如何切换菜单
How to toggle up menu when another menu item is clicked
我正在使用 Genesis Framework 开发我自己的子主题。对于我的主题,当您单击顶级菜单项时,子菜单会在菜单下方水平切换打开。现在,要关闭它,您需要单击相同的菜单选项。如果您在第一个子菜单仍处于打开状态时单击另一个菜单项,则新的子菜单会出现在它上面,从而创建一个子菜单层。
我在这里查看了多个线程,并尝试按照其他人的建议使用 e.stopPropagation();
。我确定我用错了,因为它不起作用。
下面是我使用的代码:
jQuery(document).ready(function(){
jQuery(".nav-header-left .menu-item, .nav-header-right .menu-item").removeClass("menu-open");
jQuery(".nav-header-left .menu-item-has-children, .nav-header-right .menu-item-has-children")
.click( function( e ){
jQuery(this).find( 'ul.sub-menu:first' ).slideToggle( function() {
jQuery(this).parent().toggleClass("menu-open");
});
if ( e.target !== this ) {
return;
}
});
});
这里是一个 fiddle 显示菜单的功能:https://jsfiddle.net/mve1mrcp/15/(已经更新了几次以使其看起来更好一些)
如有任何建议,我们将不胜感激!
这是您的 fiddle 中的 JS/CSS/HTML,其中包含小的修复
jQuery(document).ready(function() {
jQuery(".nav-header-left .menu-item, .nav-header-right .menu-item").removeClass("menu-open");
jQuery(".nav-header-left .menu-item-has-children, .nav-header-right .menu-item-has-children")
.click(function(e) {
var $menuItem = jQuery(this);
console.log($menuItem)
var $nav = $menuItem.closest('.nav-header-left, .nav-header-right'); // get the nav parent
var shouldClose = $menuItem.hasClass('menu-open');
var $openMenuItem = $('.menu-open').removeClass('menu-open'); // toggle all menu items to be closed
$openMenuItem.find('ul.sub-menu:first').hide(); // hide sub-menu
if(!shouldClose){
$menuItem.find('ul.sub-menu:first').slideToggle(function() {
jQuery(this).parent().addClass("menu-open");
});
}
});
jQuery(document).click(function(){
$('.menu-open').removeClass('menu-open');
})
});
.genesis-nav-menu .menu-item {
display: inline-block;
text-align: left;
}
li {
list-style-type: none;
}
nav {
display: block;
}
.nav-header-left {
background: #000;
}
.nav-header-left .genesis-nav-menu {
text-align: left;
}
.genesis-nav-menu li a {
color: #fff;
display: block;
padding: 22px;
text-transform: uppercase;
font-weight: bold;
font-size: 12px;
}
.nav-header-left .genesis-nav-menu li > .sub-menu {
display: none;
top: 104px;
height: 100px;
background-color: #fff;
}
.genesis-nav-menu .sub-menu {
left: -9999px;
opacity: 0;
position: absolute;
-webkit-transition: opacity .4s ease-in-out;
-moz-transition: opacity .4s ease-in-out;
-ms-transition: opacity .4s ease-in-out;
-o-transition: opacity .4s ease-in-out;
transition: opacity .4s ease-in-out;
width: 200px;
z-index: 99;
}
.nav-header-left .genesis-nav-menu li > .sub-menu {
display: none;
top: 104px;
height: 100px;
background-color: #fff;
}
.nav-header-left .genesis-nav-menu > .menu-item-has-children:before {
content: "\f347";
float: right;
font: normal 12px/1 'dashicons';
height: 16px;
padding: 15px 0px 0px 0px;
text-align: right;
z-index: 9999;
cursor: pointer;
color: #fff;
}
.nav-header-left .genesis-nav-menu .menu-open .sub-menu {
border: none;
opacity: 1;
position: absolute;
left: 0;
-moz-transition: opacity .4s ease-in-out;
-ms-transition: opacity .4s ease-in-out;
-o-transition: opacity .4s ease-in-out;
-webkit-transition: opacity .4s ease-in-out;
transition: opacity .4s ease-in-out;
width: 100%;
z-index: 99;
}
.nav-header-left .genesis-nav-menu > li.menu-item-has-children > a {
pointer-events: none;
}
.nav-header-left .genesis-nav-menu li > .sub-menu li {
display: inline-block;
}
.site-header .genesis-nav-menu li li {
margin-left: 0;
}
.nav-header-left .genesis-nav-menu li > .sub-menu li a {
display: block;
width: auto;
font-weight: lighter;
font-size: 15px;
margin-top: 15px;
font-family: 'Fjalla One', sans-serif;
}
.nav-header-left .genesis-nav-menu .sub-menu a {
background-color: #fff;
color: #333;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav class="nav-header-left">
<ul id="menu-left" class="menu genesis-nav-menu">
<li id="menu-item-377" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-ancestor current-menu-parent current_page_parent current_page_ancestor menu-item-has-children menu-item-377"><a href="#" itemprop="url"><span itemprop="name">Courses</span></a>
<ul class="sub-menu">
<li id="menu-item-376" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-376"><a href="#" itemprop="url"><span itemprop="name">Ontario</span></a></li>
<li id="menu-item-388" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-388"><a href="#" itemprop="url"><span itemprop="name">Nova Scotia</span></a></li>
<li id="menu-item-374" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-374"><a href="#" itemprop="url"><span itemprop="name">Driving Ranges</span></a></li>
<li id="menu-item-375" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-375"><a href="#" itemprop="url"><span itemprop="name">Daily Updates</span></a></li>
<li id="menu-item-392" class="menu-item menu-item-type-post_type menu-item-object-post menu-item-392"><a href="#" itemprop="url"><span itemprop="name">Loch March</span></a></li>
</ul>
</li>
<li id="menu-item-219" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-219"><a target="_blank" href="#" itemprop="url"><span itemprop="name">Tee Times</span></a></li>
<li id="menu-item-63" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-63"><a href="#" itemprop="url"><span itemprop="name">Memberships</span></a>
<ul class="sub-menu">
<li id="menu-item-487" class="menu-item menu-item-type-post_type menu-item-object-post menu-item-487"><a href="#" itemprop="url"><span itemprop="name">Member Guest Days</span></a></li>
<li id="menu-item-395" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-395"><a href="#" itemprop="url"><span itemprop="name">2018 Memberships</span></a></li>
<li id="menu-item-396" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-396"><a href="#" itemprop="url"><span itemprop="name">Add-On Programs</span></a></li>
<li id="menu-item-398" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-398"><a href="#" itemprop="url"><span itemprop="name">Corporate Programs</span></a></li>
<li id="menu-item-397" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-397"><a href="#" itemprop="url"><span itemprop="name">FAQs</span></a></li>
</ul>
</li>
</ul>
</nav>
我正在使用 Genesis Framework 开发我自己的子主题。对于我的主题,当您单击顶级菜单项时,子菜单会在菜单下方水平切换打开。现在,要关闭它,您需要单击相同的菜单选项。如果您在第一个子菜单仍处于打开状态时单击另一个菜单项,则新的子菜单会出现在它上面,从而创建一个子菜单层。
我在这里查看了多个线程,并尝试按照其他人的建议使用 e.stopPropagation();
。我确定我用错了,因为它不起作用。
下面是我使用的代码:
jQuery(document).ready(function(){
jQuery(".nav-header-left .menu-item, .nav-header-right .menu-item").removeClass("menu-open");
jQuery(".nav-header-left .menu-item-has-children, .nav-header-right .menu-item-has-children")
.click( function( e ){
jQuery(this).find( 'ul.sub-menu:first' ).slideToggle( function() {
jQuery(this).parent().toggleClass("menu-open");
});
if ( e.target !== this ) {
return;
}
});
});
这里是一个 fiddle 显示菜单的功能:https://jsfiddle.net/mve1mrcp/15/(已经更新了几次以使其看起来更好一些)
如有任何建议,我们将不胜感激!
这是您的 fiddle 中的 JS/CSS/HTML,其中包含小的修复
jQuery(document).ready(function() {
jQuery(".nav-header-left .menu-item, .nav-header-right .menu-item").removeClass("menu-open");
jQuery(".nav-header-left .menu-item-has-children, .nav-header-right .menu-item-has-children")
.click(function(e) {
var $menuItem = jQuery(this);
console.log($menuItem)
var $nav = $menuItem.closest('.nav-header-left, .nav-header-right'); // get the nav parent
var shouldClose = $menuItem.hasClass('menu-open');
var $openMenuItem = $('.menu-open').removeClass('menu-open'); // toggle all menu items to be closed
$openMenuItem.find('ul.sub-menu:first').hide(); // hide sub-menu
if(!shouldClose){
$menuItem.find('ul.sub-menu:first').slideToggle(function() {
jQuery(this).parent().addClass("menu-open");
});
}
});
jQuery(document).click(function(){
$('.menu-open').removeClass('menu-open');
})
});
.genesis-nav-menu .menu-item {
display: inline-block;
text-align: left;
}
li {
list-style-type: none;
}
nav {
display: block;
}
.nav-header-left {
background: #000;
}
.nav-header-left .genesis-nav-menu {
text-align: left;
}
.genesis-nav-menu li a {
color: #fff;
display: block;
padding: 22px;
text-transform: uppercase;
font-weight: bold;
font-size: 12px;
}
.nav-header-left .genesis-nav-menu li > .sub-menu {
display: none;
top: 104px;
height: 100px;
background-color: #fff;
}
.genesis-nav-menu .sub-menu {
left: -9999px;
opacity: 0;
position: absolute;
-webkit-transition: opacity .4s ease-in-out;
-moz-transition: opacity .4s ease-in-out;
-ms-transition: opacity .4s ease-in-out;
-o-transition: opacity .4s ease-in-out;
transition: opacity .4s ease-in-out;
width: 200px;
z-index: 99;
}
.nav-header-left .genesis-nav-menu li > .sub-menu {
display: none;
top: 104px;
height: 100px;
background-color: #fff;
}
.nav-header-left .genesis-nav-menu > .menu-item-has-children:before {
content: "\f347";
float: right;
font: normal 12px/1 'dashicons';
height: 16px;
padding: 15px 0px 0px 0px;
text-align: right;
z-index: 9999;
cursor: pointer;
color: #fff;
}
.nav-header-left .genesis-nav-menu .menu-open .sub-menu {
border: none;
opacity: 1;
position: absolute;
left: 0;
-moz-transition: opacity .4s ease-in-out;
-ms-transition: opacity .4s ease-in-out;
-o-transition: opacity .4s ease-in-out;
-webkit-transition: opacity .4s ease-in-out;
transition: opacity .4s ease-in-out;
width: 100%;
z-index: 99;
}
.nav-header-left .genesis-nav-menu > li.menu-item-has-children > a {
pointer-events: none;
}
.nav-header-left .genesis-nav-menu li > .sub-menu li {
display: inline-block;
}
.site-header .genesis-nav-menu li li {
margin-left: 0;
}
.nav-header-left .genesis-nav-menu li > .sub-menu li a {
display: block;
width: auto;
font-weight: lighter;
font-size: 15px;
margin-top: 15px;
font-family: 'Fjalla One', sans-serif;
}
.nav-header-left .genesis-nav-menu .sub-menu a {
background-color: #fff;
color: #333;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav class="nav-header-left">
<ul id="menu-left" class="menu genesis-nav-menu">
<li id="menu-item-377" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-ancestor current-menu-parent current_page_parent current_page_ancestor menu-item-has-children menu-item-377"><a href="#" itemprop="url"><span itemprop="name">Courses</span></a>
<ul class="sub-menu">
<li id="menu-item-376" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-376"><a href="#" itemprop="url"><span itemprop="name">Ontario</span></a></li>
<li id="menu-item-388" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-388"><a href="#" itemprop="url"><span itemprop="name">Nova Scotia</span></a></li>
<li id="menu-item-374" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-374"><a href="#" itemprop="url"><span itemprop="name">Driving Ranges</span></a></li>
<li id="menu-item-375" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-375"><a href="#" itemprop="url"><span itemprop="name">Daily Updates</span></a></li>
<li id="menu-item-392" class="menu-item menu-item-type-post_type menu-item-object-post menu-item-392"><a href="#" itemprop="url"><span itemprop="name">Loch March</span></a></li>
</ul>
</li>
<li id="menu-item-219" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-219"><a target="_blank" href="#" itemprop="url"><span itemprop="name">Tee Times</span></a></li>
<li id="menu-item-63" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-63"><a href="#" itemprop="url"><span itemprop="name">Memberships</span></a>
<ul class="sub-menu">
<li id="menu-item-487" class="menu-item menu-item-type-post_type menu-item-object-post menu-item-487"><a href="#" itemprop="url"><span itemprop="name">Member Guest Days</span></a></li>
<li id="menu-item-395" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-395"><a href="#" itemprop="url"><span itemprop="name">2018 Memberships</span></a></li>
<li id="menu-item-396" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-396"><a href="#" itemprop="url"><span itemprop="name">Add-On Programs</span></a></li>
<li id="menu-item-398" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-398"><a href="#" itemprop="url"><span itemprop="name">Corporate Programs</span></a></li>
<li id="menu-item-397" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-397"><a href="#" itemprop="url"><span itemprop="name">FAQs</span></a></li>
</ul>
</li>
</ul>
</nav>