使用CSS和Java來構(gòu)建管理儀表盤布局的實例代碼

您將要創(chuàng)造的
在這個新教程中,我們將使用CSS和JavaScript來創(chuàng)建響應(yīng)式管理儀表板布局。 要構(gòu)建它,我們將從WordPress儀表板中借鑒一些想法,例如其可折疊的側(cè)邊欄菜單。
在整個教程中,我們將面臨許多挑戰(zhàn),但是這些挑戰(zhàn)將為我們提供良好的實踐技巧,以提高我們的前端技能。
事不宜遲,讓我們看一下最終的管理儀表板演示(單擊側(cè)邊欄底部的“ 折疊”按鈕以查看可折疊的導航功能,并查看全屏版本以發(fā)揮其響應(yīng)能力):
1.從頁面標記開始
要開始標記,我們需要一個SVG,一個標題和一個部分:
<svg style="display:none;">...</svg> <header class="page-header">...</header> <section class="page-content">...</section>
SVG精靈
您可能會想到,在任何管理控制臺中,我們都需要一堆圖標。 值得慶幸的是, Envato Elements提供了越來越多的有用矢量圖標集合,因此,讓我們利用該庫并下載這些Trade和Dashboard Icons 。

與其通過img或svg標簽將它們直接包含在頁面中,不如讓我們更進一步以創(chuàng)建SVG精靈。 為此,我們將所有圖標包裝在SVG容器中。 該容器應(yīng)該是隱藏的,因此我們將對其應(yīng)用display: none 。 如果我們不隱藏它,則頁面頂部會出現(xiàn)一個很大的空白區(qū)域。
每個圖標將放置在具有唯一ID和viewBox屬性的symbol元素內(nèi),該屬性取決于圖標的大小。 然后,只要需要,我們就可以通過調(diào)用use元素來呈現(xiàn)目標圖標(我將在稍后展示給您看)。
現(xiàn)在,讓我們熟悉SVG Sprite所需的標記:
<svg style="display:none;">
<symbol id="down" viewBox="0 0 16 16">
<polygon points="3.81 4.38 8 8.57 12.19 4.38 13.71 5.91 8 11.62 2.29 5.91 3.81 4.38" />
</symbol>
<symbol id="users" viewBox="0 0 16 16">
<path d="M8,0a8,8,0,1,0,8,8A8,8,0,0,0,8,0ZM8,15a7,7,0,0,1-5.19-2.32,2.71,2.71,0,0,1,1.7-1,13.11,13.11,0,0,0,1.29-.28,2.32,2.32,0,0,0,.94-.34,1.17,1.17,0,0,0-.27-.7h0A3.61,3.61,0,0,1,5.15,7.49,3.18,3.18,0,0,1,8,4.07a3.18,3.18,0,0,1,2.86,3.42,3.6,3.6,0,0,1-1.32,2.88h0a1.13,1.13,0,0,0-.27.69,2.68,2.68,0,0,0,.93.31,10.81,10.81,0,0,0,1.28.23,2.63,2.63,0,0,1,1.78,1A7,7,0,0,1,8,15Z" />
</symbol>
<!-- more symbols here -->
</svg>
實際上,這就是我們創(chuàng)建內(nèi)置SVG Sprite所需的全部。
標頭
繼續(xù)我們的管理儀表板布局,讓我們看一下頁面標題。
在其中,我們將定義一個nav元素,它將用作以下元素的包裝:
- 徽標
- 折疊按鈕,將在移動屏幕上切換菜單
- 菜單本身將包含菜單鏈接,兩個標題以及折疊/展開按鈕。 從語義上來說,擁有兩個單獨的菜單并將標題放在它們外面可能更正確,但是如果您愿意,可以采用不同的方法。
這是寬屏(> 767px)上的樣子:

