【发布时间】:2022-01-12 02:15:07
【问题描述】:
点击汉堡菜单时,我已经有了动画。我想做的是:在点击汉堡包 css/javascripted <button>。我想要另一堂课;我的.dropdownMenu 类将它的opacity 更改为1)。比如:
$(document).on('click', '.hamburger', function () {
$(this).toggleClass('.dropdownMenu'.style.opacity = 1);
});
const hamburger = document.querySelector('.hamburger');
hamburger.addEventListener('click', function() {
this.classList.toggle('is-active')
})
body {
background : grey;
}
.hamburger {
display : block;
position : relative;
z-index : 1;
user-select : none;
appearance : none;
border : none;
outline : none;
background : none;
cursor : pointer;
}
.hamburger span {
display : block;
width : 33px;
height : 4px;
margin-bottom : 5px;
position : relative;
background-color : #eeeeee;
border-radius : 6px;
z-index : 1;
transform-origin : 0 0;
transition : 0.4s;
}
.hamburger:hover span:nth-child(2) {
transform : translateX(10px);
background-color : #8c38ff;
}
.hamburger.is-active span:nth-child(1) {
transform : translate(0px, -2px) rotate(45deg);
background-color : #8c38ff;
}
.hamburger.is-active span:nth-child(2) {
opacity : 0;
transform : translateX(15px);
}
.hamburger.is-active span:nth-child(3) {
transform : translate(-3px, 3px) rotate(-45deg);
background-color : #8c38ff;
}
.hamburger.is-active:hover span {
background-color : #8c38ff;
}
.menu {
display : none;
flex : 1 1 0%;
justify-content : center;
margin : 0 -16;
letter-spacing : 2px;
}
.menu a {
color : ivory;
margin : 0 16;
font-weight : 600;
text-decoration : none;
transition : 0.4s;
padding : 8px 16px;
border-radius : 99px;
}
.menu a.is-active,
.menu a:hover {
background-color : #8c38ff;
}
.dropdown {
position : relative;
}
.dropdownMenu {
position : absolute;
z-index : 100;
right : -2rem;
top : calc(100% + 1.36rem);
border-bottom : 3px solid #8c38ff;
border-left : 3px solid #8c38ff;
background-color : #212121;
border-radius : 0 0 15px 15px;
padding : 20px;
padding-right : 50px;
opacity : 0;
}
.dropdownMenu a {
color : ivory;
text-decoration : none;
font-size : 20px;
opacity : 1;
}
<nav>
<div class="container">
<h1>Company Name</h1>
<div class="menu">
<a href="#" class="is-active">Events</a>
<a href="#">About</a>
<a href="#">Login</a>
<a href="#">Dashboard</a>
</div>
<div class="dropdown">
<button class="hamburger">
<span></span>
<span></span>
<span></span>
</button>
<div class="dropdownMenu">
<a href="#" class="link">Events</a>
<a href="#" class="link">About</a>
<a href="#" class="link">Login</a>
<a href="#" class="is-active">Dashboard</a>
</div>
</div>
</div>
</nav>
【问题讨论】:
-
一般用js来设置css属性不是一个好主意。我建议对所需元素使用 opacity = 1 的类并切换它。至于在点击事件上改变它,你应该没有js的问题。说这部分,您可以在其中监听点击并切换类,为您希望为其设置不透明度的第二个元素添加另一个切换。您提供的 jquery 部分不起作用: $(this).toggleClass('.dropdownMenu'.style.opacity = 1);当你切换类时,它不应该有“。”在其中,style.opacity 不是类的一部分
标签: javascript html jquery css