css - Blazor 动画 - 将 div 设置为显示:动画结束后无
问题描述
最近我实现了一些动画,但是当动画结束时我遇到了一些问题。我有一个用于响应的 div 结构,当屏幕尺寸小于 X 时,它会显示一个菜单按钮。
当有人单击菜单按钮时,默认设置为 的左侧栏会display:none
通过立即删除display: none
属性然后添加动画类来显示,这一切都很好。但是,当侧边栏再次折叠时(通过单击关闭按钮),正在添加一个新动画(用于删除菜单),当此动画结束时,我需要将显示设置回display:none
(我不想要侧边栏占用任何空间)。
在当前情况下,我通过设置一些布尔变量来实现这一点,这些变量添加display:none
false 并删除它为 true。我的动画最多占用 300 毫秒,所以我添加了一个Task.Delay
300 毫秒来设置布尔值。遗憾的是,这并没有创建一个完美的“流程”,您可以看到屏幕打嗝,因为设置display:none
发生在几毫秒到晚。当然,我可以将其降低Task.Delay
到 290MS~ 但这不是一个很好的解决方案。
任何想法如何使用 Blazor 完成此任务?
代码下方:
HTML:
<!-- The mobile side bar, display: none default on screen sizes > X -->
<div class="md:hidden" style=@mobileSideBarDisplay>
<div class="fixed inset-0 flex z-40">
<! this sets the opacity of tyhe space to the right (grayed out) -->
<div class="fixed inset-0 @sidebarOverlayAnimation">
<div class="absolute inset-0 bg-gray-600 opacity-75"></div>
</div>
<!-- the actual side bar with it's buttons -->
<div class="relative flex-1 flex flex-col max-w-xs w-full pt-5 pb-4 bg-gray-800 @sidebarMenuAnimation">
<!-- Close button with which the sidebar menu can be closed -->
<button class=".." aria-label="Close sidebar" @onclick="ToggleSidebar"></button>
... Left out for readability
</div>
</div>
</div>
<!-- Static sidebar for desktop -->
<div class="hidden md:flex md:flex-shrink-0">
.. Left out for readability
</div>
<!-- menu button div which becomes visible at screensize < X
<div class="flex flex-col w-0 flex-1 overflow-hidden">
<!-- Menu button with which the sidebar menu can be opened -->
<button class=".. md:hidden" aria-label="Open sidebar" @onclick="ToggleSidebar">
.. Left out for readability
</div>
C#后端代码:
@code {
// used to set display: none; (or empty)
private bool showMobileSideBar = false;
// Used to show / toggle animations
private bool showMobileSideBarAnimation = false;
// toggle animation and display classes
private string mobileSideBarDisplay => showMobileSideBar ? "" : "display: none;";
private string sidebarOverlayAnimation => showMobileSideBarAnimation ? "sidebar-overlay-animation-entering" : "sidebar-overlay-animation-leaving";
private string sidebarMenuAnimation => showMobileSideBarAnimation ? "sidebar-menu-animation-entering" : "sidebar-menu-animation-leaving";
// Function to display entering or leaving animation depending on the boolean value)
private async Task ToggleSidebar()
{
showMobileSideBarAnimation = !showMobileSideBarAnimation;
if (showMobileSideBar == true) // only delay when we need to set div to display: none;
{
await Task.Delay(300); // this works kinda clunky now
}
showMobileSideBar = !showMobileSideBar;
}
}
CSS / 动画:
/*
Animations for setting the right-space opacity (when the sidebar is shown / hidden)
*/
.sidebar-overlay-animation-entering {
animation-name: sidebar-overlay-entering;
animation-duration: 300ms;
animation-timing-function: linear;
}
@keyframes sidebar-overlay-entering {
from { opacity: 0; }
to { opacity: 1; }
}
.sidebar-overlay-animation-leaving {
animation-name: sidebar-overlay-leaving;
animation-duration: 300ms;
animation-timing-function: linear;
}
@keyframes sidebar-overlay-leaving {
from { opacity: 1; }
to { opacity: 0; }
}
/*
Animations for displaying the sidebar (in- and out)
*/
.sidebar-menu-animation-entering {
animation-name: sidebar-menu-entering;
animation-duration: 300ms;
animation-timing-function: ease-in-out;
}
@keyframes sidebar-menu-entering {
from { transform: translateX(-100%); }
to { transform: translateX(0); }
}
.sidebar-menu-animation-leaving {
animation-name: sidebar-menu-leaving;
animation-duration: 300ms;
animation-timing-function: ease-in-out;
}
@keyframes sidebar-menu-leaving {
from { transform: translateX(0); }
to { transform: translateX(-100%); }
}
PS 我尝试使用https://github.com/dazinator/BlazorDeferredRemove但我无法让它工作。也没有使用动画的示例(仅具有可见性的过渡)或使用display: none;
(但如果有人可以给出一些指示,也许该库可以工作)
解决方案
推荐阅读
- c++ - 如何从 LCP 数组和 Suffix 数组构造 Suffix 树
- python - 加载保存的 cookie 后页面未登录
- wordpress - 在生产服务器上更改 .htaccess 文件时出现错误 500
- javascript - 悬停在子导航可见性上
- python-3.x - 如何使用 Pandas 从一天中删除 15 分钟的数据
- javascript - 使用 javascript 在元素断言中引用变量
- php - Codeigniter 应用:避免在同一个控制器的多个方法中重复代码
- jenkins - 无法使用文件规范中具有“build”属性的工件插件或 jfrog cli 下载最新工件
- angular - 找不到模拟服务
- javascript - 如何通过 API 调用 (Node.js) 从 ReadableStream 读取和下载文件?