【问题标题】:Transition is not applying to dropdown [duplicate]过渡不适用于下拉菜单[重复]
【发布时间】:2021-05-30 20:11:16
【问题描述】:

我正在尝试将过渡应用到我的下拉菜单,但它不起作用。悬停后立即出现下拉菜单,效果不可见。

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  padding: 12px 16px;
  z-index: 1;
  transform: translateY(-170px);
  transition: all 1s;
}

.dropdown:hover>.dropdown-content {
  display: block;
  background-color: yellowgreen;
  transform: translateY(20px);
}
<h2>Hoverable Dropdown</h2>
<p>Move the mouse over the text below to open the dropdown content.</p>
<div class="dropdown">
  <div class="dropdown-content">
    <p>Hello World!</p>
  </div>
  <span>Mouse over me</span>
</div>

【问题讨论】:

  • 您不能为 display 属性设置动画。尝试为不透明度设置动画。
  • 我正在尝试为变换制作动画。
  • 您将display: none 设为默认值,display: block 处于悬停状态,因此它会阻止任何其他动画。
  • 不客气。

标签: html css css-transitions


【解决方案1】:

display 属性不可设置动画。您可以改为为 opacity 设置动画。
您可以将display 默认为block

<html>
  <head>
    <style>
      .dropdown {
        position: relative;
        display: inline-block;
      }
      .dropdown-content {
        display: block;
        position: absolute;
        background-color: #f9f9f9;
        min-width: 160px;
        box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
        padding: 12px 16px;
        z-index: 1;
        opacity: 0;
        transform: translateY(-170px);
        transition: all 1s;
      }
      .dropdown:hover > .dropdown-content {
        opacity: 1;
        background-color: yellowgreen;
        transform: translateY(20px);
      }
    </style>
  </head>
  <body>
    <h2>Hoverable Dropdown</h2>
    <p>Move the mouse over the text below to open the dropdown content.</p>
    <div class="dropdown">
      <div class="dropdown-content">
        <p>Hello World!</p>
      </div>
      <span>Mouse over me</span>
    </div>
  </body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-28
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    相关资源
    最近更新 更多