百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>JS教程> js自定义QQ菜单效果的教程
分享文章到:

js自定义QQ菜单效果的教程

发布时间:01/15 来源: 浏览: 关键词:
小编推荐的这篇文章介绍了js自定义QQ菜单效果的教程,非常简单易懂,有兴趣的同学可以参考一下

QQ菜单大家都见过,这样有以下的功能:
1.点击我的好友,会展示下拉出具体的好友
2.再点击我的好友,会收缩
3.首次点击具体的某个好友,只有当前这个好友高亮
4.再次点击这个好友时,高亮状态就消失了

还是瞄一眼效果图吧:

最后代码

<html>

<head>

<metahttp-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 ( vari=0; i < aUl.length; i++ ) {

 aLi=aUl[i].getElementsByTagName('li');

 for ( varj=0; j < aLi.length; j++ ) {

  arr.push(aLi[j]);

 }

 }

 // 遍历aLi

 for ( vari=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>

<ulid="list">

 <liclass="lis">

 <h2>我的好友</h2>

 <ul>

  <li>张三1</li>

  <li>张三2</li>

  <li>张三3</li>

 </ul>

 </li>

 <liclass="lis">

 <h2>企业好友</h2>

 <ul>

  <li>李四1</li>

  <li>李四2</li>

  <li>李四3</li>

 </ul>

 </li>

 <liclass="lis">

 <h2>黑名单</h2>

 <ul>

  <li>王五1</li>

  <li>王五2</li>

 </ul>

 </li>

</ul>

</body>

</html>

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有2人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板