[英]css animation on sidebar close
In this stackblitz, I am not able to add animation while closing, I tried it using transform, but it didnt seem to work HTML Blocker is used to covering the full screen in a half-transparent mode in mobile devices在这个 stackblitz 中,我无法在关闭时添加 animation,我尝试使用变换,但它似乎不起作用 HTML Blocker 用于在移动设备中以半透明模式覆盖全屏
const sidebar = document.querySelector('.sidebar'); sidebar.querySelector('.blocker').onclick = hide; function show() { // swipe right sidebar.classList.add('visible'); document.body.style.overflow = 'hidden'; } function hide() { // by blocker click, swipe left, or url change sidebar.classList.remove('visible'); document.body.style.overflow = ''; } function toggle() { sidebar.classList.contains('visible')? hide(): show(); }
.sidebar { /* it's a mobile sidebar, blocker and content */ position: fixed; top: 0; left: 0; width: 100vw; /* to cover the whole screen */ height: 100vh; padding: 0; /* to override the default padding */ background: rgba(0, 0, 0, .5); /* half transparent background */ display: none; z-index: 99999; /* to be on top of any other elements */ }.sidebar.visible { display: block; } /*cover the whole screen and to detect user click on background */.sidebar.blocker { position: absolute; width: 100%; height: 100%; } /* user content */.sidebar.content { position: absolute; top: 0; left: 0; background: #FFF; height: 100%; width: 250px; left: -50%; /* will be animated to left: 0, by animation */ animation: slide 0.5s forwards; } @keyframes slide { 100% { left: 0; } }
<div class="sidebar"> <div class="blocker"></div> <div class="content"> Sidebar Content </div> </div>
With the above code, you can have a working sidebar.使用上面的代码,您可以拥有一个工作侧边栏。
Check the working code from stackblitz检查来自 stackblitz 的工作代码
https://allenhwkim.medium.com/mobile-friendly-sidebar-in-few-minutes-7817b5c5239f https://stackblitz.com/edit/medium-sidebar-1-eevvax?file=style.css,index.js https://allenhwkim.medium.com/mobile-friendly-sidebar-in-few-minutes-7817b5c5239f https://stackblitz.com/edit/medium-sidebar-1-eevvax?file=style.css,index.js
You can't animate between display:block
(when .sidebar
has .visible
applied to it) and display:none
(when .visible
is removed from .sidebar
).您不能在
display:block
(当.sidebar
应用了.visible
时)和display:none
(当.visible
从.sidebar
中删除时)之间制作动画。
display:none
turns off the display of an element so that it has no effect on layout (the document is rendered as though the element did not exist). display:none
关闭元素的显示,使其对布局没有影响(文档呈现为好像该元素不存在)。 All descendant elements (ie .blocker
and .content
) also have their display turned off.所有后代元素(即
.blocker
和.content
)的显示也会关闭。
The reason you get an animation upon adding .visible
is that .sidebar
now "exists" and so .sidebar-content
also exists and as such animates.在添加
.visible
时获得 animation 的原因是.sidebar
现在“存在”,因此.sidebar-content
也存在并且因此具有动画效果。 As soon as you remove .visible
, .sidebar
ceases to exist again and so it and its descendants disappear instantaneously.一旦您删除
.visible
, .sidebar
就会再次不复存在,因此它及其后代会立即消失。
You are along the right lines using transforms but you need to remove display:none
as the method for hiding the sidebar.您使用转换是正确的,但您需要删除
display:none
作为隐藏侧边栏的方法。 Something like the below is a good starting point.像下面这样的东西是一个很好的起点。 You may need to change some values to get it looking exactly as you wish.
您可能需要更改一些值以使其看起来完全符合您的要求。 I have added a working codepen to show the result .
我添加了一个工作代码笔来显示结果。
.sidebar {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
padding: 0;
background: rgba(0, 0, 0, .5);
z-index: 99999;
transform: translateX(-100%); // new property - will move the element off the left hand side of the screen
transition: transform .5s ease-in-out; // new property - will make the sidebar slide in in a similar manner to your animation
}
.sidebar.visible {
transform: translateX(0); // new property - makes sidebar sit in its natural position (i.e. taking up the whole viewport)
}
.sidebar .blocker {
position: absolute;
width: 100%;
height: 100%;
}
.sidebar .content {
position: absolute;
top: 0;
left: 0;
background: #FFF;
height: 100%;
width: 250px;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.