在我给导航栏添加一个css动画效果之前,所有东西都居中并完美地工作,但现在有了动画,当主类别悬停时,子类别不再居中显示.

我试着用

text-align:center, margin:0 auto, left:50% and transform:translatex(-50%); 

但似乎什么都不管用.

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: 'IBM Plex Serif', 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: '▪ ';
}
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: '';
}
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) 
  }
}
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Serif:wght@300;400;500&family=Playfair+Display:wght@400;700&display=swap" rel="stylesheet">

<nav class="menu"> 
  <!-- 1. -->
  <main-categories>
    <a href="#void">News</a>
    <sub-categories>
    <a href="#">Europe</a>
    <a href="#">Asia</a>
    <a href="#">Africa</a>
    <a href="#">Oceania</a>
    <a href="#">North America</a>
    <a href="#">South America</a>
    </sub-categories>
  </main-categories>
    
  <!-- 2. -->
  <main-categories>
    <a href="#void">Politics</a>
    <sub-categories>
      <a href="#">Europe</a>
      <a href="#">Asia</a>
      <a href="#">Africa</a>
      <a href="#">Oceania</a>
      <a href="#">North America</a>
      <a href="#">South America</a>
    </sub-categories>
  </main-categories>

  <!-- 3. -->
  <main-categories>
    <a href="#void">Economy</a>
    <sub-categories>
      <a href="#">Europe</a>
      <a href="#">Asia</a>
      <a href="#">Africa</a>
      <a href="#">Oceania</a>
      <a href="#">North America</a>
      <a href="#">South America</a>
    </sub-categories>
  </main-categories>

  <!-- 4. -->
  <main-categories>
    <a href="#void">Health</a>
    <sub-categories>
      <a href="#">Europe</a>
      <a href="#">Asia</a>
      <a href="#">Africa</a>
      <a href="#">Oceania</a>
      <a href="#">North America</a>
      <a href="#">South America</a>
    </sub-categories>
  </main-categories>

  <!-- 5. -->
  <main-categories>
    <a href="#void">Education</a>
    <sub-categories>
      <a href="#">Europe</a>
      <a href="#">Asia</a>
      <a href="#">Africa</a>
      <a href="#">Oceania</a>
      <a href="#">North America</a>
      <a href="#">South America</a>
    </sub-categories>
  </main-categories>
    
  <!-- 6. -->
  <main-categories>
    <a href="#void">Culture</a>
  </main-categories>
</nav>

我还在这里创建了一个Codesen来查看它的工作情况,并对其进行处理 https://codepen.io/familias/pen/XWygWzY

当主类别悬停时,你知道如何将子类别居中吗?

推荐答案

反射到您的代码

sub-categories {
  left: 50%;
  transform: translateX(-50%);
}

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

这是设置中心位置的正确代码.然而,transform: translateX(-50%);(在sub-categories上)不会生效,因为您已经用动画覆盖了它...所以它应该应用在动画的末尾!

sub-categories {
  left: 50%;
}

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

@keyframes rotateMenu {
  0% {
    transform: rotateX(-90deg) translateX(-50%); /* HERE */
  }
  70% {
    transform: rotateX(20deg) translateX(-50%); /* HERE */
  }
  100% {
    transform: rotateX(0deg) translateX(-50%); /* HERE */
  }
}

在动画中,定义变换时,需要在所有位置都包含transformX(-50%).这样,它将在动画过程中和之后应用于元素.

示例

/*** OPTIONAL SETTINGS ***/
.menu {
  display: flex; /* No adjustment is necessary, but this way the menu will always be single-line, even on small screens. */
}
.menu > main-categories > a {
  display: flex; /* No need for adjustment, but this way the dot and the text within the <a> will always be placed next to each other. */
  gap: 5px; /* The distance between the two elements can be set in Flex, so there will always be a 5px distance between the dot and the text. */
}

