我正在尝试创建一个汉堡菜单,在该菜单中,单击它后,它会变成一个关闭(" x"(按钮。所以我为汉堡菜单汇总了此HTML
<div id="mobile-nav">
<div class="menuContainer">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
<nav>
<a href='#'>Vote</a>
<a href='#'>Search</a>
<a href='#'>Contact</a>
</nav>
</div>
和这些样式
.menuContainer {
display: inline-block;
cursor: pointer;
}
.bar1, .bar2, .bar3 {
width: 25px;
height: 4px;
background-color: #333;
margin: 5px 0;
transition: 0.4s;
}
.change .bar1 {
-webkit-transform: rotate(-45deg) translate(-9px, 6px);
transform: rotate(-45deg) translate(-9px, 6px);
}
.change .bar2 {opacity: 0;}
.change .bar3 {
-webkit-transform: rotate(45deg) translate(-8px, -8px);
transform: rotate(45deg) translate(-8px, -8px);
}
但是,当我单击按钮时," x"似乎在右侧切断-https://jsfiddle.net/8jvrrcqq/3/。即使在左侧和右侧,我该怎么做才能制作X?
您只需要稍微调整翻译值即可。还要考虑您可以使用translateY
和rotateZ
,因为您只关心这些动画。
$('.close-btn').click(function() {
$('.responsive-menu').removeClass('expand')
$('.menu-btn').removeClass('btn-none')
})
$('.menuContainer').click(function() {
$(this).toggleClass('change');
$('nav').toggleClass('nav-open');
});
#mobile-nav {
display: block;
}
.menuContainer {
display: inline-block;
cursor: pointer;
}
.bar1,
.bar2,
.bar3 {
width: 25px;
height: 4px;
background-color: #333;
margin: 5px 0;
transition: 0.4s;
}
.change .bar1 {
transform: translateY(9px) rotateZ(45deg);
}
.change .bar2 {
opacity: 0;
}
.change .bar3 {
transform: translateY(-9px) rotateZ(-45deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="mobile-nav">
<div class="menuContainer">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
</div>
您的transform: translate
金额需要调整。我在下面调整了它们;随意根据需要进行更多调整。
$('.close-btn').click(function() {
$('.responsive-menu').removeClass('expand')
$('.menu-btn').removeClass('btn-none')
})
$('.menuContainer').click(function() {
$(this).toggleClass('change');
$('nav').toggleClass('nav-open');
});
#mobile-nav {
display: block;
}
.menuContainer {
display: inline-block;
cursor: pointer;
}
.bar1,
.bar2,
.bar3 {
width: 25px;
height: 4px;
background-color: #333;
margin: 5px 0;
transition: 0.4s;
}
.change .bar1 {
-webkit-transform: rotate(-45deg) translate(-5px, 6px);
transform: rotate(-45deg) translate(-8px, 4px);
}
.change .bar2 {
opacity: 0;
}
.change .bar3 {
-webkit-transform: rotate(45deg) translate(-8px, -8px);
transform: rotate(45deg) translate(-9px, -5px);
}
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<div id="mobile-nav">
<div class="menuContainer">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
<nav>
<a href='#'>Vote</a>
<a href='#'>Search</a>
<a href='#'>Contact</a>
</nav>
</div>