【发布时间】:2021-02-26 00:21:03
【问题描述】:
我有一个基于 Webdevkit 响应式菜单的菜单。 菜单由 Jquery 控制,我正在尝试修改代码,以便在第一次单击汉堡包图标时在移动菜单上更改为“X”, 然后当点击移动菜单上的一个项目时,菜单被隐藏并且“X”变回汉堡符号,并且当点击汉堡符号时重复该过程。
我已经阅读了几篇关于此的文章,并认为我需要向 HREF 添加一个类,我已经这样做了,但未能制定出正确的 (jquery) 语句来实现上述目标。
/** code by webdevtrick ( https://webdevtrick.com ) **/
(function($) {
$(function() {
$('nav ul li a:not(:only-child)').click(function(e) {
$(this).siblings('.nav-dropdown').toggle();
$('.nav-dropdown').not($(this).siblings()).hide();
e.stopPropagation();
});
$('html').click(function() {
$('.nav-dropdown').hide();
});
/* create mobile dropdown menu */
$('#nav-toggle').click(function() {
$('nav ul').slideToggle();
});
$('a.mmenu').click(function() {
$('#nav-toggle').toggleClass('active');
$('.nav-mobile').toggleClass('hide');
});
$('#nav-toggle').on('click', function() {
this.classList.toggle('active');
});
});
})(jQuery);
<style type="text/css">
body {
margin: 0;
padding: 0;
}
{
font-family: 'Righteous', cursive;
}
.nav-bar {
height: 70px;
background: #262626;
}
.brand {
display: none;
}
.brand a img {
max-height: 70px;
}
.brand a,
.brand a:visited {
color: #ffffff;
text-decoration: none;
}
.nav-container {
max-width: 1000px;
margin: 0 auto;
}
/* position navbar items */
nav {
float: center ;
}
nav ul {
list-style: none;
margin: 0;
padding: 0;
}
nav ul li {
float: left;
position: relative;
}
nav ul li a,
nav ul li a:visited {
display: block;
padding: 0 20px;
line-height: 70px;
background: #262626;
color: #ffffff;
text-decoration: none;
}
nav ul li a:hover,
nav ul li a:visited:hover {
background: #2ab1ce;
color: #ffffff;
}
nav ul li a:not(:only-child):after,
nav ul li a:visited:not(:only-child):after {
padding-left: 4px;
content: ' ▾';
}
nav ul li ul li {
min-width: 190px;
}
nav ul li ul li a {
padding: 15px;
line-height: 20px;
}
.nav-dropdown {
position: absolute;
display: none;
z-index: 1;
box-shadow: 0 3px 12px rgba(0, 0, 0, 0.15);
}
.nav-mobile {
display: none;
position: absolute;
top: 0;
right: 0;
background: #262626;
height: 70px;
width: 70px;
}
@media only screen and (max-width: 798px) {
.nav-mobile {
display: block;
}
nav {
width: 100%;
padding: 70px 0 15px;
}
nav ul {
display: none;
}
nav ul li {
float: none;
}
nav ul li a {
padding: 15px;
line-height: 20px;
padding-left: 25%;
}
nav ul li ul li a {
padding-left: 30%;
}
.nav-dropdown {
position: static;
}
.brand {
position: absolute;
padding-left: 20px;
float: left;
line-height: 70px;
text-transform: uppercase;
font-size: 1.4em;
display: block;
}
.brand a img {
max-height: 60px;
margin-top: 5px;
display: block;
}
}
@media screen and (min-width: 799px) {
.nav-list {
display: block !important;
}
}
#nav-toggle {
position: absolute;
left: 18px;
top: 22px;
cursor: pointer;
padding: 10px 35px 16px 0px;
}
#nav-toggle span,
#nav-toggle span:before,
#nav-toggle span:after {
cursor: pointer;
border-radius: 1px;
height: 5px;
width: 35px;
background: #ffffff;
position: absolute;
display: block;
content: '';
transition: all 300ms ease-in-out;
}
#nav-toggle span:before {
top: -10px;
}
#nav-toggle span:after {
bottom: -10px;
}
#nav-toggle.active span {
background-color: transparent;
}
#nav-toggle.active span:before, #nav-toggle.active span:after {
top: 0;
}
#nav-toggle.active span:before {
transform: rotate(45deg);
}
#nav-toggle.active span:after {
transform: rotate(-45deg);
}
<html>
<head>
<meta charset="UTF-8">
<title>Responsive Dropdown nav-bar Bar</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Righteous&display=swap" rel="stylesheet">
<link rel="stylesheet" href="media/idea3.css">
</head>
<body>
<section class="nav-bar">
<div class="nav-container">
<div class="brand">
<a href="">MENU</a>
</div>
<nav>
<div class="nav-mobile"><a id="nav-toggle" href="#!"><span></span></a></div>
<ul class="nav-list">
<li>
<a class="mmenu" href="#">Home</a>
</li>
<li>
<a class="mmenu" href="#">News</a>
</li>
<li>
<a class="mmenu" href="#">Projects</a>
<ul class="nav-dropdown">
<li>
<a class="mmenu" href="#">Project 1</a>
</li>
<li>
<a class="mmenu" href="#">Project 2</a>
</li>
<li>
<a class="mmenu" href="#">Project3</a>
</li>
</ul>
</li>
<li>
<li>
<a clas="mmenu" href="#">About</a>
</li>
</ul>
</nav>
</div>
</section>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="media/idea4.js"></script>
</body>
</html>
任何关于前进道路的帮助或想法将不胜感激。
<style type="text/css">
body {
margin: 0;
padding: 0;
}
{
font-family: 'Righteous', cursive;
}
.nav-bar {
height: 70px;
background: #262626;
}
.brand {
display: none;
}
.brand a img {
max-height: 70px;
}
.brand a,
.brand a:visited {
color: #ffffff;
text-decoration: none;
}
.nav-container {
max-width: 1000px;
margin: 0 auto;
}
/* position navbar items */
nav {
float: center ;
}
nav ul {
list-style: none;
margin: 0;
padding: 0;
}
nav ul li {
float: left;
position: relative;
}
nav ul li a,
nav ul li a:visited {
display: block;
padding: 0 20px;
line-height: 70px;
background: #262626;
color: #ffffff;
text-decoration: none;
}
nav ul li a:hover,
nav ul li a:visited:hover {
background: #2ab1ce;
color: #ffffff;
}
nav ul li a:not(:only-child):after,
nav ul li a:visited:not(:only-child):after {
padding-left: 4px;
content: ' ▾';
}
nav ul li ul li {
min-width: 190px;
}
nav ul li ul li a {
padding: 15px;
line-height: 20px;
}
.nav-dropdown {
position: absolute;
display: none;
z-index: 1;
box-shadow: 0 3px 12px rgba(0, 0, 0, 0.15);
}
.nav-mobile {
display: none;
position: absolute;
top: 0;
right: 0;
background: #262626;
height: 70px;
width: 70px;
}
@media only screen and (max-width: 798px) {
.nav-mobile {
display: block;
}
nav {
width: 100%;
padding: 70px 0 15px;
}
nav ul {
display: none;
}
nav ul li {
float: none;
}
nav ul li a {
padding: 15px;
line-height: 20px;
padding-left: 25%;
}
nav ul li ul li a {
padding-left: 30%;
}
.nav-dropdown {
position: static;
}
.brand {
position: absolute;
padding-left: 20px;
float: left;
line-height: 70px;
text-transform: uppercase;
font-size: 1.4em;
display: block;
}
.brand a img {
max-height: 60px;
margin-top: 5px;
display: block;
}
}
@media screen and (min-width: 799px) {
.nav-list {
display: block !important;
}
}
#nav-toggle {
position: absolute;
left: 18px;
top: 22px;
cursor: pointer;
padding: 10px 35px 16px 0px;
}
#nav-toggle span,
#nav-toggle span:before,
#nav-toggle span:after {
cursor: pointer;
border-radius: 1px;
height: 5px;
width: 35px;
background: #ffffff;
position: absolute;
display: block;
content: '';
transition: all 300ms ease-in-out;
}
#nav-toggle span:before {
top: -10px;
}
#nav-toggle span:after {
bottom: -10px;
}
#nav-toggle.active span {
background-color: transparent;
}
#nav-toggle.active span:before, #nav-toggle.active span:after {
top: 0;
}
#nav-toggle.active span:before {
transform: rotate(45deg);
}
#nav-toggle.active span:after {
transform: rotate(-45deg);
}
<html>
<head>
<meta charset="UTF-8">
<title>Responsive Dropdown nav-bar Bar</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Righteous&display=swap" rel="stylesheet">
<link rel="stylesheet" href="media/idea3.css">
</head>
<body>
<section class="nav-bar">
<div class="nav-container">
<div class="brand">
<a href="">MENU</a>
</div>
<nav>
<div class="nav-mobile"><a id="nav-toggle" href="#!"><span></span></a></div>
<ul class="nav-list">
<li>
<a class="mmenu" href="#">Home</a>
</li>
<li>
<a class="mmenu" href="#">News</a>
</li>
<li>
<a class="mmenu" href="#">Projects</a>
<ul class="nav-dropdown">
<li>
<a class="mmenu" href="#">Project 1</a>
</li>
<li>
<a class="mmenu" href="#">Project 2</a>
</li>
<li>
<a class="mmenu" href="#">Project3</a>
</li>
</ul>
</li>
<li>
<li>
<a clas="mmenu" href="#">About</a>
</li>
</ul>
</nav>
</div>
</section>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="media/idea4.js"></script>
</body>
</html>
/** code by webdevtrick ( https://webdevtrick.com ) **/
(function($) {
$(function() {
$('nav ul li a:not(:only-child)').click(function(e) {
$(this).siblings('.nav-dropdown').toggle();
$('.nav-dropdown').not($(this).siblings()).hide();
e.stopPropagation();
});
$('html').click(function() {
$('.nav-dropdown').hide();
});
/* create mobile dropdown menu */
$('#nav-toggle').click(function() {
$('nav ul').slideToggle();
});
$('a.mmenu').click(function() {
$('#nav-toggle').toggleClass('active');
$('.nav-mobile').toggleClass('hide');
});
$('#nav-toggle').on('click', function() {
this.classList.toggle('active');
});
});
})(jQuery);
【问题讨论】: