<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
#menu {
background-color: #f5f5f5;
}
.sub-menu,
.menu-item {
line-height: 30px;
text-indent: 10px;
}
.sub-menu {
padding-top: 30px;
position: relative;
transition: all 0.3s;
padding-left: 15px;
overflow-y: hidden;
}
.menu-item:hover {
background-color: gainsboro;
}
.sub-title {
background-color: #000;
color: #fff;
position: absolute;
top: 0;
left: 0;
width: 100%;
}
.sub-title:after {
content: '>';
transition: all 0.3s;
transform-origin: center;
position: absolute;
right: 10px;
margin-left: 15px;
width: 30px;
height: 30px;
line-height: 30px;
transform: rotate(90deg);
}
.sub-title-arrow-down:after {
transform: rotate(90deg);
}
.sub-title-arrow-right:after {
transform: rotate(0);
}
</style>
</head>
<body>
<div id="menu"></div>
<script>
const arr = [{
"name": "前端",
"url": "/src/dist/docs/前端",
"child": [{
"name": "html",
"url": "/src/dist/docs/前端/html.html",
"child": []
}]
}, {
"name": "后端",
"url": "/src/dist/docs/后端",
"child": [{
"name": "nodejs",
"url": "/src/dist/docs/后端/nodejs.html",
"child": []
}]
}, {
"name": "文檔",
"url": "/src/dist/docs/文檔",
"child": [{
"name": "doc",
"url": "/src/dist/docs/文檔/doc.html",
"child": []
}, {
"name": "文檔",
"url": "/src/dist/docs/文檔/文檔.html",
"child": [{
"name": "文檔1",
"url": "/src/dist/docs/文檔/文檔.html",
child: []
}, {
"name": "文檔2",
"url": "/src/dist/docs/文檔/文檔.html",
child: [{
"name": "文檔3",
"url": "/src/dist/docs/文檔/文檔.html",
child: []
},
{
"name": "文檔4",
"url": "/src/dist/docs/文檔/文檔.html",
child: []
},
{
"name": "文檔5",
"url": "/src/dist/docs/文檔/文檔.html",
child: []
},
]
}, ]
}]
}]
function renderMenu(arr) {
function renderMenuItem(data) {
let html = ''
data.forEach(item => {
if (!!item.child.length) {
html +=
`<div class="sub-menu"><div class="sub-title">${item.name}</div>${renderMenuItem(item.child)}</div>`
} else {
html += `<div class="menu-item">${item.name}</div>`
}
})
return html
}
const html = renderMenuItem(arr)
const menu = document.querySelector('#menu')
menu.innerHTML = html
const subMenus = document.querySelectorAll('.sub-menu')
subMenus.forEach(item => {
const children = item.querySelectorAll('.menu-item, .sub-title')
item.style.height = (children.length - 1) * 30 + 'px';
const subTitle = item.querySelectorAll('.sub-title')
subTitle.forEach(v => {
v.onclick = function() {
const height = (children.length - 1) * 30 + 'px';
const isFold = parseInt(item.style.height) == 0
v.className = isFold ? 'sub-title sub-title-arrow-down' : 'sub-title sub-title-arrow-right'
item.style.height = isFold ? height : 0
}
})
})
}
renderMenu(arr)
</script>
</body>
</html>
原生js折疊菜單欄
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門菠镇,熙熙樓的掌柜王于貴愁眉苦臉地迎上來冗荸,“玉大人,你說我怎么就攤上這事利耍“霰荆” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵隘梨,是天一觀的道長程癌。 經(jīng)常有香客問我,道長轴猎,這世上最難降的妖魔是什么嵌莉? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮捻脖,結(jié)果婚禮上锐峭,老公的妹妹穿的比我還像新娘中鼠。我一直安慰自己,他們只是感情好沿癞,可當(dāng)我...
- 文/花漫 我一把揭開白布援雇。 她就那樣靜靜地躺著,像睡著了一般椎扬。 火紅的嫁衣襯著肌膚如雪熊杨。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼较沪,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了失仁?” 一聲冷哼從身側(cè)響起尸曼,我...
- 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎萄焦,沒想到半個(gè)月后控轿,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡拂封,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年茬射,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片冒签。...
- 正文 年R本政府宣布票唆,位于F島的核電站朴读,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏惰说。R本人自食惡果不足惜磨德,卻給世界環(huán)境...
- 文/蒙蒙 一缘回、第九天 我趴在偏房一處隱蔽的房頂上張望吆视。 院中可真熱鬧典挑,春花似錦、人聲如沸啦吧。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽授滓。三九已至琳水,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間般堆,已是汗流浹背在孝。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長得像和橙,于是被迫代替她去往敵國和親仔燕。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...