在本教程中,您将学习如何使用 Tailwind CSS 在 Svelte 中创建响应式侧边栏。您还将了解如何使用过渡和动画来增强用户体验。
工具使用
带有打字稿的 Svelte Tailwind CSS 侧边栏仪表板。
<script lang="ts">
let homeLabel: string = "Home";
let inboxLabel: string = "Inbox";
let ordersLabel: string = "Orders";
let settingsLabel: string = "Settings";
let logoutLabel: string = "Logout";
</script>
<main>
<div class="flex">
<div class="flex flex-col h-screen p-3 bg-white shadow w-60">
<div class="space-y-3">
<div class="flex items-center">
<h2 class="text-xl font-bold">Dashboard</h2>
</div>
<div class="flex-1">
<ul class="pt-2 pb-4 space-y-1 text-sm">
<li class="rounded-sm">
<a href={null} class="flex items-center p-2 space-x-3 rounded-md">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M2.25 12l8.954-8.955c.44-.439 1.152-.439 1.591 0L21.75 12M4.5 9.75v10.125c0 .621.504 1.125 1.125 1.125H9.75v-4.875c0-.621.504-1.125 1.125-1.125h2.25c.621 0 1.125.504 1.125 1.125V21h4.125c.621 0 1.125-.504 1.125-1.125V9.75M8.25 21h8.25"
/>
</svg>
<span>{homeLabel}</span>
</a>
</li>
<li class="rounded-sm">
<a href={null} class="flex items-center p-2 space-x-3 rounded-md">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M2.25 13.5h3.86a2.25 2.25 0 012.012 1.244l.256.512a2.25 2.25 0 002.013 1.244h3.218a2.25 2.25 0 002.013-1.244l.256-.512a2.25 2.25 0 012.013-1.244h3.859m-19.5.338V18a2.25 2.25 0 002.25 2.25h15A2.25 2.25 0 0021.75 18v-4.162c0-.224-.034-.447-.1-.661L19.24 5.338a2.25 2.25 0 00-2.15-1.588H6.911a2.25 2.25 0 00-2.15 1.588L2.35 13.177a2.25 2.25 0 00-.1.661z"
/>
</svg>
<span>{inboxLabel}</span>
</a>
</li>
<li class="rounded-sm">
<a href={null} class="flex items-center p-2 space-x-3 rounded-md">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M15.75 10.5V6a3.75 3.75 0 10-7.5 0v4.5m11.356-1.993l1.263 12c.07.665-.45 1.243-1.119 1.243H4.25a1.125 1.125 0 01-1.12-1.243l1.264-12A1.125 1.125 0 015.513 7.5h12.974c.576 0 1.059.435 1.119 1.007zM8.625 10.5a.375.375 0 11-.75 0 .375.375 0 01.75 0zm7.5 0a.375.375 0 11-.75 0 .375.375 0 01.75 0z"
/>
</svg>
<span>{ordersLabel}</span>
</a>
</li>
<li class="rounded-sm">
<a href={null} class="flex items-center p-2 space-x-3 rounded-md">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M9.594 3.94c.09-.542.56-.94 1.11-.94h2.593c.55 0 1.02.398 1.11.94l.213 1.281c.063.374.313.686.645.87.074.04.147.083.22.127.324.196.72.257 1.075.124l1.217-.456a1.125 1.125 0 011.37.49l1.296 2.247a1.125 1.125 0 01-.26 1.431l-1.003.827c-.293.24-.438.613-.431.992a6.759 6.759 0 010 .255c-.007.378.138.75.43.99l1.005.828c.424.35.534.954.26 1.43l-1.298 2.247a1.125 1.125 0 01-1.369.491l-1.217-.456c-.355-.133-.75-.072-1.076.124a6.57 6.57 0 01-.22.128c-.331.183-.581.495-.644.869l-.213 1.28c-.09.543-.56.941-1.11.941h-2.594c-.55 0-1.02-.398-1.11-.94l-.213-1.281c-.062-.374-.312-.686-.644-.87a6.52 6.52 0 01-.22-.127c-.325-.196-.72-.257-1.076-.124l-1.217.456a1.125 1.125 0 01-1.369-.49l-1.297-2.247a1.125 1.125 0 01.26-1.431l1.004-.827c.292-.24.437-.613.43-.992a6.932 6.932 0 010-.255c.007-.378-.138-.75-.43-.99l-1.004-.828a1.125 1.125 0 01-.26-1.43l1.297-2.247a1.125 1.125 0 011.37-.491l1.216.456c.356.133.751.072 1.076-.124.072-.044.146-.087.22-.128.332-.183.582-.495.644-.869l.214-1.281z"
/>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"
/>
</svg>
<span>{settingsLabel}</span>
</a>
</li>
<li class="rounded-sm">
<a href={null} class="flex items-center p-2 space-x-3 rounded-md">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M15.75 9V5.25A2.25 2.25 0 0013.5 3h-6a2.25 2.25 0 00-2.25 2.25v13.5A2.25 2.25 0 007.5 21h6a2.25 2.25 0 002.25-2.25V15M12 9l-3 3m0 0l3 3m-3-3h12.75"
/>
</svg>
<span>{logoutLabel}</span>
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="container mx-auto mt-12">
<div class="grid grid-cols-1 gap-6 mb-6 lg:grid-cols-3">
<div class="w-full px-4 py-5 bg-white rounded-lg shadow">
<div class="text-sm font-medium text-gray-500 truncate">
Total users
</div>
<div class="mt-1 text-3xl font-semibold text-gray-900">12,00</div>
</div>
<div class="w-full px-4 py-5 bg-white rounded-lg shadow">
<div class="text-sm font-medium text-gray-500 truncate">
Total Profit
</div>
<div class="mt-1 text-3xl font-semibold text-gray-900">$ 450k</div>
</div>
<div class="w-full px-4 py-5 bg-white rounded-lg shadow">
<div class="text-sm font-medium text-gray-500 truncate">
Total Orders
</div>
<div class="mt-1 text-3xl font-semibold text-gray-900">20k</div>
</div>
</div>
</div>
</div>
</main>
带有搜索栏的苗条顺风侧边栏。
<main>
<div class="flex">
<div class="flex flex-col h-screen p-3 bg-white shadow w-60">
<div class="space-y-3">
<div class="flex items-center">
<h2 class="text-xl font-bold">Dashboard</h2>
</div>
<div class="relative">
<span class="absolute inset-y-0 left-0 flex items-center py-4">
<button type="submit" class="p-2 focus:outline-none focus:ring">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M21 21l-5.197-5.197m0 0A7.5 7.5 0 105.196 5.196a7.5 7.5 0 0010.607 10.607z"
/>
</svg>
</button>
</span>
<input
type="search"
name="Search"
placeholder="Search..."
class="w-full py-2 pl-10 text-sm border rounded-md focus:outline-none"
/>
</div>
<div class="flex-1">
<ul class="pt-2 pb-4 space-y-1 text-sm">
<li class="rounded-sm">
<a href={null} class="flex items-center p-2 space-x-3 rounded-md">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M2.25 12l8.954-8.955c.44-.439 1.152-.439 1.591 0L21.75 12M4.5 9.75v10.125c0 .621.504 1.125 1.125 1.125H9.75v-4.875c0-.621.504-1.125 1.125-1.125h2.25c.621 0 1.125.504 1.125 1.125V21h4.125c.621 0 1.125-.504 1.125-1.125V9.75M8.25 21h8.25"
/>
</svg>
<span>Home</span>
</a>
</li>
<li class="rounded-sm">
<a href={null} class="flex items-center p-2 space-x-3 rounded-md">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M2.25 13.5h3.86a2.25 2.25 0 012.012 1.244l.256.512a2.25 2.25 0 002.013 1.244h3.218a2.25 2.25 0 002.013-1.244l.256-.512a2.25 2.25 0 012.013-1.244h3.859m-19.5.338V18a2.25 2.25 0 002.25 2.25h15A2.25 2.25 0 0021.75 18v-4.162c0-.224-.034-.447-.1-.661L19.24 5.338a2.25 2.25 0 00-2.15-1.588H6.911a2.25 2.25 0 00-2.15 1.588L2.35 13.177a2.25 2.25 0 00-.1.661z"
/>
</svg>
<span>Inbox</span>
</a>
</li>
<li class="rounded-sm">
<a href={null} class="flex items-center p-2 space-x-3 rounded-md">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M15.75 10.5V6a3.75 3.75 0 10-7.5 0v4.5m11.356-1.993l1.263 12c.07.665-.45 1.243-1.119 1.243H4.25a1.125 1.125 0 01-1.12-1.243l1.264-12A1.125 1.125 0 015.513 7.5h12.974c.576 0 1.059.435 1.119 1.007zM8.625 10.5a.375.375 0 11-.75 0 .375.375 0 01.75 0zm7.5 0a.375.375 0 11-.75 0 .375.375 0 01.75 0z"
/>
</svg>
<span>Orders</span>
</a>
</li>
<li class="rounded-sm">
<a href={null} class="flex items-center p-2 space-x-3 rounded-md">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M9.594 3.94c.09-.542.56-.94 1.11-.94h2.593c.55 0 1.02.398 1.11.94l.213 1.281c.063.374.313.686.645.87.074.04.147.083.22.127.324.196.72.257 1.075.124l1.217-.456a1.125 1.125 0 011.37.49l1.296 2.247a1.125 1.125 0 01-.26 1.431l-1.003.827c-.293.24-.438.613-.431.992a6.759 6.759 0 010 .255c-.007.378.138.75.43.99l1.005.828c.424.35.534.954.26 1.43l-1.298 2.247a1.125 1.125 0 01-1.369.491l-1.217-.456c-.355-.133-.75-.072-1.076.124a6.57 6.57 0 01-.22.128c-.331.183-.581.495-.644.869l-.213 1.28c-.09.543-.56.941-1.11.941h-2.594c-.55 0-1.02-.398-1.11-.94l-.213-1.281c-.062-.374-.312-.686-.644-.87a6.52 6.52 0 01-.22-.127c-.325-.196-.72-.257-1.076-.124l-1.217.456a1.125 1.125 0 01-1.369-.49l-1.297-2.247a1.125 1.125 0 01.26-1.431l1.004-.827c.292-.24.437-.613.43-.992a6.932 6.932 0 010-.255c.007-.378-.138-.75-.43-.99l-1.004-.828a1.125 1.125 0 01-.26-1.43l1.297-2.247a1.125 1.125 0 011.37-.491l1.216.456c.356.133.751.072 1.076-.124.072-.044.146-.087.22-.128.332-.183.582-.495.644-.869l.214-1.281z"
/>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"
/>
</svg>
<span>Settings</span>
</a>
</li>
<li class="rounded-sm">
<a href={null} class="flex items-center p-2 space-x-3 rounded-md">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M15.75 9V5.25A2.25 2.25 0 0013.5 3h-6a2.25 2.25 0 00-2.25 2.25v13.5A2.25 2.25 0 007.5 21h6a2.25 2.25 0 002.25-2.25V15M12 9l-3 3m0 0l3 3m-3-3h12.75"
/>
</svg>
<span>Logout</span>
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="container mx-auto mt-12">
<div class="grid grid-cols-1 gap-6 mb-6 lg:grid-cols-3">
<div class="w-full px-4 py-5 bg-white rounded-lg shadow">
<div class="text-sm font-medium text-gray-500 truncate">
Total users
</div>
<div class="mt-1 text-3xl font-semibold text-gray-900">12,00</div>
</div>
<div class="w-full px-4 py-5 bg-white rounded-lg shadow">
<div class="text-sm font-medium text-gray-500 truncate">
Total Profit
</div>
<div class="mt-1 text-3xl font-semibold text-gray-900">$ 450k</div>
</div>
<div class="w-full px-4 py-5 bg-white rounded-lg shadow">
<div class="text-sm font-medium text-gray-500 truncate">
Total Orders
</div>
<div class="mt-1 text-3xl font-semibold text-gray-900">20k</div>
</div>
</div>
</div>
</div>
</main>
苗条的顺风折叠侧边栏。
<script>
let isOpen = false;
function toggleOpen() {
isOpen = !isOpen;
}
</script>
<main>
<div class="flex">
<div
class="{isOpen
? 'w-40'
: 'w-60'} flex flex-col h-screen p-3 duration-300 bg-gray-800 shadow"
>
<div class="space-y-3">
<div class="flex items-center justify-between">
<h2 class="text-xl font-bold text-white">Dashboard</h2>
<button on:click={toggleOpen}>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6 text-white"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25h16.5"
/>
</svg>
</button>
</div>
<div class="relative">
<span class="absolute inset-y-0 left-0 flex items-center py-4">
<button type="submit" class="p-2 focus:outline-none focus:ring">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M21 21l-5.197-5.197m0 0A7.5 7.5 0 105.196 5.196a7.5 7.5 0 0010.607 10.607z"
/>
</svg>
</button>
</span>
<input
type="search"
name="Search"
placeholder="Search..."
class="w-full py-2 pl-10 text-sm rounded-md focus:outline-none"
/>
</div>
<div class="flex-1">
<ul class="pt-2 pb-4 space-y-1 text-sm">
<li class="rounded-sm">
<a href={null} class="flex items-center p-2 space-x-3 rounded-md">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6 text-gray-100"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M2.25 12l8.954-8.955c.44-.439 1.152-.439 1.591 0L21.75 12M4.5 9.75v10.125c0 .621.504 1.125 1.125 1.125H9.75v-4.875c0-.621.504-1.125 1.125-1.125h2.25c.621 0 1.125.504 1.125 1.125V21h4.125c.621 0 1.125-.504 1.125-1.125V9.75M8.25 21h8.25"
/>
</svg>
<span class="text-gray-100">Home</span>
</a>
</li>
<li class="rounded-sm">
<a href={null} class="flex items-center p-2 space-x-3 rounded-md">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6 text-gray-100"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M2.25 13.5h3.86a2.25 2.25 0 012.012 1.244l.256.512a2.25 2.25 0 002.013 1.244h3.218a2.25 2.25 0 002.013-1.244l.256-.512a2.25 2.25 0 012.013-1.244h3.859m-19.5.338V18a2.25 2.25 0 002.25 2.25h15A2.25 2.25 0 0021.75 18v-4.162c0-.224-.034-.447-.1-.661L19.24 5.338a2.25 2.25 0 00-2.15-1.588H6.911a2.25 2.25 0 00-2.15 1.588L2.35 13.177a2.25 2.25 0 00-.1.661z"
/>
</svg>
<span class="text-gray-100">Inbox</span>
</a>
</li>
<li class="rounded-sm">
<a href={null} class="flex items-center p-2 space-x-3 rounded-md">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6 text-gray-100"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M15.75 10.5V6a3.75 3.75 0 10-7.5 0v4.5m11.356-1.993l1.263 12c.07.665-.45 1.243-1.119 1.243H4.25a1.125 1.125 0 01-1.12-1.243l1.264-12A1.125 1.125 0 015.513 7.5h12.974c.576 0 1.059.435 1.119 1.007zM8.625 10.5a.375.375 0 11-.75 0 .375.375 0 01.75 0zm7.5 0a.375.375 0 11-.75 0 .375.375 0 01.75 0z"
/>
</svg>
<span class="text-gray-100"> Orders </span>
</a>
</li>
<li class="rounded-sm">
<a href={null} class="flex items-center p-2 space-x-3 rounded-md">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6 text-gray-100"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M9.594 3.94c.09-.542.56-.94 1.11-.94h2.593c.55 0 1.02.398 1.11.94l.213 1.281c.063.374.313.686.645.87.074.04.147.083.22.127.324.196.72.257 1.075.124l1.217-.456a1.125 1.125 0 011.37.49l1.296 2.247a1.125 1.125 0 01-.26 1.431l-1.003.827c-.293.24-.438.613-.431.992a6.759 6.759 0 010 .255c-.007.378.138.75.43.99l1.005.828c.424.35.534.954.26 1.43l-1.298 2.247a1.125 1.125 0 01-1.369.491l-1.217-.456c-.355-.133-.75-.072-1.076.124a6.57 6.57 0 01-.22.128c-.331.183-.581.495-.644.869l-.213 1.28c-.09.543-.56.941-1.11.941h-2.594c-.55 0-1.02-.398-1.11-.94l-.213-1.281c-.062-.374-.312-.686-.644-.87a6.52 6.52 0 01-.22-.127c-.325-.196-.72-.257-1.076-.124l-1.217.456a1.125 1.125 0 01-1.369-.49l-1.297-2.247a1.125 1.125 0 01.26-1.431l1.004-.827c.292-.24.437-.613.43-.992a6.932 6.932 0 010-.255c.007-.378-.138-.75-.43-.99l-1.004-.828a1.125 1.125 0 01-.26-1.43l1.297-2.247a1.125 1.125 0 011.37-.491l1.216.456c.356.133.751.072 1.076-.124.072-.044.146-.087.22-.128.332-.183.582-.495.644-.869l.214-1.281z"
/>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"
/>
</svg>
<span class="text-gray-100"> Settings </span>
</a>
</li>
<li class="rounded-sm">
<a href={null} class="flex items-center p-2 space-x-3 rounded-md">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6 text-gray-100"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M15.75 9V5.25A2.25 2.25 0 0013.5 3h-6a2.25 2.25 0 00-2.25 2.25v13.5A2.25 2.25 0 007.5 21h6a2.25 2.25 0 002.25-2.25V15M12 9l-3 3m0 0l3 3m-3-3h12.75"
/>
</svg>
<span class="text-gray-100"> Logout </span>
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="container mx-auto mt-12">
<div class="grid grid-cols-1 gap-6 mb-6 lg:grid-cols-3">
<div class="w-full px-4 py-5 bg-white rounded-lg shadow">
<div class="text-sm font-medium text-gray-500 truncate">
Total users
</div>
<div class="mt-1 text-3xl font-semibold text-gray-900">12,00</div>
</div>
<div class="w-full px-4 py-5 bg-white rounded-lg shadow">
<div class="text-sm font-medium text-gray-500 truncate">
Total Profit
</div>
<div class="mt-1 text-3xl font-semibold text-gray-900">$ 450k</div>
</div>
<div class="w-full px-4 py-5 bg-white rounded-lg shadow">
<div class="text-sm font-medium text-gray-500 truncate">
Total Orders
</div>
<div class="mt-1 text-3xl font-semibold text-gray-900">20k</div>
</div>
</div>
</div>
</div>
</main>