在悬停时为简单的CSS下拉导航栏添加动画效果。

huangapple go评论76阅读模式
英文:

animating a simple css dropdown navbar on hovering

问题

更新后的代码和问题

我有一个简单的导航栏,我试图在悬停下拉菜单时添加一些动画效果。但是我无法使其生效,所以我需要一些帮助理解为什么它不起作用。

这是HTML代码:

<nav> 
<main-categories><a href="#void">新闻</a>
<sub-categories>
<a href="#">欧洲</a>
<a href="#">亚洲</a>
<a href="#">非洲</a>
<a href="#">大洋洲</a>
<a href="#">北美洲</a>
<a href="#">南美洲</a>
</sub-categories></main-categories>
	
<!-- 其他代码省略 -->

&lt;/nav&gt;

这是CSS代码:

body, html {
  /* 其他样式省略 */
}

/* 其他样式省略 */

sub-categories {
  /* 动画效果样式 */
}

@keyframes rotateMenu {
  /* 动画关键帧样式 */
}

我还创建了一个Codepen链接,可以在这里看到效果:
https://codepen.io/familias/pen/XWygWzY

你有想法为什么效果不起作用或如何实现它吗?

英文:

UPDATED CODE AND QUESTION

I have a simple navbar and I am trying to add some animation when hovering the dropdowns. But I cannot make it to work so I need some help to understand why it is not working.

Here is the html

&lt;nav&gt; 
&lt;main-categories&gt;&lt;a href=&quot;#void&quot;&gt;News&lt;/a&gt;
&lt;sub-categories&gt;
&lt;a href=&quot;#&quot;&gt;Europe&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Asia&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Africa&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Oceania&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;North America&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;South America&lt;/a&gt;
&lt;/sub-categories&gt;&lt;/main-categories&gt;
	
&lt;main-categories&gt;&lt;a href=&quot;#void&quot;&gt;Politics&lt;/a&gt;
&lt;sub-categories&gt;
&lt;a href=&quot;#&quot;&gt;Europe&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Asia&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Africa&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Oceania&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;North America&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;South America&lt;/a&gt;
&lt;/sub-categories&gt;&lt;/main-categories&gt;
	
&lt;main-categories&gt;&lt;a href=&quot;#void&quot;&gt;Economy&lt;/a&gt;
&lt;sub-categories&gt;
&lt;a href=&quot;#&quot;&gt;Europe&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Asia&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Africa&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Oceania&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;North America&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;South America&lt;/a&gt;
&lt;/sub-categories&gt;&lt;/main-categories&gt;

&lt;main-categories&gt;&lt;a href=&quot;#void&quot;&gt;Health&lt;/a&gt;
&lt;sub-categories&gt;
&lt;a href=&quot;#&quot;&gt;Europe&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Asia&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Africa&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Oceania&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;North America&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;South America&lt;/a&gt;
&lt;/sub-categories&gt;&lt;/main-categories&gt;

&lt;main-categories&gt;&lt;a href=&quot;#void&quot;&gt;Education&lt;/a&gt;
&lt;sub-categories&gt;
&lt;a href=&quot;#&quot;&gt;Europe&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Asia&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Africa&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Oceania&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;North America&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;South America&lt;/a&gt;
&lt;/sub-categories&gt;&lt;/main-categories&gt;
	
&lt;main-categories&gt;&lt;a href=&quot;#void&quot;&gt;Culture&lt;/a&gt;&lt;/main-categories&gt;

&lt;/nav&gt;

Here is the CSS

body, html {
  margin:0;
  padding:0;
  border:0;
  outline:0;
  box-sizing:border-box;
  text-align:center;
  width:100%;
  height:100%
}
body {
  background-color:#FFFFFF;
  font-family:&#39;IBM Plex Serif&#39;,serif;
  font-size:16px;
  font-weight:400;
  color:#121212
}
a {
  color:#121212;
  text-decoration:none
}
a:hover {
  color:#E5633F
}
nav {
  grid-area:navi;
  display:block;
  text-align:center;
  border-bottom:3px solid #121212;
  background-color:inherit
}
nav {
  position:-webkit-sticky;
  position:sticky;
  top:0;
  cursor:pointer;
  z-index:8;
  width:100%
}
main-categories {
  position:relative;
  display:inline-block;
  background-color:inherit;
  text-transform:uppercase;
  height:42px;
  line-height:42px;
  text-align:center;
  padding:0px 22px 0px 22px;
}
main-categories:hover {
  color:#E5633F
}
main-categories a {
  position:relative;
  text-decoration:none
}	
main-categories a:hover {
  color:#E5633F
}
main-categories a:before{
  content: &#39;▪ &#39;;
}
sub-categories {
 display:none;
  position:absolute;
  width:auto;
  left:50%;
  transform:translatex(-50%);
  text-align:center;
  white-space:nowrap;
  background-color:inherit
}
sub-categories a {
  display:block;
  font-size:15px;
  padding:0 36px 0 36px;
  text-transform:capitalize;
  text-decoration:none;
  height:36px;
  line-height:36px;
  border-left:1px solid #121212;
  border-right:1px solid #121212;
  border-bottom:1px solid #121212
}	
sub-categories a:hover {
  color:#FFFFFF;
  background-color:#121212
}
main-categories:hover sub-categories {
  display:block
}
sub-categories a:before{
  content: &#39;&#39;;
}

And here is the animation effect I am trying to apply when hovering

sub-categories {
  animation:rotateMenu 400ms ease-in-out forwards
  transform-origin:top center
}
@keyframes rotateMenu {
    0% {
    transform: rotateX(-90deg)
    }
    70% {
    transform: rotateX(20deg) 
    }
    100% {
    transform: rotateX(0deg) 
    }

I also created a Codepen here to see it working
https://codepen.io/familias/pen/XWygWzY

Any idea why the effect is not working or how to achieve this?

答案1

得分: 3

在CSS动画的末尾添加一个分号,就像这样:

sub-categories {
  animation: rotateMenu 400ms ease-in-out forwards;
  transform-origin: top center;
}
英文:

Add a semicolon at the end of the CSS animation like this:

sub-categories {
  animation: rotateMenu 400ms ease-in-out forwards;
  transform-origin: top center;
}

huangapple
  • 本文由 发表于 2023年7月3日 04:17:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600623.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定