標頭結(jié)構(gòu):
<header class="page-header">
<nav>
<a href="#0">
<img class="logo" src="logo.svg" alt="forecastr logo">
</a>
<button class="toggle-mob-menu" aria-expanded="false" aria-label="open menu">
<svg width="20" height="20" aria-hidden="true">
<use xlink:href="#down"></use>
</svg>
</button>
<ul class="admin-menu">
<li class="menu-heading">
<h3>Admin</h3>
</li>
<li>
<a href="#0">
<svg>
<use xlink:href="#pages"></use>
</svg>
<span>Pages</span>
</a>
</li>
<!-- more list items here -->
<li>
<button class="collapse-btn" aria-expanded="true" aria-label="collapse menu">
<svg aria-hidden="true">
<use xlink:href="#collapse"></use>
</svg>
<span>Collapse</span>
</button>
</li>
</ul>
</nav>
</header>
注意上面的代碼中的兩件事:
- 我們?nèi)绾问褂?code class="inline">use元素引用目標圖標。
- 我們添加到切換按鈕的ARIA屬性(
aria-expanded,aria-label,aria-hidden)。 這些屬性將幫助我們使組件更易于訪問。 稍后,我們將討論如何根據(jù)按鈕的狀態(tài)更新其值。
部分
該部分將包含兩個嵌套部分。
第1節(jié)
在第一部分的內(nèi)部,我們將放置搜索表單和一些有關(guān)當前登錄用戶的信息(名稱,頭像和通知)。
這是它在寬屏(> 767px)上的外觀:

部分結(jié)構(gòu):
<section class="search-and-user">
<form>
<input type="search" placeholder="Search Pages...">
<button type="submit" aria-label="submit form">
<svg aria-hidden="true">
<use xlink:href="#search"></use>
</svg>
</button>
</form>
<div class="admin-profile">
<span class="greeting">...</span>
<div class="notifications">
<span class="badge">...</span>
<svg>
<use xlink:href="#users"></use>
</svg>
</div>
</div>
</section>
同樣,請注意,我們向提交按鈕添加了一些ARIA屬性。
第2節(jié)
在第二部分中,僅是為了使演示中充實一些虛擬內(nèi)容,我們將放置一堆文章占位符。 這些通常可能包含表格數(shù)據(jù),圖表或某種形式的提要。
“最多使用5–7個不同的小部件來創(chuàng)建視圖。 否則,用戶將很難集中精力并獲得清晰的概覽。” – 塔拉斯Bakusevych
這是它在寬屏(> 767px)上的外觀:

