英文:
How can I get a CSS animation to work in reverse?
问题
我正在尝试制作菜单的动画效果。菜单的打开动画运行正常,但我无法让菜单关闭动画生效。
菜单成功打开,但我不知道如何使用MenuClose设置来关闭它。
<input id="MenuButton" type="button" value="Menu" onclick="showMenu();" title="Menu" />
<div id="MenuContainer"></div>
function showMenu() {
const el = document.getElementById("MenuContainer");
el.classList.toggle('MenuOpen');
}
#MenuContainer {
height: 0px;
left: 10px;
overflow: hidden;
padding: 5px 0px 0px 0px;
position: relative;
top: 20px;
width: 0px;
}
.MenuOpen {
animation-direction: normal;
animation-duration: 4s;
animation-fill-mode: forwards;
animation-name: Menu;
}
.MenuClose {
animation-direction: reverse;
animation-duration: 2s;
animation-fill-mode: forwards;
animation-name: Menu;
}
@keyframes Menu {
from {
height: 0px;
width: 0px;
}
to {
height: 100px;
width: 300px;
}
}
英文:
I am trying to animate my menu. The menu open animation works fine, but I can't get the menu close animation to work.
The menu opens fine, but I don't know how to make it close with the MenuClose settings.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function showMenu() {
const el = document.getElementById("MenuContainer");
el.classList.toggle('MenuOpen');
}
<!-- language: lang-css -->
#MenuContainer {
height: 0px;
left: 10px;
overflow: hidden;
padding: 5px 0px 0px 0px;
position: relative;
top: 20px;
width: 0px;
}
.MenuOpen {
animation-direction: normal;
animation-duration: 4s;
animation-fill-mode: forwards;
animation-name: Menu;
}
.MenuClose {
animation-direction: reverse;
animation-duration: 2s;
animation-fill-mode: forwards;
animation-name: Menu;
}
@keyframes Menu {
from {
height: 0px;
width: 0px;
}
to {
height: 100px;
width: 300px;
}
}
<!-- language: lang-html -->
<input id="MenuButton" type="button" value="Menu" onclick="showMenu();" title="Menu" />
<div id="MenuContainer"></div>
<!-- end snippet -->
答案1
得分: 1
以下是翻译好的部分:
2个主要要点:
- 通过切换 MenuOpen 来... 什么也不做,它会打开菜单并关闭它。
- 最好有打开动画和关闭动画,更容易控制。
function showMenu() {
const el = document.getElementById("MenuContainer");
if (el.classList.contains('MenuOpen')) {
el.classList.add('MenuClose');
el.classList.remove('MenuOpen');
} else {
el.classList.add('MenuOpen');
el.classList.remove('MenuClose');
}
}
#MenuContainer {
height: 0px;
left: 10px;
overflow: hidden;
padding: 5px 0px 0px 0px;
position: relative;
top: 20px;
width: 0px;
background-color: #3388ff;
}
.MenuOpen {
animation-duration: 4s;
animation-fill-mode: forwards;
animation-name: MenuOpen;
}
.MenuClose {
animation-duration: 2s;
animation-fill-mode: forwards;
animation-name: MenuClose;
}
@keyframes MenuOpen {
from {
height: 0px;
width: 0px;
}
to {
height: 100px;
width: 300px;
}
}
@keyframes MenuClose {
from {
height: 100px;
width: 300px;
}
to {
height: 0px;
width: 0px;
}
}
<input id="MenuButton" type="button" value="Menu" onclick="showMenu();" title="Menu" />
<div id="MenuContainer"></div>
英文:
2 main points:
- you toggle MenuOpen with... nothing, it puts menuopen and remove it
- better to have open animation and close animation, eas
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function showMenu() {
const el = document.getElementById("MenuContainer");
if (el.classList.contains('MenuOpen')) {
el.classList.add('MenuClose');
el.classList.remove('MenuOpen');
} else {
el.classList.add('MenuOpen');
el.classList.remove('MenuClose');
}
}
<!-- language: lang-css -->
#MenuContainer {
height: 0px;
left: 10px;
overflow: hidden;
padding: 5px 0px 0px 0px;
position: relative;
top: 20px;
width: 0px;
background-color: #3388ff;
}
.MenuOpen {
animation-duration: 4s;
animation-fill-mode: forwards;
animation-name: MenuOpen;
}
.MenuClose {
animation-duration: 2s;
animation-fill-mode: forwards;
animation-name: MenuClose;
}
@keyframes MenuOpen {
from {
height: 0px;
width: 0px;
}
to {
height: 100px;
width: 300px;
}
}
@keyframes MenuClose {
from {
height: 100px;
width: 300px;
}
to {
height: 0px;
width: 0px;
}
}
<!-- language: lang-html -->
<input id="MenuButton" type="button" value="Menu" onclick="showMenu();" title="Menu" />
<div id="MenuContainer"></div>
<!-- end snippet -->
ier to control
答案2
得分: 0
你可以使用if else语句和replace函数来切换打开和关闭动画。
// 获取菜单容器
const el = document.getElementById("MenuContainer");
// 切换菜单的打开/关闭动画的函数
function showMenu() {
// 检查菜单容器是否包含"MenuOpen"类
if (el.classList.contains('MenuOpen')) {
// 用"MenuClose"类替换"MenuOpen"类
el.classList.replace('MenuOpen', 'MenuClose');
} else {
// 将"MenuOpen"类添加到菜单容器
el.classList.add('MenuOpen');
}
}
希望这有所帮助。这只是众多解决方案中的一个。
<details>
<summary>英文:</summary>
You can use if else statement and replace function to toggle the open close animation.
//Get the menu container
const el = document.getElementById("MenuContainer");
// Function to toggle the menu open/close animation
function showMenu() {
// Check if the menu container has the 'MenuOpen' class
if (el.classList.contains('MenuOpen')) {
// Replace the 'MenuOpen' class with 'MenuClose'
el.classList.replace('MenuOpen', 'MenuClose');
} else {
// Add the 'MenuOpen' class to the menu container
el.classList.add('MenuOpen');
}
}
Hope this will help. This is just one solution of many ways.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论