HTML+JS模擬實(shí)現(xiàn)QQ下拉菜單效果
功能:
1、點(diǎn)擊我的好友會展開下拉出具體的好友
2、再次點(diǎn)擊,會折疊內(nèi)容
3、首次點(diǎn)擊某個具體的好友,只有當(dāng)前這個好友高亮
4、再次點(diǎn)擊這個好友時,高亮狀態(tài)就會消失
主要練習(xí):js綁定事件
慢慢修改小細(xì)節(jié):再次點(diǎn)擊,會折疊內(nèi)容時,里面的高亮要全部取消

實(shí)現(xiàn)代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: auto;
padding: 0px
}
.content {
position: relative;
top: 40px;
width: 280px;
height: auto;
border: 1px solid black;
margin: 0 auto
}
span {
display: inline-block;
border-bottom: 1px dotted grey;
width: 100%;
background-color: red;
}
li {
padding-left: 20px;
list-style: none;
}
ul {
display: none
}
</style>
</head>
<body>
<div class="box1">
<div class="content">
<div class="cl1"><span>
<img src="右箭頭.png" alt="" style="width:20px;height:20px;vertical-align: text-bottom;"> 我的好友
</span>
<ul>
<li>lili</li>
<li>zhangsan</li>
<li>uncle</li>
</ul>
</div>
<div class="cl1">
<span>
<img src="右箭頭.png" alt="" style="width:20px;height:20px;vertical-align: text-bottom;">企業(yè)好友
</span>
<ul>
<li>lili</li>
<li>zhangsan</li>
<li>uncle</li>
</ul>
</div>
<div class="cl1"><span>
<img src="右箭頭.png" alt="" style="width:20px;height:20px;vertical-align: text-bottom;">黑名單
</span>
<ul>
<li>lili</li>
<li>zhangsan</li>
<li>uncle</li>
<li>unle</li>
<li>une</li>
</ul>
</div>
</div>
</div>
<script>
//點(diǎn)擊分組,顏色改變,下面的選項(xiàng)出來
var cls = document.querySelectorAll('span')
var uls = document.querySelectorAll('ul')
for (i = 0; i < cls.length; i++) {
cls[i].addEventListener('click', function() {
if (this.flag == 0) {
this.style.backgroundColor = 'skyblue'
this.children[0].src = '實(shí) 向下箭頭-01.png'
console.log(uls[this.index]);
uls[this.index].style.display = 'block'
this.flag = 1
} else if (this.flag == 1) {
this.style.backgroundColor = 'red'
this.children[0].src = '右箭頭.png'
console.log(uls[this.index]);
uls[this.index].style.display = 'none'
this.flag = 0
//需要把li全部取消高亮
for (j = 0; j < uls.length; j++) {
for (m = 0; m < uls[j].children.length; m++) {
uls[j].children[m].style.backgroundColor = 'white'
uls[j].children[m].states = 0
}
}
}
})
cls[i].index = i //通過添加一個屬性,進(jìn)行索引
cls[i].flag = 0
}
for (i = 0; i < uls.length; i++) { //利用事件冒泡對具體好友綁定點(diǎn)擊事件
uls[i].addEventListener('click', function(e) {
console.log(e.target);
console.log(e.target.states);
if (e.target.states == 0) {
// this.style.backgroundColor = 'pink'//不能用this,這里的this指向的是uls[i]
e.target.style.backgroundColor = 'pink'
e.target.states = 1
} else if (e.target.states == 1) {
// this.style.backgroundColor = 'pink'//不能用this,這里的this指向的是uls[i]
e.target.style.backgroundColor = 'white'
e.target.states = 0
}
})
console.log(uls[i].children.length);
for (j = 0; j < uls[i].children.length; j++) {
uls[i].children[j].states = 0
}
}
///關(guān)鍵在于第二次重復(fù)點(diǎn)擊
</script>
</body>
</html>需要加索引時,一般說通過自定義屬性來設(shè)置
可以將該元素視為一個對象,為期添加一個屬性,就可以進(jìn)行索引
另外一種可以在for循環(huán),使用閉包原理
相比,加屬性的方法更加方便
以上就是HTML+JS模擬實(shí)現(xiàn)QQ下拉菜單效果的詳細(xì)內(nèi)容,更多關(guān)于JS QQ下拉菜單的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
javascript實(shí)現(xiàn)獲取圖片大小及圖片等比縮放的方法
這篇文章主要介紹了javascript實(shí)現(xiàn)獲取圖片大小及圖片等比縮放的方法,涉及javascript針對圖形圖像相關(guān)屬性獲取與修改相關(guān)操作技巧,需要的朋友可以參考下2016-11-11
詳解V8是如何執(zhí)行一段JavaScript代碼原理
這篇文章主要為大家介紹了詳解V8是如何執(zhí)行一段JavaScript代碼原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
JavaScript實(shí)現(xiàn)身份證驗(yàn)證代碼實(shí)例
這篇文章主要介紹了JavaScript實(shí)現(xiàn)身法證驗(yàn)證代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08
JavaScript中將一個值轉(zhuǎn)換為字符串的方法分析[譯]
在JavaScript中,主要有三種方法能讓任意值轉(zhuǎn)換為字符串.本文講解了每種方法以及各自的優(yōu)缺點(diǎn)2012-09-09
深入理解JavaScript 中的執(zhí)行上下文和執(zhí)行棧
這篇文章主要介紹了JavaScript 中的執(zhí)行上下文和執(zhí)行棧的相關(guān)知識,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-10-10
JavaScript原生節(jié)點(diǎn)操作小結(jié)
本文主要介紹了JavaScript原生節(jié)點(diǎn)操作的相關(guān)知識。具有一定的參考價值,下面跟著小編一起來看下吧2017-01-01