根據(jù)UX最佳實踐,您可能不需要這么多部分
部分結(jié)構(gòu):
<section class="page-content">
<section class="grid">
<article></article>
<article></article>
<article></article>
<article></article>
<article></article>
<article></article>
<article></article>
<article></article>
</section>
</section>
2.定義一些基本樣式
準備好我們的管理控制臺標記后,我們將繼續(xù)使用CSS。 與往常一樣,第一步是指定一些CSS變量和常見的重置樣式:
:root {
--page-header-bgColor: #242e42;
--page-header-bgColor-hover: #1d2636;
--page-header-txtColor: #dde9f8;
--page-header-headingColor: #7889a4;
--page-header-width: 220px;
--page-content-bgColor: #f0f1f6;
--page-content-txtColor: #171616;
--page-content-blockColor: #fff;
--white: #fff;
--black: #333;
--blue: #00b9eb;
--red: #ec1848;
--border-radius: 4px;
--box-shadow: 0 0 10px -2px rgba(0, 0, 0, 0.075);
}
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
ul {
list-style: none;
}
a,
button {
color: inherit;
}
a {
text-decoration: none;
}
button {
background: none;
cursor: pointer;
}
input {
-webkit-appearance: none;
}
button,
input {
border: none;
}
svg {
display: block;
}
body {
font: 16px/1.5 "Lato", sans-serif;
}
注意 :為簡單起見,我不會逐步學習本教程中的所有 CSS規(guī)則。 這里有將近400行CSS。 如果需要,可以通過單擊演示項目的CSS選項卡將其全部選中。
3.定義主儀表板樣式
至此,我們準備專注于頁面樣式。
設(shè)置標題
標頭將是固定位置元素。 其寬度將為220px,其高度等于視口高度。 如果其內(nèi)容超過視口高度,則將顯示一個垂直滾動條。
nav元素的行為將是高度至少為100%的flex容器。 請記住,它的直接子對象是三個:
徽標 移動菜單切換按鈕, 和菜單。
切換按鈕僅在小屏幕(<768px)上可見。 這是我們需要的樣式:
/*CUSTOM VARIABLES HERE*/
.page-header {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
overflow: auto;
padding-top: 20px;
width: var(--page-header-width);
color: var(--page-header-txtColor);
background: var(--page-header-bgColor);
}
.page-header nav {
display: flex;
flex-direction: column;
min-height: 100%;
}
.page-header .toggle-mob-menu {
display: none;
}
提示:如果您希望覆蓋整個頁面高度的絕對定位頁眉,請?zhí)砑右韵聵邮剑?/p>
body {
position: relative;
}
.page-header {
position: absolute;
top: 0;
left: 0;
height: 100%;
/*Comment these styles*/
/*position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
overflow: auto;*/
}
菜單樣式
菜單將用作flex容器,我們將為其指定flex: 1 ,以便其展開并覆蓋整個父級高度。
最后一個菜單項將被設(shè)置為margin-top: auto因為它應(yīng)該位于菜單的最底部。 當標題滾動條不出現(xiàn)時,此行為將更加清楚。 要對其進行測試,請嘗試刪除一些菜單項,或在高屏幕上查看演示。
菜單中的鏈接和按鈕也將充當彈性容器,其內(nèi)容(文本和圖標)應(yīng)垂直對齊。
與其他菜單元素相比,菜單標題要小一些。 此外,我們將增加其字符之間的間距。
這是菜單樣式的一部分:
/*CUSTOM VARIABLES HERE*/
.page-header .admin-menu {
display: flex;
flex-direction: column;
flex-grow: 1;
margin-top: 35px;
}
.page-header .admin-menu li:last-child {
margin-top: auto;
margin-bottom: 20px;
}
.page-header .admin-menu li > * {
width: 100%;
padding: 12px 15px;
}
.page-header .admin-menu a,
.page-header .admin-menu button {
display: flex;
align-items: center;
font-size: 0.9rem;
transition: background 0.2s, color 0.2s;
}
.page-header .admin-menu .menu-heading h3 {
text-transform: uppercase;
letter-spacing: 0.15em;
font-size: 12px;
margin-top: 12px;
color: var(--page-header-headingColor);
}
頁面內(nèi)容樣式
請記住, .page-content部分包含兩個子部分。
此部分將放置在距視口左側(cè)220px的位置。 另外,我們將其width: calc(100% - 220px) 。 請注意,它的left屬性值等于標題寬度。
其樣式:
/*CUSTOM VARIABLES HERE*/
.page-content {
position: relative;
left: var(--page-header-width);
width: calc(100% - var(--page-header-width));
min-height: 100vh;
padding: 30px;
color: var(--page-content-txtColor);
background: var(--page-content-bgColor);
}
搜索和用戶樣式
另外,請記住, .search-and-user部分包含兩個元素:搜索表單和.admin-profile 。
為了進行布局,我們將使用CSS Grid。 搜索表單將覆蓋全部可用空間,并且與其兄弟姐妹之間會有50px的間距。 兩個兄弟將垂直對齊。
表單內(nèi)的提交按鈕將處于絕對位置。 它只會包含一個裝飾性圖標,因此我們需要一個ARIA屬性,以允許屏幕閱讀器對其進行解釋并使其可訪問。
包含兩個元素的.admin-profile將充當具有垂直居中內(nèi)容的flex容器。 badge(counter)元素將以水平和垂直居中的內(nèi)容絕對定位在其父對象內(nèi)部。
這是此部分所需樣式的一部分:
/*CUSTOM VARIABLES HERE*/
.search-and-user {
display: grid;
grid-template-columns: 1fr auto;
grid-column-gap: 50px;
align-items: center;
background: var(--page-content-bgColor);
margin-bottom: 30px;
}
.search-and-user form {
position: relative;
}
.search-and-user form button {
position: absolute;
top: 50%;
right: 15px;
transform: translateY(-50%);
}
.search-and-user .admin-profile {
display: flex;
align-items: center;
}
.search-and-user .admin-profile .notifications {
position: relative;
}
.search-and-user .admin-profile .badge {
display: flex;
align-items: center;
justify-content: center;
position: absolute;
top: -10px;
right: -3px;
width: 18px;
height: 18px;
border-radius: 50%;
font-size: 10px;
color: var(--white);
background: var(--red);
}
網(wǎng)格樣式
要在我們的管理儀表板上布置文章,我們將利用CSS網(wǎng)格。 我們將為所有文章提供300px的固定高度。 除了第一篇和最后一篇文章將覆蓋整個父寬度,其他所有文章都將成為兩列布局的一部分。
關(guān)聯(lián)的樣式:
/*CUSTOM VARIABLES HERE*/
.page-content .grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-gap: 30px;
}
.page-content .grid > article {
display: flex;
height: 300px;
background: var(--page-content-blockColor);
border-radius: var(--border-radius);
box-shadow: var(--box-shadow);
}
.page-content .grid > article:first-child,
.page-content .grid > article:last-child {
grid-column: 1 / -1;
}
4.切換標題
每次我們單擊折疊/展開按鈕時,標題狀態(tài)都會改變。 如果展開,它將折疊(僅保留菜單項的圖標變體),反之亦然。

