You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现侧边栏顶部面板固定与下方容器独立横向滚动

解决侧边栏顶部面板固定、下方容器横向滚动的问题

嘿,我来帮你搞定这个问题!你遇到的核心问题是布局结构没把顶部面板和滚动容器彻底分开,导致滚动时整个侧边栏内容都跟着动。咱们来调整一下代码,让顶部面板稳稳固定,下方的内容单独横向滚动:

<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet"/>
<div class="h-screen top-0 left-0 fixed w-64 bg-blue-300 flex flex-col">
<!-- Begin top panel -->
<div class="flex bg-red-300">
<span class="flex flex-1 p-4 border-b">
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path d="M12 14l9-5-9-5-9 5 9 5z" />
<path d="M12 14l6.16-3.422a12.083 12.083 0 01.665 6.479A11.952 11.952 0 0012 20.055a11.952 11.952 0 00-6.824-2.998 12.078 12.078 0 01.665-6.479L12 14z" />
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 14l9-5-9-5-9 5 9 5zm0 0l6.16-3.422a12.083 12.083 0 01.665 6.479A11.952 11.952 0 0012 20.055a11.952 11.952 0 00-6.824-2.998 12.078 12.078 0 01.665-6.479L12 14zm-4 6v-7.5l4-2.222" />
</svg>
</span>
<span class="flex p-4 border-b cursor-pointer">
<svg @click="closeSidebar" xmlns="http://www.w3.org/2000/svg" class="items-end h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd" />
</svg>
</span>
</div>
<!-- End top panel -->
<aside class="flex-grow flex flex-row overflow-x-auto overflow-y-hidden bg-yellow-400">
<div class="flex-shrink-0 bg-yellow-100 w-64">One</div>
<div class="flex-shrink-0 bg-green-300 w-64 overflow-y-auto">Two</div>
<div class="flex-shrink-0 bg-pink-300 w-64">Three</div>
<div class="flex-shrink-0 bg-blue-300 w-64">Four</div>
<div class="flex-shrink-0 bg-indigo-300 w-64">Five</div>
</aside>
</div>

关键改动说明:

  1. 外层侧边栏容器:添加了flex flex-col,让内部的顶部面板和下方滚动容器纵向排列,顶部面板占自身高度,下方容器自动填充剩余空间。
  2. 顶部面板:移除了多余的w-64(外层容器已经固定宽度,会自动继承),用flex和flex-1让Logo区域占满剩余宽度,布局更合理。
  3. 下方滚动容器(<aside>):
    • 把默认的纵向flex改成flex-row,让内部的5个div横向排列;
    • 添加overflow-x-auto开启横向滚动功能,当内容总宽度超过侧边栏宽度时自动出现滚动条;
    • 用flex-grow让容器占满侧边栏剩余高度;
    • 保留overflow-y-hidden避免出现不必要的纵向滚动条(如果你的内容需要纵向滚动,可以去掉这个属性)。

这样调整后,顶部面板会牢牢固定在侧边栏顶部,只有下方的5个div容器会响应横向滚动操作,完全符合你的需求~

内容的提问来源于stack exchange,提问作者pileup

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 23:42:50