展开 CSS3 动画不工作
Expand CSS3 animation is not working
我有一个非常简单的叠加导航,见下文:
<nav role="navigation" class="navigation">
<a href="#" class="brand-logo"><img src="img/beer-jug-logo.png" alt="brand logo"></a>
<ul class="navigation-list">
<li><a href="">Home</a></li>
<li><a href="">About</a></li>
<li><a href="">Design</a></li>
<li><a href="">Interiors</a></li>
<li><a href="">Contact</a></li>
</ul>
<a class="close"></a>
</nav>
现在我添加了一个非常简单的 CSS 展开和收缩动画,这些动画是使用 jQuery 切换的。动画代码如下:
.navigation.shrinkMenu {
-webkit-animation-name: shrinkMenu;
-o-animation-name: shrinkMenu;
animation-name: shrinkMenu;
-webkit-animation-duration: 1s;
-o-animation-duration: 1s;
animation-duration: 1s;
-webkit-animation-fill-mode: both;
-o-animation-fill-mode: both;
animation-fill-mode: both;
}
.navigation.expandMenu {
-webkit-animation-name: expandMenu;
-o-animation-name: expandMenu;
animation-name: expandMenu;
-webkit-animation-delay: 3s;
-o-animation-delay: 3s;
animation-delay: 3s;
-webkit-animation-duration: 1s;
-o-animation-duration: 1s;
animation-duration: 1s;
-webkit-animation-fill-mode: both;
-o-animation-fill-mode: both;
animation-fill-mode: both;
}
@keyframes shrinkMenu {
90% {
opacity: 0;
}
99% {
left: 50%;
top: 50%;
bottom: 50%;
right: 50%;
}
100% {
opacity: 0;
visibility: hidden;
left: 50%;
top: 50%;
bottom: 50%;
right: 50%;
}
}
@keyframes expandMenu {
0% {
left: 50%;
top: 50%;
bottom: 50%;
right: 50%;
}
100% {
left: 0;
top: 0;
bottom: 0;
right: 0;
}
}
菜单初始样式如下:
.navigation {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
/*background: rgba(244 ,41, 65,.9);*/
background: rgba(255 ,255, 255,.9);
display: flex;
align-items:center;
justify-content:center;
flex-direction:column;
z-index: 999;
}
切换菜单可见性的 jQuery 代码如下:
$('.close').on('click', function() {
$('.navigation').addClass('shrinkMenu');
});
$('.H-menu').on('click', function() {
$('.navigation').addClass('expandMenu');
});
现在,如果您在 Chrome 或 FF 中看到 fiddle 甚至 运行 动画,您会注意到收缩动画效果很好,但展开动画很突然, IE。它只是行不通。谁能解释为什么动画不起作用?
Note: This doesn't seem to be consistent problem. The problem happens in Fiddle only when it is loaded for the first time (by giving the URL in the address bar and clicking Go). When any edit is made to the Fiddle and we just "Run" it, the error does not happen. I could not re-create the issue in Stack Snippet either.
您的动画代码或 CSS 没有问题。问题似乎是由 a.H-menu
标记中的 href
属性引起的。当指定此属性但没有值时,似乎整个页面都在单击菜单图标时重新加载,因此您看不到动画。
您可以执行以下操作之一:
- 在单击事件处理程序中设置
href='#'
并使用 e.preventDefault()
(或)
- 甚至不要在
a.H-menu
标签中提及 href
属性。
执行上述任一操作都意味着不会重新加载页面,因此会显示动画。
您可以通过访问以下 Fiddles(首次加载时查看控制台)来验证我在说什么:
- Fiddle with original code - 打开 Fiddle 并立即打开控制台。将显示错误消息,指示无法加载图像 (beer-jug-logo.png)。清除此错误消息,关闭菜单并单击图标重新打开它。您会注意到错误再次显示(表明它正在重新加载)。
- Fiddle with href='#' - 执行与上述相同的步骤,您会注意到动画正常工作,并且在单击
.H-menu
图标时控制台中没有错误消息。
- Fiddle with no href - 相同的步骤,您会注意到与第二个 Fiddle.
相同的结果
我有一个非常简单的叠加导航,见下文:
<nav role="navigation" class="navigation">
<a href="#" class="brand-logo"><img src="img/beer-jug-logo.png" alt="brand logo"></a>
<ul class="navigation-list">
<li><a href="">Home</a></li>
<li><a href="">About</a></li>
<li><a href="">Design</a></li>
<li><a href="">Interiors</a></li>
<li><a href="">Contact</a></li>
</ul>
<a class="close"></a>
</nav>
现在我添加了一个非常简单的 CSS 展开和收缩动画,这些动画是使用 jQuery 切换的。动画代码如下:
.navigation.shrinkMenu {
-webkit-animation-name: shrinkMenu;
-o-animation-name: shrinkMenu;
animation-name: shrinkMenu;
-webkit-animation-duration: 1s;
-o-animation-duration: 1s;
animation-duration: 1s;
-webkit-animation-fill-mode: both;
-o-animation-fill-mode: both;
animation-fill-mode: both;
}
.navigation.expandMenu {
-webkit-animation-name: expandMenu;
-o-animation-name: expandMenu;
animation-name: expandMenu;
-webkit-animation-delay: 3s;
-o-animation-delay: 3s;
animation-delay: 3s;
-webkit-animation-duration: 1s;
-o-animation-duration: 1s;
animation-duration: 1s;
-webkit-animation-fill-mode: both;
-o-animation-fill-mode: both;
animation-fill-mode: both;
}
@keyframes shrinkMenu {
90% {
opacity: 0;
}
99% {
left: 50%;
top: 50%;
bottom: 50%;
right: 50%;
}
100% {
opacity: 0;
visibility: hidden;
left: 50%;
top: 50%;
bottom: 50%;
right: 50%;
}
}
@keyframes expandMenu {
0% {
left: 50%;
top: 50%;
bottom: 50%;
right: 50%;
}
100% {
left: 0;
top: 0;
bottom: 0;
right: 0;
}
}
菜单初始样式如下:
.navigation {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
/*background: rgba(244 ,41, 65,.9);*/
background: rgba(255 ,255, 255,.9);
display: flex;
align-items:center;
justify-content:center;
flex-direction:column;
z-index: 999;
}
切换菜单可见性的 jQuery 代码如下:
$('.close').on('click', function() {
$('.navigation').addClass('shrinkMenu');
});
$('.H-menu').on('click', function() {
$('.navigation').addClass('expandMenu');
});
现在,如果您在 Chrome 或 FF 中看到 fiddle 甚至 运行 动画,您会注意到收缩动画效果很好,但展开动画很突然, IE。它只是行不通。谁能解释为什么动画不起作用?
Note: This doesn't seem to be consistent problem. The problem happens in Fiddle only when it is loaded for the first time (by giving the URL in the address bar and clicking Go). When any edit is made to the Fiddle and we just "Run" it, the error does not happen. I could not re-create the issue in Stack Snippet either.
您的动画代码或 CSS 没有问题。问题似乎是由 a.H-menu
标记中的 href
属性引起的。当指定此属性但没有值时,似乎整个页面都在单击菜单图标时重新加载,因此您看不到动画。
您可以执行以下操作之一:
- 在单击事件处理程序中设置
href='#'
并使用e.preventDefault()
(或) - 甚至不要在
a.H-menu
标签中提及href
属性。
执行上述任一操作都意味着不会重新加载页面,因此会显示动画。
您可以通过访问以下 Fiddles(首次加载时查看控制台)来验证我在说什么:
- Fiddle with original code - 打开 Fiddle 并立即打开控制台。将显示错误消息,指示无法加载图像 (beer-jug-logo.png)。清除此错误消息,关闭菜单并单击图标重新打开它。您会注意到错误再次显示(表明它正在重新加载)。
- Fiddle with href='#' - 执行与上述相同的步骤,您会注意到动画正常工作,并且在单击
.H-menu
图标时控制台中没有错误消息。 - Fiddle with no href - 相同的步骤,您会注意到与第二个 Fiddle. 相同的结果