gpt4 book ai didi

css - 你如何在显示 :none and display:block? 之间转换

转载 作者:技术小花猫 更新时间:2023-10-29 12:12:44 25 4
gpt4 key购买 nike

我正在尝试延迟移动导航元素的出现(理想情况下不使用 JS 来延迟它们的出现)直到导航覆盖完全展开(0.4 秒)。

有两种状态:1.非叠加状态:top nav左边是汉堡包菜单,右边是我的名字。 4 个导航链接被隐藏。2. 覆盖状态:汉堡菜单变成一个 x,顶部导航过渡到覆盖整个页面,4 个导航链接集中出现在导航覆盖上。

我在别处读到,如果 display: 发生变化,现代浏览器将忽略任何动画或过渡规范。现在还是这样吗?我怎样才能克服这个问题并延迟导航项的出现,直到叠加层完全展开?

它似乎在这里工作:https://codepen.io/KingKabir/pen/QyPwgG但我不确定他们是如何完成的?

我试过使用 visibility: 和 opacity: 但它们并没有完全隐藏非覆盖状态下的 4 个导航元素,因此在非覆盖状态下我的名字在导航菜单的右侧弄乱了.

HTML:

  <nav class="nav_container" id="overlay">
<div class="hamburger" id="hamburger" onclick="mobileMenu()">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
<div class="icon_personal_container">
<a href="../index.html">
<svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" id="icon_personal">
<title>J Logo</title>
<defs>
<linearGradient x1="14.6040568%" y1="100%" x2="85.3959432%" y2="0%" id="linearGradient-1">
<stop stop-color="#12A9FF" offset="0%"></stop>
<stop stop-color="#FF0096" offset="100%"></stop>
</linearGradient>
</defs>
<g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<circle fill="#333333" cx="12" cy="11.94" r="12"></circle>
<polygon points="18.57144 7.82616 14.68812 7.82616 12 7.82616 12 5.16 18.57144 5.16"></polygon>
<path d="M12.0021063,5.16 L17.3805494,5.16 L14.6827527,7.80073139 L14.6827527,13.044557 C14.6827527,16.5810127 13.1030779,18.9 9.57770614,18.9 C8.045129,18.9 6.72924975,18.3030871 5.82008559,17.4140986 C5.81142009,17.4056254 6.46074609,16.7540162 7.76806357,15.459271 C8.20030047,15.9163181 8.81488621,16.2138397 9.57770614,16.2138397 C11.2344382,16.2138397 11.985747,15.1509705 11.985747,13.1218565 L11.985747,5.16 L12.0021063,5.16 Z"
fill="url(#linearGradient-1)"></path>
</g>
</svg>
</a>
</div>
<div class="nav_tab" id="nav_tab_about"><a href="#about" class="nav_tab_hover_effect" onclick="mobileMenu()">About</a></div>
<div class="nav_tab" id="nav_tab_work"><a href="#work" class="nav_tab_hover_effect" onclick="mobileMenu()">Work</a></div>
<div class="nav_tab" id="nav_tab_skills"><a href="#skills" class="nav_tab_hover_effect" onclick="mobileMenu()">Skills</a></div>
<div class="nav_tab" id="nav_tab_contact"><a href="#contact" class="nav_tab_hover_effect" onclick="mobileMenu()">Contact</a></div>
<div class="blanks"></div>
<div class="blanks"></div>
<div id="nav_text">Jordan Tranchina</div>
</nav>

CSS:

    .nav_container {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr !important;
grid-template-rows: auto;
column-gap: 16px;
grid-template-areas:
"nav-far-left nav-left nav-right nav-far-right";
}
.nav_container.open {
max-height: 100%;
height: 200%;
background-color: #000000;
display: grid;
grid-template-rows: auto auto auto auto auto;
grid-template-areas:
"nav-far-left nav-left nav-right nav-far-right"
"second second second second"
"third third third third"
"fourth fourth fourth fourth"
"fifth fifth fifth fifth";
transition: all 0.4s cubic-bezier(0.455, 0.03, 0.515, 0.955);
}

.icon_personal_container,.blanks{
display: none;
}
.nav_tab {
display: block;
height: 0;
width: 0;
visibility: hidden;
opacity: 0;
}
.nav_tab.open{
display: block;
height: auto;
font-size: 32px;
font-weight: 500;
padding-top: 64px;
visibility: visible;
opacity: 1;
transition: visibility 0s linear;
}

#nav_tab_about.open{
grid-area: second;
transition-delay: .10s;
width: auto;
}
#nav_tab_work.open {
grid-area: third;
transition-delay: .15s;
width: auto;
}
#nav_tab_skills.open {
grid-area: fourth;
transition-delay: .2s;
width: auto;
}
#nav_tab_contact.open {
grid-area: fifth;
transition-delay: .25s;
width: auto;
}

#nav_text {
grid-area: 1 / nav-right / 1 / nav-far-right;
}

Javascript:

function mobileMenu() {
document.getElementById("hamburger").classList.toggle("active"); // toggling active class
document.body.classList.toggle("overlay-body"); //toggling overlay state on body
document.getElementById("overlay").classList.toggle("open"); //toggling overlay state on nav by setting classlist to "open"
// improve below by grabing by class not by id
document.getElementById("nav_tab_about").classList.toggle("open");
document.getElementById("nav_tab_work").classList.toggle("open");
document.getElementById("nav_tab_skills").classList.toggle("open");
document.getElementById("nav_tab_contact").classList.toggle("open");
}

我希望类“nav_tab”的导航元素在 nav_container 有时间完全展开后的十分之几秒内出现。

当前的结果是,在 nav_container 有时间完全展开之前,类“nav_tab”的导航元素立即出现。

最佳答案

如果您将visibilityopacitymax-height 结合使用,您可以实现从可见到隐藏或反之的良好过渡。反之亦然。隐藏时将元素的 max-height 设置为 0,将 max-height 设置为 Xpx(大于您的element will ever) when visible, prevents the element from messing with your layout in any way (as you mentioned in your question).

这是一个简单的例子:

var visible = document.querySelector(".visible");

function hide() {
visible.classList.add("hidden");
}

visible.addEventListener("click", hide);
div {
background-color: blue;
padding: 40px;
color: white;
cursor: pointer;
transition: all .1s ease;
}

.visible {
visibility: visible;
opacity: 1;
max-height: 1000px;
}

.hidden {
visibility: hidden;
opacity: 0;
max-height: 0;
}
<div class="visible">Click to hide</div>

关于css - 你如何在显示 :none and display:block? 之间转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55523881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com