css - 我可以通过 CSS 获得打开和关闭的双向转换速度吗?

标签 css transition

我有一个全屏覆盖菜单。

.overlay {
    height: 0%;
    width: 100%;
    position: fixed;
    z-index: 70;
    top: 0;
    right: 0;
    background-color:#415566;
    overflow-y: hidden;
    transition: 2.0s;
}

 <div id="menuActive"><a href="javascript:void(0)" onclick="openNav()">&#9776;</a></div>

 <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>

我有 JS 调用它来打开和关闭:

     function openNav() {
        document.getElementById("navBar").style.height = "100%";
     }

     function closeNav() {
        document.getElementById("navBar").style.height = "0%";
     }

是否可以为 openNav()closeNav() 设置不同的转换时间?目前,我只能控制两者。

最佳答案

您可以在 .overlay 中添加一个额外的类来修改过渡:

 function openNav() {
    var navBar = document.getElementById("navBar");
    navBar.style.height = "100%";
    setTimeout(function(){
       navBar.classList.add("opened");
    }, 2000)
 }

 function closeNav() {
    var navBar = document.getElementById("navBar");
    navBar.style.height = "0%";
    setTimeout(function(){
       navBar.classList.remove("opened");
    }, 300)
 }

这个类的 CSS 可以是:

.opened {
    transition: height 300ms; /* The duration you want... */
}

Also, take the good advice given by skobaljic in the question's comments about using JS to just add/remove classes for this kind of interaction.

关于css - 我可以通过 CSS 获得打开和关闭的双向转换速度吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40785805/

相关文章:

java - Eclipse RAP FormToolkit 控件忽略 CSS 样式

html - 我的 Bootstrap 模式显示正常,但背景不会褪色

HTML : hover effect for all cell with different colours

ios 7 - 具有延迟的 UIView 动画方法不延迟

jquery - 需要从 css 转换的间隔

system - 基于不变生成约束的方法

html - margin 属性未按预期工作

css - 在html5中设置单元格的宽度

javascript - D3 : almost identical codes, 不同的结果

javascript - VSCode 无法识别 <transition> 元素