【发布时间】:2020-04-09 06:19:42
【问题描述】:
目标是在显示其父级时通过 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