請記住,此功能僅在大于767px的屏幕上可用。 對于較小的屏幕,標題將具有不同的布局,稍后我們將介紹。
在標頭處于折疊狀態(tài)時,主體接收collapsed類。 到那時,發(fā)生了以下事情:
- 標題縮小。 其寬度從220px變?yōu)?0px。
- 響應(yīng)于此,
.page-content節(jié)增大。 具體來說,其寬度從width: calc(100% - 220px)變?yōu)?code class="inline">width: calc(100% - 40px) 。 此外,其left屬性值變?yōu)?0px而不是220px。 - 徽標,菜單標題,菜單鏈接文本和菜單按鈕文本消失。
- 切換按鈕的
aria-expanded和aria-label屬性值已更新。 另外,其圖標旋轉(zhuǎn)了180度,因此看起來像是展開圖標。
這是實現(xiàn)此功能JavaScript代碼:
const body = document.body;
const collapseBtn = document.querySelector(".admin-menu button");
const collapsedClass = "collapsed";
collapseBtn.addEventListener("click", function() {
this.getAttribute("aria-expanded") == "true"
? this.setAttribute("aria-expanded", "false")
: this.setAttribute("aria-expanded", "true");
this.getAttribute("aria-label") == "collapse menu"
? this.setAttribute("aria-label", "expand menu")
: this.setAttribute("aria-label", "collapse menu");
body.classList.toggle(collapsedClass);
});
以及所有相關(guān)的樣式:
/*CUSTOM VARIABLES HERE*/
@media screen and (min-width: 768px) {
.collapsed .page-header {
width: 40px;
}
.collapsed .page-header .admin-menu li > * {
padding: 10px;
}
.collapsed .page-header .logo,
.collapsed .page-header .admin-menu span,
.collapsed .page-header .admin-menu .menu-heading {
display: none;
}
.collapsed .page-header .admin-menu svg {
margin-right: 0;
}
.collapsed .page-header .collapse-btn svg {
transform: rotate(180deg);
}
.collapsed .page-content {
left: 40px;
width: calc(100% - 40px);
}
}
5.在管理菜單項上顯示工具提示
現(xiàn)在,讓我們更進一步,并向可折疊標頭添加另一個新功能。
如前一節(jié)所述,當標題折疊時,菜單鏈接的文本將消失。 這意味著到那時,僅SVG圖標將可見。 因此,讓我們顯示一個工具提示,使用戶可以更好地了解每個鏈接的作用。
為此,每次將菜單鏈接(圖標)懸停在上方時,我們都會向其添加title屬性,其值為純文本。 但是同樣,僅當標題折疊且窗口寬度至少為768px時,才應(yīng)該發(fā)生這種情況。

以下是相應(yīng)JavaScript:
const body = document.body;
const menuLinks = document.querySelectorAll(".admin-menu a");
const collapsedClass = "collapsed";
for (const link of menuLinks) {
link.addEventListener("mouseenter", function() {
body.classList.contains(collapsedClass) &&
window.matchMedia("(min-width: 768px)").matches
? this.setAttribute("title", this.textContent)
: this.removeAttribute("title");
});
}
6.積極響應(yīng)
在寬達767像素的屏幕上,我們的頁面如下所示:

