js自定義QQ菜單效果
更新時間:2017年01月10日 11:43:55 作者:光明大神棍
這篇文章主要為大家詳細介紹了js自定義QQ菜單,具有收縮,下拉等功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
QQ菜單大家都見過,這樣有以下的功能:
1.點擊我的好友,會展示下拉出具體的好友
2.再點擊我的好友,會收縮
3.首次點擊具體的某個好友,只有當前這個好友高亮
4.再次點擊這個好友時,高亮狀態(tài)就消失了
還是瞄一眼效果圖吧:

最后代碼
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
ul,h2 {padding:0;margin:0;}
li{list-style:none;}
#list{width:200px;margin:0 auto;border:1px solid #000;}
#list{width:200px;}
#list h2{width:200px;height:30px;line-height:30px;text-align:center;background:url(img/ico1.gif) no-repeat 20% 50% #6699FF;text-indent:24px;border-bottom:1px dotted #000;}
#list h2.active{background:url(img/ico2.gif) no-repeat 20% 50% #FFCC99;}
#list ul{width:200px;text-align:center;display:none;}
#list ul li{width:200px;height:30px;line-height:30px;}
#list ul li.highlight{background:#FF66FF;}
</style>
<title>無標題文檔</title>
<script>
window.onload = function () {
var oUl = document.getElementById('list');
var oH2 = oUl.getElementsByTagName('h2');
var aUl = oUl.getElementsByTagName('ul');
var aLi = [];
var arr = [];
var oldLi = null;
var num = 0;
// 點擊菜單顯示隱藏
for( var i = 0; i < oH2.length; i++ ) {
oH2[i].index = i;
oH2[i].onclick = function () {
if (this.className === '') {
aUl[this.index].style.display = 'block';
this.className = 'active';
} else {
aUl[this.index].style.display = 'none';
this.className = '';
}
}
}
// 獲取菜單中的每個li
for ( var i = 0; i < aUl.length; i++ ) {
aLi = aUl[i].getElementsByTagName('li');
for ( var j = 0; j < aLi.length; j++ ) {
arr.push(aLi[j]);
}
}
// 遍歷aLi
for ( var i = 0; i < arr.length; i++ ) {
arr[i].onOff = false;
arr[i].onclick = function () {
// 當上一個點擊的li不是當前點擊的Li
if (oldLi && oldLi !== this) {
oldLi.className = '';
oldLi.onOff = false;
}
this.className = this.onOff ? '' : 'highlight';
this.onOff = !this.onOff;
oldLi = this;
}
}
}
</script>
</head>
<body>
<ul id="list">
<li class="lis">
<h2>我的好友</h2>
<ul>
<li>張三1</li>
<li>張三2</li>
<li>張三3</li>
</ul>
</li>
<li class="lis">
<h2>企業(yè)好友</h2>
<ul>
<li>李四1</li>
<li>李四2</li>
<li>李四3</li>
</ul>
</li>
<li class="lis">
<h2>黑名單</h2>
<ul>
<li>王五1</li>
<li>王五2</li>
</ul>
</li>
</ul>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS函數(shù)參數(shù)的傳遞與同名參數(shù)實例分析
這篇文章主要介紹了JS函數(shù)參數(shù)的傳遞與同名參數(shù),結(jié)合實例形式分析了JS函數(shù)參數(shù)的傳遞與同名參數(shù)相關(guān)原理、使用技巧與操作注意事項,需要的朋友可以參考下2020-03-03
javascript數(shù)組中的reduce方法和pop方法
這篇文章主要介紹了javascript數(shù)組中的reduce方法和pop方法,文章內(nèi)容介紹詳細,具有一定的參考價值需要的小伙伴可以參考一下,希望對你的學(xué)習(xí)有所幫助2022-03-03
Javascript 更新 JavaScript 數(shù)組的 uniq 方法
2008-01-01
Bootstrap Table實現(xiàn)定時刷新數(shù)據(jù)的方法
這篇文章主要介紹了Bootstrap Table實現(xiàn)定時刷新數(shù)據(jù)的方法,在這里小編比較推薦使用第二種方法,需要的朋友參考下吧2018-08-08

