javascript - 响应式菜单在网站上不起作用
问题描述
我对响应式菜单进行了编码,但它不起作用。我的期望:当你点击汉堡菜单时,菜单会变成关闭图标,全屏菜单打开(背景->不透明度.5)。
我不确定错误在哪里。CSS?还是js?你有什么建议吗?任何帮助表示赞赏。太感谢了。
$(document).ready(function() {
let hamburgerClicked = false;
$('#toggle').on("click", function() {
if (hamburgerClicked == false) {
$(".top").css('transform', 'translate (11px) translateX(0) rotate(45deg)');
$(".bottom").css('transform', 'translateY(-11px) translateX(0) rotate(-45deg)');
hamburgerClicked = true;
} else {
$(".top").css('transform', 'translate (0px) translateX(0) rotate(0deg)');
$(".bottom").css('transform', 'translateY (0px) translateX(0) rotate(0deg)');
hamburgerClicked = false;
}
$("#overlay").toggleClass('active');
$('#overlay').toggleClass('open');
});
});
.button-container {
position: fixed;
top: 5%;
right: 2%;
height: 27px;
width: 35px;
cursor: pointer;
z-index: 100;
transition: opacity .25s ease;
opacity: 1;
content: "";
}
.button-container:hover {
opacity: .7;
}
.top:active {
transform: translate(11px) translateX(0) rotate(45deg);
}
.middle:active {
opacity: 0;
}
.bottom:active {
transform: translateY(-11px) translateX(0) rotate(-45deg);
}
span {
background: #fff;
border: none;
height: 4px;
width: 32px;
position: absolute;
top: 0;
left: 0;
transition: all .35s ease;
cursor: pointer;
border-radius: 3px;
}
.middle {
top: 10px;
}
.bottom {
top: 20px;
}
.overlay {
z-index: 500;
position: fixed;
background: rgba(0, 0, 0, 0.6);
top: 0;
left: 0;
width: 5%;
height: 0%;
opacity: .6;
visibility: hidden;
transition: opacity .35s, visibility .35s, height .35s;
}
li {
animation: fadeInRight .5s ease forwards;
animation-delay: .35s;
}
li:nth-of-type(2) {
animation-delay: .4s;
}
li:nth-of-type(3) {
animation-delay: .45s;
}
li:nth-of-type(4) {
animation-delay: .50s;
}
nav {
position: relative;
height: 70%;
top: 20%;
transform: translateY(-50%);
font-size: 0.8em;
}
ul {
list-style: none;
padding: 0;
margin: 0 auto;
display: inline-block;
position: relative;
height: 100%;
}
li {
display: block;
height: 25%;
min-height: 50px;
position: relative;
opacity: 0;
}
a {
display: block;
position: relative;
text-decoration: none;
overflow: hidden;
}
a:hover {
transform: scale(1);
}
a:hover:after,
a:focus:after,
a:active:after {
width: 30%;
}
a:after {
content: '';
position: absolute;
bottom: 0;
left: 50%;
width: 0%;
transform: translateX(-50%);
height: 3px;
background: rgba(0, 0, 0, 0.6);
transition: .35s;
}
@keyframes fadeInRight {
0% {
opacity: 0;
left: 20%;
}
100% {
opacity: 1;
left: 0;
}
}
.active {
visibility: visible;
}
<div class="button-container" id="toggle">
<span class="top"></span>
<span class="middle"></span>
<span class="bottom"></span>
</div>
<div class="overlay" id="overlay">
<nav class="overlay-menu">
<ul>
<li><a href="#constellation">Home</a></li>
<li><a href="#what is constellations?">About</a></li>
<li><a href="#12 constellations">12 Constellations</a></li>
<li><a href="#">Stargazing</a></li>
</ul>
</nav>
</div>
</header>
解决方案
您的 Javascript 代码中有错误。
当您更改 .top span 的 css 时,您使用的是“translate”而不是“translateY”。
$(".top").css('transform', 'translate (11px) translateX(0) rotate(45deg)'); // In if condition
$(".top").css ('transform', 'translate (0px) translateX(0) rotate(0deg)'); // In else condition
这应该是:
$(".top").css('transform', 'translateY(11px) translateX(0) rotate(45deg)'); // In if condition
$(".top").css ('transform', 'translateY(0px) translateX(0) rotate(0deg)'); // In else condition
此外,当单击按钮时,您应该切换中间跨度的可见性,使按钮看起来像一个十字。
$(".middle").css("display", "none"); // In if condition
$(".middle").css("display", ""); // In else condition
这是关于 JSFiddle 的工作演示:https ://jsfiddle.net/0p9faz7m/
推荐阅读
- javascript - 如何在不知道 NextJS 上的静态路径的情况下生成动态页面?
- linux - 如何存储 find 生成的文件,即实际文件和内容作为文件而不是 find 的控制台输出?
- lua - 如何使用 IUP THEME 属性/DEFAULTTHEME 全局属性
- php - 在 Laravel 中以同名保存会话安全吗?
- reactjs - 如何在不刷新页面的情况下在 ReactJS 中重新加载?
- c++14 - 为什么我在二叉树的对角遍历代码中出现 SIGBART 错误?
- winapi - ShowWindow 调用可防止通过 SetLayeredWindowAttribute 更改 alpha
- bash - 如何找出一个数字与文件中下一个相同数字的出现之间有多少行?
- angular - 如果条件失败,Angular 11
- reactjs - 反应原生 setInterval useState