在这个示例中,我有一个菜单项。下拉菜单的第一级工作正常,但是使用此jQuery代码的第二级效果不好。您可以看到,如果您单击Menu4.4,那么它首先将隐藏菜单并再次显示(如闪烁一个菜单)。我该如何解决或纠正此行为?
$(function() {
"use strict";
// dropdown slide in navigation
$(".dropdown").on("show.bs.dropdown", function() {
$(this).find(".dropdown-menu").first().stop(true, true).slideDown();
});
// dropdown slide in navigation
$(".dropdown").on("hide.bs.dropdown", function() {
$(this).find(".dropdown-menu").first().stop(true, true).slideUp();
});
});
.navbar-nav li .nav-link {
padding-right: .5rem;
padding-left: .5rem;
}
#navbarCollapse li.active {
background-color: #BA122B;
}
#navbarCollapse li.active a {
color: #FFF;
background-color: #BA122B;
}
#navbarCollapse .menu {
padding-top: 30px;
}
#navbarCollapse .dropdown-menu {
padding-left: 20px;
}
#navbarCollapse li a {
border-bottom: 1px solid #E3E3E3;
color: #BA122B;
-webkit-transition-duration: 0.3s;
transition-duration: 0.3s;
-webkit-transition-property: color, background-color;
transition-property: color, background-color;
-webkit-transform: perspective(1px) translateZ(0);
transform: perspective(1px) translateZ(0);
}
#navbarCollapse li a:hover {
color: #FFF;
background-color: #BA122B;
}
#navbarCollapse parent:after {}
.body {
background-color: #f6f6f6;
padding-top: 2rem;
}
.off-canvas-active .off-canvas-overlay {
opacity: 1;
visibility: visible;
}
.off-canvas-overlay {
position: fixed;
right: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, .75);
z-index: 1000;
visibility: hidden;
opacity: 0;
transition: .3s ease-in-out;
}
.offcanvas-collapse.open {
display: block;
-webkit-animation: slide-right .3s ease-out;
-moz-animation: slide-right .3s ease-out;
}
@-webkit-keyframes slide-right {
0% {
opacity: 0;
-webkit-transform: translateX(-100%);
}
100% {
opacity: 1;
-webkit-transform: translateX(0);
}
}
@-moz-keyframes slide-right {
0% {
opacity: 0;
-moz-transform: translateX(-100%);
}
100% {
opacity: 1;
-moz-transform: translateX(0);
}
}
.offcanvas-collapse {
position: fixed;
bottom: 0;
right: 0;
width: 330px;
transition-timing-function: ease-in-out;
transition-duration: .3s;
transition-property: width;
display: none;
overflow-y: auto;
}
.navbar-expand-md .navbar-toggler {
display: block!important;
}
.navbar-toggler {
border: none;
height: 40px;
}
.navbar-toggler:active,
.navbar-toggler:focus {
outline: 0;
}
.navbar-toggler .icon-bar {
display: block;
width: 30px;
height: 2px;
border-radius: 1px;
transition: .3s ease-in-out;
background-color: #BA122B;
}
.navbar-dark .navbar-toggler .icon-bar {
background: #ffffff;
}
.navbar-toggler .icon-bar:nth-of-type(1) {
transform: rotate(45deg) translate(2px, 2px);
}
.navbar-toggler .icon-bar:nth-of-type(2) {
opacity: 0;
}
.navbar-toggler .icon-bar:nth-of-type(3) {
transform: rotate(-45deg) translate(1px, -1px);
}
.navbar-toggler.collapsed .icon-bar {
margin: 5px auto;
transform: none;
opacity: 1;
}
.navbar-nav .dropdown-menu {
padding: 0;
border: none;
margin: 0;
border-radius: 0;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<nav class="offcanvas-collapse navbar open" id="navbarCollapse">
<div class="menu-body bg-white open" style="left: 1267.5px;">
<ul class="nav menu navbar-nav ml-auto text-uppercase mod-list">
<li class="item-103 default current active"><a href="#" class="nav-link">Home!</a></li>
<li class="item-111"><a href="#" class="nav-link">Menu1</a></li>
<li class="item-104 deeper parent dropdown"><a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Menu2</a>
<ul class="nav-child unstyled small dropdown-menu" style="display: none;">
<li class="item-105"><a href="#" class="nav-link">Menu2.1</a></li>
<li class="item-106"><a href="#" class="nav-link">Menu2.2</a></li>
</ul>
</li>
<li class="item-112 deeper parent dropdown"><a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Menu3</a>
<ul class="nav-child unstyled small dropdown-menu" style="display: none;">
<li class="item-114"><a href="#" class="nav-link">Menu3.1</a></li>
<li class="item-115"><a href="#" class="nav-link">Menu3.2</a></li>
<li class="item-116"><a href="#" class="nav-link">Menu3.3</a></li>
<li class="item-117"><a href="#" class="nav-link">Menu3.4</a></li>
</ul>
</li>
<li class="item-118 deeper parent dropdown"><a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Menu4</a>
<ul class="nav-child unstyled small dropdown-menu" style="display: none;">
<li class="item-119"><a href="#" class="nav-link">Menu4.1</a></li>
<li class="item-142"><a href="#" class="nav-link">Menu4.2</a></li>
<li class="item-143"><a href="#" class="nav-link">Menu4.3</a></li>
<li class="item-154 deeper parent dropdown"><a href="#" class="nav-link dep dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Menu4.4</a>
<ul class="nav-child unstyled small dropdown-menu" style="display: none;">
<li class="item-155"><a href="#" class="nav-link">Menu4.4.1</a></li>
<li class="item-156"><a href="#" class="nav-link">Menu4.4.2</a></li>
</ul>
</li>
<li class="item-157"><a href="#" class="nav-link">Menu4.5</a></li>
</ul>
</li>
<li class="item-137 deeper parent dropdown"><a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Menu5</a>
<ul class="nav-child unstyled small dropdown-menu">
<li class="item-138"><a href="#" class="nav-link">Menu5.1</a></li>
<li class="item-139"><a href="#" class="nav-link">Menu5.2</a></li>
</ul>
</li>
<li class="item-140"><a href="#" class="nav-link">Menu6</a></li>
</ul>
</div>
</nav>