這與我們的側(cè)邊欄安排有很大的不同,對吧? 讓我們重點介紹與臺式機版本相比最重要的區(qū)別:
- 標頭和
.page-content都具有position: static,width: 100%。 nav元素的伸縮方向從column更改為row。- 移動菜單切換按鈕變?yōu)榭梢姟?/li>
- 菜單絕對位于標題下方,并且最初是隱藏的。 每次我們點擊切換按鈕時,它就會變得可見。
- 折疊/展開按鈕和
.greeting元素被隱藏。 .search-and-user部分絕對位于移動菜單切換按鈕的旁邊。
在下面,您可以看到部分響應(yīng)式樣式:
@media screen and (max-width: 767px) {
.page-header,
.page-content {
position: static;
width: 100%;
}
.page-header nav {
flex-direction: row;
}
.page-header .toggle-mob-menu {
display: block;
}
.page-header .admin-menu {
position: absolute;
left: 98px;
top: 57px;
margin-top: 0;
z-index: 2;
border-radius: var(--border-radius);
background: var(--page-header-bgColor);
visibility: hidden;
opacity: 0;
transform: scale(0.95);
transition: all 0.2s;
}
.page-header .admin-menu li:last-child,
.search-and-user .admin-profile .greeting {
display: none;
}
.search-and-user {
position: absolute;
left: 131px;
top: 10px;
padding: 0;
grid-column-gap: 5px;
width: calc(100% - 141px);
border-radius: var(--border-radius);
background: transparent;
}
}
7.切換手機菜單
每次單擊切換按鈕時,菜單狀態(tài)都會改變。 如果擴展,它將崩潰,反之亦然。

在菜單的展開狀態(tài)下,主體將接受生物mob-menu-opened類。 到那時,發(fā)生了以下事情:
- 出現(xiàn)菜單。
- 切換按鈕的
aria-expanded和aria-label屬性值已更新。 另外,其圖標旋轉(zhuǎn)了180度,因此看起來像是展開圖標。
這是必需JavaScript代碼:
const body = document.body;
const toggleMobileMenu = document.querySelector(".toggle-mob-menu");
toggleMobileMenu.addEventListener("click", function() {
this.getAttribute("aria-expanded") == "true"
? this.setAttribute("aria-expanded", "false")
: this.setAttribute("aria-expanded", "true");
this.getAttribute("aria-label") == "open menu"
? this.setAttribute("aria-label", "close menu")
: this.setAttribute("aria-label", "open menu");
body.classList.toggle("mob-menu-opened");
});
以及相關(guān)CSS:
.page-header .toggle-mob-menu svg {
transition: transform 0.2s;
}
.page-header .admin-menu {
transition: all 0.2s;
}
.mob-menu-opened .toggle-mob-menu svg {
transform: rotate(180deg);
}
.mob-menu-opened .page-header .admin-menu {
transform: scale(1);
visibility: visible;
opacity: 1;
}
結(jié)論
就是這樣! 我們成功構(gòu)建了功能齊全的管理儀表板布局。 您將可以在此基礎(chǔ)上擴展以創(chuàng)建各種管理界面。 希望您和我一樣喜歡這次旅行!
最后一點。 我當然不是可訪問性專家,但是我嘗試通過添加一些常見的ARIA屬性使此組件更易于訪問。 在此過程中,我檢查了WordPress和Codepen儀表板以供參考。 代碼中可能還包含其他ARIA屬性。 例如,我排除了負責標識相關(guān)內(nèi)容的aria-controls屬性,但這是因為Aria-Controls是Poop 。
如果我錯過了任何事情,或者您認為某些事情應(yīng)該做的不同,請在下面的評論中告訴我。
一如既往,感謝您的閱讀!
到此這篇關(guān)于使用CSS和Java來構(gòu)建管理儀表盤布局的實例代碼的文章就介紹到這了,更多相關(guān)css 管理儀表盤布局內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!
相關(guān)文章
Flexbox布局模塊旨在提供一個更有效的方式,在一個容器里面去布局分配空間。這篇文章給大家介紹CSS3中的Flex布局,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或2020-04-27
CSS實現(xiàn)動態(tài)圖片的九宮格布局的實例代碼
這篇文章主要介紹了CSS實現(xiàn)動態(tài)圖片的九宮格布局的實例代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-03
css之display屬性之inline-block布局實現(xiàn)詳解
今天學習css樣式的時候發(fā)現(xiàn)很多網(wǎng)站都是用css的display:inline-block這個屬性,這里剛好有篇特別好的解釋,特分享一下2020-03-21
flex布局又稱為彈性布局,任何一個容器都可以指定為flex布局,這篇文章主要介紹了CSS中的flex布局,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參2020-03-19
這篇文章主要介紹了css用Flex布局制作簡易柱狀圖的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起2020-03-17






