【问题标题】:CSS translation animation doesn't work when parent is shown显示父级时 CSS 翻译动画不起作用
【发布时间】:2020-04-09 06:19:42
【问题描述】:

有this和this之类的类似问题,但不要针对这种情况。

目标是在显示其父级时通过 CSS 翻译将菜单滑动到屏幕上。但是,显示父级然后应用 CSS 类来触发翻译会立即发生,而不是随着时间的推移。实际上,没有动画。

JavaScript 可用于将子元素滑动到屏幕上,但目标是将尽可能多的动画逻辑保留在 CSS 中。

将不透明度设置为 0 不起作用,因为我们需要菜单及其父级不占用任何空间或成为布局的一部分。

Codepen:https://codepen.io/Crashalot/pen/YzXmjYj

function toggleSidebar() {
  $("#sidebar").toggleClass("show");
}


$("#button").on("click", function() {
    toggleSidebar();
});
body {
  margin: 0;
  padding: 0;
}

#button {
  position: fixed;
  top: 0;
  left: 200px;
  width: 150px;
  height: 50px;
  background: yellow;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 8;
  cursor: pointer;
}

#sidebar {
  display: none;
}

#sidebar.show {
  display: block;
}

.overlay {
  position: fixed;
  width: 100vw;
  height: 100vh;
  background: red;
  z-index: -1;
}

.menuBox {
  width: 200px;
  height: 100vh;
  background: blue;
	transition: 0.3s ease-in-out;				       
  transform: translate(-100%);
}

#sidebar.show .menuBox {
  transform: translate(0);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="sidebar">
  <div class="overlay"></div>
  <div class="menuBox"></div>
</div>


<div id="button">CLICK ME</div>

【问题讨论】:

  • 你能更新你的笔吗? toggleSidebar 功能不会被任何东西触发。
  • @JohanDahl 更新了!感谢您的帮助。

标签: javascript jquery html css


【解决方案1】:

你不能动画显示:无;将不透明度设置为 0,然后在切换类上设置为 1。这是适合您的 CodePen。 ;)

我为切换事件添加了一个按钮。如果您需要更多帮助,请告诉我!

enter link description here

$(".btn").on("click", toggleSidebar);

function toggleSidebar() {
  $("#sidebar").toggleClass("show");
}
body {
  margin: 0;
  padding: 0;
}

#sidebar {
  opacity: 0;
  transition: all 300ms ease-in-out;
}

#sidebar.show {
  display: block;
  opacity: 1;

}

.overlay {
  position: fixed;
  width: 100vw;
  height: 100vh;
  background: red;
  z-index: -1;
}

.menuBox {
  width: 200px;
  height: 100vh;
  background: blue;
	transition: 300ms ease-in-out;				       
  -webkit-transform: translate(-100%);
}

#sidebar.show .menuBox {
  -webkit-transform: translate(0);
}

.btn {
  position: absolute;
  top: 10px;
  left: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="sidebar">
  <div class="overlay"></div>
  <div class="menuBox"></div>
</div>

<button class="btn">Click</button>

【讨论】:

  • 感谢您的快速响应!抱歉忘了提到 #sidebar 元素必须隐藏,不占用空间,并且是布局流程的一部分。
  • opacity: 0 你似乎只是隐藏了我们的眼睛,但它仍然适用于任何 javascript 事件,如点击、鼠标事件等。你可能是你的“可见性:隐藏”,不透明度:0。
  • 你能澄清你想要的用户体验吗?就我而言,它似乎可以按您的意愿工作。也许如果您提供一些有关您希望看到的详细信息,我可以为您修改代码。 :)
【解决方案2】:

您需要考虑动画。当元素出现在屏幕上时动画会自动运行

function toggleSidebar() {
  $("#sidebar").toggleClass("show");
}


$("#button").on("click", function() {
  toggleSidebar();
});
body {
  margin: 0;
  padding: 0;
}

#button {
  position: fixed;
  top: 0;
  left: 200px;
  width: 150px;
  height: 50px;
  background: yellow;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 8;
  cursor: pointer;
}

#sidebar {
  display: none;
}

#sidebar.show {
  display: block;
}

.overlay {
  position: fixed;
  width: 100vw;
  height: 100vh;
  background: red;
  z-index: -1;
}

.menuBox {
  width: 200px;
  height: 100vh;
  background: blue;
  transform: translate(-100%);
  animation: show 0.3s ease-in-out forwards;
}

@keyframes show {
  to {
    transform: translate(0);
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="sidebar">
  <div class="overlay"></div>
  <div class="menuBox"></div>
</div>


<div id="button">CLICK ME</div>

将 display 更新为 none 以外的值将启动所有由 animation-name 属性应用于元素的动画,以及所有 应用于后代 且 display 不是 none 的动画。 ref

【讨论】:

  • 谢谢!你能解释一下为什么 CSS 动画有效,但通过 transform 属性实现的动画无效?
  • @Crashalot 添加了 Spec 的部分,解释了为什么它与动画一起使用 过渡有点棘手,但问题是您没有更改值,因为最初没有显示元素并且当您使用 display:block 添加它,它将添加新值(旧值从未存在),因此您将看不到任何转换,因为对于浏览器,它带有最后一个值
【解决方案3】:

我认为您应该为调用的函数定义操作。当加载页面或点击如下:

$(document).ready(function () {
    $('#sidebar').on('click', function () {
          $(this).toggleClass('show');
    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-06
    • 2018-09-28
    • 1970-01-01
    • 2018-03-28
    • 2014-05-02
    • 2019-10-16
    • 2015-01-10
    • 1970-01-01
    相关资源
    最近更新 更多