html - 当您将鼠标移开时,如何反转下拉菜单的动画?

标签 html css

我正在制作一个下拉菜单,当鼠标悬停在菜单选项卡上时它会在屏幕上飞行,但当我将鼠标移出选项卡或下拉菜单时似乎无法让它反转动画并返回屏幕外。

我已经尝试过 animation-directionanimation-iteration-count 等动画属性,但似乎都不起作用。

这是 HTML:

<div id="menu"> <!--The menu bar-->
    <ul class="menulist"> <!--The list in the menu which has the tabs-->
        <li id="menutab"> <!--One of the tabs-->
            <a class="menulinks">tab 1</a> <!--The tab text-->
            <div class="dropdown-content"> <!--The dropdown menu with all the links-->
                <a>link 1</a>
                <a>link 2</a>
                <a>link 3</a>
            </div>
        </li>
    </ul>
</div>

这是有问题的缩短的 css。完整代码在这里:https://codepen.io/PercyCho/pen/PMqgEg

.dropdown-content {
    display: none;
    position: absolute;
    background-color: blue;
    min-width: 260px;
    margin-top: 19px;
    z-index: 1;
  /*is there a property or animation that I can put here to reverse the animation once I mouse off of it?*/
    animation-name: dropdown-slide;
    animation-duration: 0.35s;
}

@keyframes dropdown-slide{
    from{transform: translate(-1500px, 0px);} /*-1500px just means off the screen*/
    to{transform: translate(0px, 0px);}/*which then makes it come onscreen */
}

li#menutab:hover .dropdown-content {
    display: block;
}

最佳答案

您不需要动画来获得所需的结果...只需在 transition 上使用带有持续时间的 transform 属性即可:

.dropdown-content {
    position: absolute;
    transform: translate(-1500px, 0px);
    transition : .35s all;
    background-color: blue;
}

li#menutab:hover .dropdown-content {
transform: translate(0px, 0px)
}

检查这个codepen

关于html - 当您将鼠标移开时,如何反转下拉菜单的动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57136429/

相关文章:

javascript - 在 keyup() 之后迭代时 jQuery 卡住

使用 for 循环的 javascript 计数器

javascript - Bootstrap 导航栏在单击侧边栏导航列表后隐藏页面部分

css - 如何更改第一个选择选项的文本颜色

html - NW.js、Brackets-Shell 和 Electron 在功能上有什么区别?

javascript - iframe 链接在 iframe 内重定向

html - 使用 CSS 更改 anchor 标记的内容

css - 如何下载包括 Fontawesome 等网络字体的完整页面?

javascript - 在 OWL Carousel 中滑动 2 个元素

javascript - 无效字符错误: Failed to execute 'createElement' on 'Document' : The tag name provided ('/static/media/tab1.fab25bc3.png' ) is not a valid name