Blazor Web assembly - 处理视口/设备宽度更改

Nic*_*las 3 css blazor

最近,我在 Blazor WebAssembly 项目中创建了一个自定义导航菜单(使用 TailwindCSS)。根据设备宽度,显示/隐藏侧边栏(使用 Tailwind 隐藏和带有 CSS 类的设备)。

在特定的设备宽度上,显示菜单按钮,特定于移动侧边栏的 div 始终隐藏(显示:无),在 Blazor 中,我创建了一个函数,将@onclick移动侧边栏的样式设置为空(所以显示:没有被删除),反之亦然。

这一切都很完美,但是这种方法有一个问题。单击菜单按钮时,将显示移动侧边栏,如果我现在增加设备宽度,则菜单按钮将自动隐藏,移动侧边栏也是如此,如果我再次减小设备宽度,菜单按钮将自动隐藏再次显示,但“打开”侧边栏 div 也被显示(因为在最后一个较小的设备宽度中也被打开(显示样式:无被删除)。

行为应该是,当设备宽度增加时,移动侧栏应始终保留其样式属性 display:none。

不过,据我所知,Blazor 中没有任何事件会触发changed device width? 有什么方法可以让它工作吗?

下面的一些图像和 HTML 代码显示了“问题”:

正常尺寸视图: 在此输入图像描述

手机查看:

在此输入图像描述

移动侧边栏打开视图: 在此输入图像描述

所以问题是,当我从Mobile sidebar opened viewback 到Normal size view然后回到时Mobile view,它会显示Mobile sidebar opened view相反 (因为它仍然有一个空的样式属性,相当于display: block

HTML / Blazor C# 代码:

 <div class="md:hidden" style=@mobileSideBarDisplay> <!-- Blazor C# string that sets the display to display: none, or removes the display entirely to show it)
    <div class="fixed inset-0 flex z-40">

    <!--
      Off-canvas menu overlay, show/hide based on off-canvas menu state.

      Entering: "transition-opacity ease-linear duration-300"
        From: "opacity-0"
        To: "opacity-100"
      Leaving: "transition-opacity ease-linear duration-300"
        From: "opacity-100"
        To: "opacity-0"
    -->
        <div class="fixed inset-0">
            <div class="absolute inset-0 bg-gray-600 opacity-75"></div>
        </div>

        <!-- The menu item with is Blazor onclick event -->
        <div class="relative flex-1 flex flex-col max-w-xs w-full pt-5 pb-4 bg-gray-800">
            <div class="absolute top-0 right-0 -mr-14 p-1">
                <button class="flex items-center justify-center h-12 w-12 rounded-full focus:outline-none focus:bg-gray-600" aria-label="Close sidebar" @onclick="ToggleSidebar">
                    <svg class="h-6 w-6 text-white" stroke="currentColor" fill="none" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
                    </svg>
                </button>
            </div>


            <! -- rest of HTML, left out for readability -->
        </div>
     </div>
</div>



<!-- Static sidebar for desktop -->
<div class="hidden md:flex md:flex-shrink-0">
    <div class="flex flex-col w-64">

        <! -- rest of HTML, left out for readability -->


    </div>
</div>


<!-- Horizontal navbar with the Menu button -->
<div class="flex flex-col w-0 flex-1 overflow-hidden">
    <div class="relative z-10 flex-shrink-0 flex h-16 bg-white shadow">

        <!-- menu button becomes visible at a certain device-width > done with the TailWind CSS class md:hidden -->
        <button class="px-4 border-r border-gray-200 text-gray-500 focus:outline-none focus:bg-gray-100 focus:text-gray-600 md:hidden" aria-label="Open sidebar" @onclick="ToggleSidebar">
            <svg class="h-6 w-6" stroke="currentColor" fill="none" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h7" />
            </svg>
        </button>

    <! -- rest of HTML, left out for readability -->

    </div>
</div>



<!-- C# code for viewing mobile sidebar on Menu button press -->
@code {

    private bool showMobileSideBar = false;

    private string mobileSideBarDisplay => showMobileSideBar ? "" : "display: none;";

    private void ToggleSidebar()
    {
        showMobileSideBar = !showMobileSideBar;
    }
}
Run Code Online (Sandbox Code Playgroud)

rdm*_*ptn 6

您需要 js 互操作和 window.resize 事件。

有些库可以提供帮助,例如https://github.com/EdCharbeneau/BlazorSize

  • 希望有一个更原生/非包/非 JS 的解决方案,但这会做得很完美!谢谢。 (2认同)