<html>
<head>
<title>伸縮的菜單</title>
<style>
<!--
body{
background-color:#ffdee0;
}
#navigation {
width:200px;
font-family:Arial;
}
#navigation > ul {
list-style-type:none; /* 不顯示項(xiàng)目符號(hào) */
margin:0px;
padding:0px;
}
#navigation > ul > li {
border-bottom:1px solid #ED9F9F; /* 添加下劃線 */
}
#navigation > ul > li > a{
display:block; /* 區(qū)塊顯示 */
padding:5px 5px 5px 0.5em;
text-decoration:none;
border-left:12px solid #711515; /* 左邊的粗紅邊 */
border-right:1px solid #711515; /* 右側(cè)陰影 */
}
#navigation > ul > li > a:link, #navigation > ul > li > a:visited{
background-color:#c11136;
color:#FFFFFF;
}
#navigation > ul > li > a:hover{ /* 鼠標(biāo)經(jīng)過(guò)時(shí) */
background-color:#990020; /* 改變背景色 */
color:#ffff00; /* 改變文字顏色 */
}
/* 子菜單的CSS樣式 */
#navigation ul li ul{
list-style-type:none;
margin:0px;
padding:0px 0px 0px 0px;
}
#navigation ul li ul li{
border-top:1px solid #ED9F9F;
}
#navigation ul li ul li a{
display:block;
padding:3px 3px 3px 0.5em;
text-decoration:none;
border-left:28px solid #a71f1f;
border-right:1px solid #711515;
}
#navigation ul li ul li a:link, #navigation ul li ul li a:visited{
background-color:#e85070;
color:#FFFFFF;
}
#navigation ul li ul li a:hover{
background-color:#c2425d;
color:#ffff00;
}
#navigation ul li ul.myHide{ /* 隱藏子菜單 */
display:none;
}
#navigation ul li ul.myShow{ /* 顯示子菜單 */
display:block;
}
-->
</style>
<script language="javascript">
function change(){
//通過(guò)父元素li,找到兄弟元素ul
var oSecondDiv = this.parentNode.getElementsByTagName("ul")[0];
//CSS交替更換來(lái)實(shí)現(xiàn)顯、隱
if(oSecondDiv.className == "myHide")
oSecondDiv.className = "myShow";
else
oSecondDiv.className = "myHide";
}
window.onload = function(){
var oUl = document.getElementById("listUL"); //一級(jí)菜單的ul標(biāo)簽
var aLi = oUl.childNodes; //子元素
var oA;
for(var i=0;i<aLi.length;i++){
//如果子元素為li,且這個(gè)li有子菜單ul
if(aLi[i].tagName == "LI" && aLi[i].getElementsByTagName("ul").length){
oA = aLi[i].firstChild; //找到超鏈接
oA.onclick = change; //動(dòng)態(tài)添加點(diǎn)擊函數(shù)
}
}
}
</script>
</head>
<body>
<div id="navigation">
<ul id="listUL">
<li><a href="#">Home</a></li>
<li><a href="#">News</a>
<ul class="myHide">
<li><a href="#">Lastest News</a></li>
<li><a href="#">All News</a></li>
</ul>
</li>
<li><a href="#">Sports</a>
<ul class="myHide">
<li><a href="#">Basketball</a></li>
<li><a href="#">Football</a></li>
<li><a href="#">Volleyball</a></li>
</ul>
</li>
<li><a href="#">Weather</a>
<ul class="myHide">
<li><a href="#">Today's Weather</a></li>
<li><a href="#">Forecast</a></li>
</ul>
</li>
<li><a href="#">Contact Me</a></li>
</ul>
</div>
</body>
</html>
上菜單為二級(jí)菜單,伸縮菜單是在一級(jí)菜單<li>下點(diǎn)擊實(shí)現(xiàn)的
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>伸縮菜單</title>
<meta name="keywords" content="關(guān)鍵字列表" />
<meta name="description" content="網(wǎng)頁(yè)描述" />
<link rel="stylesheet" type="text/css" href="" />
<style type="text/css">
body,p,ul,li{padding:0px;margin:0px;}
ul li{list-style:none;}
body{font-size:13px;}
.menu{
width:210px;
margin:50px auto;
border:1px solid #ccc;
}
.menu p{
height:25px;
line-height:25px;
font-weight:bold;
background:#eee;
border-bottom:1px solid #ccc;
padding-left:5px;
cursor:pointer;
}
.menu ul li{
height:24px;
line-height:24px;
padding-left:5px;
}
</style>
<script type="text/javascript">
//分析思路
//當(dāng)頁(yè)面加載完成后
//獲取到所有的p元素 獲取到所有的ul元素
//給每一個(gè)p元素綁定一個(gè)onclick事件
//判斷每一個(gè)p元素對(duì)應(yīng)的ul是否是隱藏或者是顯示
window.onload = function(){
//獲取id=menu對(duì)象
var div_obj = document.getElementById("menu");
//獲取所有的p元素
var ps_obj = div_obj.getElementsByTagName("p");
var ps_length = ps_obj.length;
//獲取到所有的ul元素
var uls_obj = div_obj.getElementsByTagName("ul");
//給每一個(gè)p元素綁定一個(gè)onclick事件
for(var i=0;i<ps_length;i++){
ps_obj[i].id = i; //給每一個(gè)p元素加上一個(gè)標(biāo)識(shí)
ps_obj[i].onclick = function(){
//判斷對(duì)應(yīng)的ul是否是顯示或者隱藏
if(uls_obj[this.id].style.display == "none"){
uls_obj[this.id].style.display = "block";
}else{
uls_obj[this.id].style.display = "none";
}
}
}
}
</script>
</head>
<body>
<div id="menu" class="menu">
<div>
<p>web前端</p>
<ul style="display:none;">
<li>HTML</li>
<li>DIV+CSS</li>
<li>JAVASCRIPT</li>
<li>jQuery</li>
<li>Bootstrap</li>
</ul>
</div>
<div>
<p>PHP+MYSQL核心編程</p>
<ul style="display:none;">
<li>PHP</li>
<li>MYSQL</li>
<li>HTTP協(xié)議</li>
<li>PHP繪圖技術(shù)</li>
</ul>
</div>
<div>
<p>PHP高級(jí)</p>
<ul style="display:none;">
<li>XML編程</li>
<li>AJAX</li>
<li>MVC</li>
</ul>
</div>
</div>
</body>
</html>
類似QQ伸縮菜單:
面試官:同學(xué)考你一個(gè)簡(jiǎn)單css內(nèi)容,寫一個(gè)可展開(kāi)列表。
我:笑出了聲。心想真的會(huì)出這么簡(jiǎn)單的內(nèi)容哈哈哈!
面試官:同學(xué)不能用js哦,如果可以用stylus編寫就更好啦!
我:小腦剎那間萎縮了......
Stylus 是一種 CSS 預(yù)處理器。預(yù)處理器是一種腳本語(yǔ)言,它擴(kuò)展了 CSS 的功能,使得編寫 CSS 更加高效、靈活和強(qiáng)大。Stylus 特別之處在于其簡(jiǎn)潔而靈活的語(yǔ)法,它允許開(kāi)發(fā)者使用變量、嵌套規(guī)則、混合(Mixins)、繼承、操作符、函數(shù)以及條件語(yǔ)句等高級(jí)功能來(lái)編寫樣式代碼。
接下來(lái),讓我們通過(guò)一個(gè)騰訊的面試題,來(lái)更加深層次認(rèn)識(shí)這個(gè)stylus語(yǔ)言帶給css的便捷。雖然考察的是純html+css內(nèi)容,要想純html+css達(dá)到完美可卻不簡(jiǎn)單。
有三個(gè)列表,并且可以展開(kāi)和收縮,這題目看起來(lái)簡(jiǎn)單,但是且聽(tīng)我細(xì)細(xì)道來(lái),你會(huì)發(fā)現(xiàn)里面有很多的秘密!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>純css菜單</title>
<link rel="stylesheet" href="1.css">
</head>
<body>
<div class="accordion">
<input type="checkbox" id="collapse1" hidden>
<input type="checkbox" id="collapse2" hidden>
<input type="checkbox" id="collapse3" hidden>
<!-- div替代品 html5語(yǔ)義化標(biāo)簽 SEO比較重要 -->
<article>
<label for="collapse1">列表1</label>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容4</p>
</article>
<article>
<label for="collapse2">列表2</label>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容4</p>
</article>
<article>
<label for="collapse3">列表3</label>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容4</p>
</article>
</div>
</body>
</html>
首先我們要進(jìn)行stylus語(yǔ)言環(huán)境的配置,并引入其生成的css文件,配置好之后,我們先看看html部分。
到此我們html部分就結(jié)束啦,我們重點(diǎn)講解一下css部分。
* {
margin: 0;
padding: 0;
}
.accordion {
width: 300px;
}
.accordion article {
cursor: pointer;
}
.accordion article + article {
margin-top: 5px;
}
.accordion label {
display: block;
height: 40px;
padding: 0 20px;
background-color: #f00;
cursor: pointer;
line-height: 40px;
font-size: 16px;
color: #fff;
}
.accordion p {
overflow: hidden;
padding: 0 20px;
border: 1px solid #f66;
border-top: none;
border-bottom-width: 0;
max-height: 0;
line-height: 30px;
transition: all 500ms;
}
.accordion input:nth-child(1):checked ~ article:nth-of-type(1) p,
.accordion input:nth-child(2):checked ~ article:nth-of-type(2) p,
.accordion input:nth-child(3):checked ~ article:nth-of-type(3) p {
max-height: 600px;
}
<生成css>
---------------------------
<書寫的stylus>
*
margin 0
padding 0
.accordion
width 300px
article
cursor pointer
& + article
margin-top 5px
label
display block
height 40px
padding 0 20px
background-color red
cursor pointer
line-height 40px
font-size 16px
color #fff
p
overflow: hidden
padding: 0 20px
border: 1px solid #f66
border-top: none
border-bottom-width 0
max-height: 0
line-height 30px
transition: all 500ms
input
&:nth-child(1):checked ~ article:nth-of-type(1) p ,
&:nth-child(2):checked ~ article:nth-of-type(2) p ,
&:nth-child(3):checked ~ article:nth-of-type(3) p
max-height: 600px
作者:落雪遙夏
鏈接:https://juejin.cn/post/7379873506543616010
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。