html - 如何获取 div 动画的当前状态?

标签 html css safari styling

我使用的是 Safari,因此为了更容易回答我的问题,我将在我的动画中使用 -webkit- 前缀。

我正在制作一个位于 Angular 落的菜单。当用户悬停时,它会跨越整个网站,直到用户离开菜单,然后它会再次后退。

我遇到的唯一问题是,如果用户不等到菜单栏完全跨越网页,则该栏会闪烁以到达其在动画中的位置。我该怎么做才能解决此问题并使动画从 div 的当前宽度开始?这是我的 CSS:

#menubar {
    /* Menu bar styling */
}

#menubar:hover {
    -webkit-animation: openMenu 2s;
    width: 100%;
}

#menubar:not(:hover) {
    -webkit-animation: closeMenu 2s;
    width: 75px;
}

/* Animations */

@-webkit-keyframes openMenu {
    from {width:75px;}
    to {width:100%;}
}

@-webkit-keyframes closeMenu {
    from {width:100%;} /* How do I get the current width of the div here instead of using width:100%;? */
    to {width:75px;}
}

在此先感谢大家的帮助。

最佳答案

你需要关键帧吗?您可以使用转换来获得相同的结果,例如 fiddle .

#menubar {
    /* Menu bar styling */
    width: 75px;

    transition: width 2s;
}

#menubar:hover {
    width: 100%;
}

基本上,您告诉菜单栏在悬停时应为 100%,空闲时应为 75px,宽度过渡应持续 2 秒,一切正常。

关于html - 如何获取 div 动画的当前状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30061730/

相关文章:

css - 背景大小的动画不适用于 Safari

css - 在 safari/chrome 中打印 css 背景?

javascript - 固定模板侧边栏位置固定

html - 在 Angular 5 App 中首次加载 App 时不播放背景视频

css - 在不使用 javascript 的情况下设置文件上传按钮的样式

css - 在 Phonegap 中设置 iFrame 高度

css - 样式化自动填充的文本字段

javascript - 从下拉列表中选择选项并使用 nodejs 提交请求

javascript - 如何添加过渡我的 slider ?

CSS动画-延迟计时