gpt4 book ai didi

javascript - 展开 CSS3 动画不起作用

转载 作者:行者123 更新时间:2023-11-30 16:12:08 25 4
gpt4 key购买 nike

我有一个非常简单的叠加导航,如下所示:

<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');
});

FIDDLE HERE

现在,如果您在 Chrome 或 FF 中看到 fiddle 甚至运行动画,您会注意到收缩动画效果很好,但展开动画很突然,I.E.它只是行不通。谁能解释为什么动画不起作用?

最佳答案

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 相同的结果。

关于javascript - 展开 CSS3 动画不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36087906/

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