英文:
How can I add a smooth transition effect to a navbar that appears and disappears with JavaScript?
问题
我正在尝试通过单击按钮元素时使用一些JS使导航栏出现和消失。我成功做到了这一点,但我想添加一个过渡效果,使导航栏平滑地弹出。我有点困惑,不太知道从哪里开始。
抱歉,如果我的问题表达不清楚。
这是我的HTML部分:
<section class="main-container">
<header class="main-header"></header>
<button class="displayer">Click Here</button>
</section>
我的CSS:
.main-container {
width: 100vw;
height: 100vh;
}
.main-header {
width: 100%;
height: 4em;
background-color: black;
position: absolute;
top: 0;
}
我编写的JS部分:
const remover = document.querySelector(".displayer");
function vanish() {
const navBar = document.querySelector(".main-header");
const display = getComputedStyle(navBar).display;
if (display === "none") {
navBar.style.display = "block";
} else {
navBar.style.display = "none";
}
}
remover.addEventListener("click", vanish);
如果您需要任何进一步的帮助,请告诉我。
英文:
I am trying to make a navbar appear and disappear with some JS when clicking on a button element. I managed to do that bit, but I would like to add a transition so that the navbar pops up smoothly. I am a bit confused and don't quite know where to start.
Sorry if my question is not well formulated.
This is my html bit
<section class="main-container">
<header class="main-header"></header>
<button class="displayer">Click Here</button>
</section>
My CSS
.main-container {
width: 100vw;
height: 100vh;
}
.main-header {
width: 100%;
height: 4em;
background-color: black;
position: absolute;
top: 0;
}
And the bit of JS I wrote
const remover = document.querySelector(".displayer");
function vanish() {
const navBar = document.querySelector(".main-header");
const display = getComputedStyle(navBar).display;
if (display === "none") {
navBar.style.display = "block";
} else {
navBar.style.display = "none";
}
}
remover.addEventListener("click", vanish);
答案1
得分: 0
你可以使用过渡并切换一个类,该类更改不透明度和变换。
const remover = document.querySelector(".displayer");
function vanish() {
const navBar = document.querySelector(".main-header");
navBar.classList.toggle('show');
}
remover.addEventListener("click", vanish);
.main-header {
width: 100%;
height: 4em;
background-color: black;
position: absolute;
top: 0;
transition: all .5s;
transform: translateY(-100px);
opacity: 0;
}
.main-header.show {
transform: none;
opacity: 1;
}
html,body{margin: 0}
<header class="main-header show"></header>
<button class="displayer" style="margin-top: 5em;">Click Here</button>
英文:
You could use a transition and toggle a class that changes opacity and transform.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const remover = document.querySelector(".displayer");
function vanish() {
const navBar = document.querySelector(".main-header");
navBar.classList.toggle('show');
}
remover.addEventListener("click", vanish);
<!-- language: lang-css -->
.main-header {
width: 100%;
height: 4em;
background-color: black;
position: absolute;
top: 0;
transition: all .5s;
transform: translateY(-100px);
opacity: 0;
}
.main-header.show {
transform: none;
opacity: 1;
}
html,body{margin: 0}
<!-- language: lang-html -->
<header class="main-header show"></header>
<button class="displayer" style="margin-top: 5em;">Click Here</button>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论