/*** ORIGINAL SETTINGS ***/
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: 'IBM Plex Serif', 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: '▪ ';
}
sub-categories {
  display: none;
  position: absolute;
  width: auto;
  left: 50%;
  /* transform: translateX(-50%); This doesn't take effect because you have overridden it with the animation... so it should be applied at the end of the animation! */
  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: '';
}
sub-categories {
  animation: rotateMenu 400ms ease-in-out forwards;
  transform-origin: top center;
}

@keyframes rotateMenu {
  0% {
    transform: rotateX(-90deg) translateX(-50%); /* HERE */
  }
  70% {
    transform: rotateX(20deg) translateX(-50%); /* HERE */
  }
  100% {
    transform: rotateX(0deg) translateX(-50%); /* HERE */
  }
}
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Serif:wght@300;400;500&family=Playfair+Display:wght@400;700&display=swap" rel="stylesheet">

<nav class="menu"> 
  <!-- 1. -->
  <main-categories>
    <a href="#void">News</a>
    <sub-categories>
    <a href="#">Europe</a>
    <a href="#">Asia</a>
    <a href="#">Africa</a>
    <a href="#">Oceania</a>
    <a href="#">North America</a>
    <a href="#">South America</a>
    </sub-categories>
  </main-categories>
    
  <!-- 2. -->
  <main-categories>
    <a href="#void">Politics</a>
    <sub-categories>
      <a href="#">Europe</a>
      <a href="#">Asia</a>
      <a href="#">Africa</a>
      <a href="#">Oceania</a>
      <a href="#">North America</a>
      <a href="#">South America</a>
    </sub-categories>
  </main-categories>

  <!-- 3. -->
  <main-categories>
    <a href="#void">Economy</a>
    <sub-categories>
      <a href="#">Europe</a>
      <a href="#">Asia</a>
      <a href="#">Africa</a>
      <a href="#">Oceania</a>
      <a href="#">North America</a>
      <a href="#">South America</a>
    </sub-categories>
  </main-categories>

  <!-- 4. -->
  <main-categories>
    <a href="#void">Health</a>
    <sub-categories>
      <a href="#">Europe</a>
      <a href="#">Asia</a>
      <a href="#">Africa</a>
      <a href="#">Oceania</a>
      <a href="#">North America</a>
      <a href="#">South America</a>
    </sub-categories>
  </main-categories>

  <!-- 5. -->
  <main-categories>
    <a href="#void">Education</a>
    <sub-categories>
      <a href="#">Europe</a>
      <a href="#">Asia</a>
      <a href="#">Africa</a>
      <a href="#">Oceania</a>
      <a href="#">North America</a>
      <a href="#">South America</a>
    </sub-categories>
  </main-categories>
    
  <!-- 6. -->
  <main-categories>
    <a href="#void">Culture</a>
  </main-categories>
</nav>

Javascript相关问答推荐

获取最接近的父项(即自定义元素)

基于每个索引迭代嵌套对象

在JS中获取名字和姓氏的首字母

使用redux-toolet DelivercThunks刷新访问令牌

使用print This时, map 容器已在LeafletJS中初始化

使用续集和下拉栏显示模型属性

微软Edge编辑和重新发送未显示""

TypeScript索引签名模板限制

为什么这个JS模块在TypeScript中使用默认属性导入?""

更改JSON中使用AJAX返回的图像的路径

try 使用javascript隐藏下拉 Select

Use Location位置成员在HashRouter内为空

类构造函数忽略Reaction Native中的可选字段,但在浏览器中按预期工作

变量在导入到Vite中的另一个js文件时成为常量.

Nextjs 13.4 Next-Auth 4.2登录(&Quot;凭据&,{});不工作

按什么顺序接收`storage`事件?

在查看网页时,如何使HTML中的按钮工作方式类似于鼠标上的滚轮或箭头键?

FindByIdAndUpdate在嵌套对象中创建_id

react 路由DOM有条件地呈现元素

将延迟加载的模块转换为Eager 加载的模块