我们可以使用回调函数延迟显示的执行=“无”?

发布于 2025-02-07 01:20:55 字数 1377 浏览 3 评论 0原文

你好,
我想创建一个使用CSS和JavaScript下拉的动画。但是,当显示屏设置为“无”时,动画可能无法正常工作,因为显示属性无法动画。但是,如果我延迟显示属性,它会提供足够的动画来加载。那么,是否有可能通过回调函数延迟同一显示属性的方法?这是我的代码:

function myfun() {
  let dis = document.getElementsByClassName("litems")[0];
  if (dis.style.display == "none") {
    dis.style.display = "block";
    dis.style.animation = "up 2s ease-in 1"
  } else {
    dis.style.animation = "down 2s ease-in 1";
    setTimeout(() => {
      dis.style.display = "none";
    }, 2000)
  }
}
.list ul {
  background: orchid;
  position: absolute;
  display: block;
}

@keyframes up {
  0% {
    top: 0px;
    background: white;
  }
  100% {
    top: 400px;
    background: orange;
  }
}

@keyframes down {
  0% {
    top: 400px;
    background: orange;
  }
  100% {
    top: 0px;
    background: white;
  }
}
  <button onclick="myfun()">Click it to toggle</button>
  <div class="list">
    <ul class="litems">
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
      <li>Item 4</li>
    </ul>
  </div>
</body>

非常感谢。

Hello all
I want to create an animation for a dropdown using css and javascript. But when the display is set to none, the animations don't work maybe because display property can't be animated. But if I delay the display property, it gives animation enough time to load. So is there any possible method to delay the same display property through callback function? Here is my code:

function myfun() {
  let dis = document.getElementsByClassName("litems")[0];
  if (dis.style.display == "none") {
    dis.style.display = "block";
    dis.style.animation = "up 2s ease-in 1"
  } else {
    dis.style.animation = "down 2s ease-in 1";
    setTimeout(() => {
      dis.style.display = "none";
    }, 2000)
  }
}
.list ul {
  background: orchid;
  position: absolute;
  display: block;
}

@keyframes up {
  0% {
    top: 0px;
    background: white;
  }
  100% {
    top: 400px;
    background: orange;
  }
}

@keyframes down {
  0% {
    top: 400px;
    background: orange;
  }
  100% {
    top: 0px;
    background: white;
  }
}
  <button onclick="myfun()">Click it to toggle</button>
  <div class="list">
    <ul class="litems">
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
      <li>Item 4</li>
    </ul>
  </div>
</body>

Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文