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

實現(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>
//點擊分組,顏色改變,下面的選項出來
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 = '實 向下箭頭-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 //通過添加一個屬性,進行索引
cls[i].flag = 0
}
for (i = 0; i < uls.length; i++) { //利用事件冒泡對具體好友綁定點擊事件
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ù)點擊
</script>
</body>
</html>需要加索引時,一般說通過自定義屬性來設(shè)置
可以將該元素視為一個對象,為期添加一個屬性,就可以進行索引
另外一種可以在for循環(huán),使用閉包原理
相比,加屬性的方法更加方便
以上就是HTML+JS模擬實現(xiàn)QQ下拉菜單效果的詳細內(nèi)容,更多關(guān)于JS QQ下拉菜單的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
js實現(xiàn)a標(biāo)簽超鏈接提交form表單的方法
這篇文章主要介紹了js實現(xiàn)a標(biāo)簽超鏈接提交form表單的方法,涉及javascript針對表單操作的相關(guān)技巧,需要的朋友可以參考下2015-06-06
javascript事件的傳播基礎(chǔ)實例講解(35)
這篇文章主要為大家詳細介紹了javascript事件的傳播基礎(chǔ)實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02
firefox下input type="file"的size是多大
firefox對type="file" 的input的width定義目前是不支持的,但是FF支持size屬性,可以給size設(shè)置一個值,來控制上傳框的大小2011-10-10
使用layui實現(xiàn)樹形結(jié)構(gòu)的方法
今天小編就為大家分享一篇使用layui實現(xiàn)樹形結(jié)構(gòu)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
js實現(xiàn)用于建立新的一行且增加的四個文本框為空的且被禁用
js實現(xiàn)用于建立新的一行且增加的四個文本框為空的且被禁用...2007-04-04

