以下是我自己用原生JS写的各种菜单特效,虽然网上一搜一大堆,但我还是喜欢自己来写一写!
这是上一篇:JavaScript实战(带收放动画效果的导航菜单)
下面是经过优化后的完整代码,优化了CSS样式、简化事件函数、减少HTML层级,删了至少20行以上的冗余代码
如果各位前辈有好的建议,记得一定留下你的宝贵意见!

window.onload = function() { //========伸缩动画菜单 var ul = document.getElementById('ul'); if(ul.addEventListener){ ul.addEventListener('mouseover',listener1,true); ul.addEventListener('mouseout',listener2,true); ul.addEventListener('click',listener3,false); }else if(ul.attachEvent){ //兼容IE8及以前版本 ul.attachEvent('onmouseover',listener1,false); ul.attachEvent('onmouseout',listener2,false); ul.attachEvent('onclick',listener3,false); } function listener1(event){ //event = event||window.event; //兼容IE8及以前版本 var target = event.target||event.srcElement; //兼容IE8及以前版本 if(target.tagName.toLowerCase() === 'li') { var div1 = target.getElementsByTagName('div')[0]; div1.style.display = 'block'; div1.style.opacity = 1; } } function listener2(event){ //event = event||window.event; var target = event.target||event.srcElement; if(target.tagName.toLowerCase() === 'li'){ var div1 = target.getElementsByTagName('div')[0]; div1.style.display = 'none'; div1.style.opacity = 0; div1.onmouseover = function(){ div1.style.display = 'block'; div1.style.opacity = 1; }; div1.onmouseout = function(){ div1.style.display = 'none'; div1.style.opacity = 0; }; } } var bool = true; function listener3(event) { var event = event || window.event; var target = event.target || event.srcElement; if (target.className === 'show-hide') { var adiv = target.nextElementSibling; if (window.getComputedStyle(adiv,null).opacity>0.5){bool=false}else{bool=true} var height = 90, changeH, opacity, id; if (bool) { changeH = 0; opacity = 0; var text = target.innerHTML.slice(0,-1); target.innerHTML = text+' -'; (function show() { if (changeH > height) {clearTimeout(id);return} changeH += 5; opacity += 0.06; console.log('opacity:'+adiv.style.opacity+',height :'+adiv.style.height); adiv.style.height = changeH + 'px'; adiv.style.opacity = opacity; adiv.style.display = 'block'; id = setTimeout(function () { clearTimeout(id); show(); }, 16.7); })(); bool = false; } else { changeH = height; opacity = 1; var text = target.innerHTML.slice(0,-1); target.innerHTML = text+' +'; (function hidden() { if (changeH *{ margin: 0; padding: 0; } a,img{border:0;} ul{ position: absolute; top: 20px; left: 30px; z-index: 100; } #ul li{ display: inline-block; position: relative; height: 30px; text-align: center; line-height: 30px; padding: 3px; border: 1px solid gray; border-radius: 10px 10px 0 0; background-color: aliceblue; cursor: pointer; -webkit-transition: all ease-in-out 0.3s; -moz-transition: all ease-in-out 0.3s; -ms-transition: all ease-in-out 0.3s; -o-transition: all ease-in-out 0.3s; transition: all ease-in-out 0.3s; } #ul li:hover{background-color: aquamarine;} .nav-div{ position: absolute; width: 100%; left: -1px; top: 37px; display: none; border: 1px solid gray; border-top: 0; border-radius:0 0 10px 10px; background-color: aliceblue; } .show-hide{ position: relative; display: block; border-radius:0 0 10px 10px; background-color: lightblue; -webkit-transition: all ease-in-out 0.3s; -moz-transition: all ease-in-out 0.3s; -ms-transition: all ease-in-out 0.3s; -o-transition: all ease-in-out 0.3s; transition: all ease-in-out 0.3s; border-bottom: 1px solid gray; } .show-hide:hover{background-color: lavender} .a-div{ background-color: aliceblue; display: none; border-radius:0 0 10px 10px; opacity: 0} .a{ z-index: -1; display: block; text-decoration: none; border-radius:10px; -webkit-transition: all ease-in-out 0.3s; -moz-transition: all ease-in-out 0.3s; -ms-transition: all ease-in-out 0.3s; -o-transition: all ease-in-out 0.3s; transition: all ease-in-out 0.3s; } .a:hover{background-color: lavender}

下面是第二个特效:(具体实现比第一个简单很多,主要注意CSS布局)

*{ margin: 0; padding: 0; } a,img{border:0;} #menu{ position: absolute; top: 30px; left: 0; right: 0; margin: auto; width: 400px; border-left: 1px solid gray; border-top: 1px solid gray; background-color: lemonchiffon; text-align: center; } #menu li{ list-style: none; float: left; width: 99px; height: 30px; line-height: 30px; border-right: 1px solid gray; background-color: burlywood; color: white; -webkit-transition: all ease-in-out 0.5s; -moz-transition: all ease-in-out 0.5s; -ms-transition: all ease-in-out 0.5s; -o-transition: all ease-in-out 0.5s; transition: all ease-in-out 0.5s; } #menu li:hover{ background-color: lemonchiffon; color: #336699; } .contain{ position: absolute; left: -1px; display: none; width: 399px; height: 300px; color: #336699; border-left: 1px solid gray; border-right: 1px solid gray; border-bottom: 1px solid gray; background-color: lemonchiffon; } window.onload = function(){ var menu = document.getElementById('menu'); if(menu.addEventListener){ menu.addEventListener('mouseover',show,false); menu.addEventListener('mouseout',hide,false); }else if(menu.attachEvent){ menu.attachEvent('onmouseover',show,false); menu.attachEvent('onmouseout',hide,false); } function show(event){ var target = event.target||event.srcElement; if(target.tagName.toLowerCase() === 'li'){ target.firstElementChild.style.display = 'block'; }else if(target.parentNode.tagName.toLowerCase() === 'li'){ target.style.display = 'block'; } } function hide(event){ var target = event.target||event.srcElement; if(target.tagName.toLowerCase() === 'li'){ target.firstElementChild.style.display = 'none'; }else if(target.parentNode.tagName.toLowerCase() === 'li'){ target.style.display = 'none'; } } }

  • 苏福的特效1
    111111111111
  • 苏福的特效2
    222222222222
  • 苏福的特效3
    333333333333
  • 苏福的特效4
    444444444444

最新文章

  1. 解决driver.findElement(By)运行到此处报null指针问题
  2. NFC学习 (1)
  3. bzoj专题训练
  4. [CF738D]Sea Battle(贪心)
  5. 最长不下降子序列的O(n^2)算法和O(nlogn)算法
  6. SPL迭代器的工作和代理模式OuterIterator
  7. 为了让vi命令也可以使用vim的配置,需要修改 vi /etc/bashrc 增加一行 alias vi='vim'此时,经过上面配置已经可以显示语法高亮了
  8. Mobile开发之meta篇
  9. RequireJS学习笔记(转)
  10. WordPress教程之判断文章所属分类函数in_category、is_category
  11. DataTable转换实体类
  12. Thread(线程)四
  13. NOI2001 食物链
  14. 我们为什么要搞长沙.NET技术社区(三)
  15. K - Video Reviews Gym - 101755K (二分)
  16. idea 项目打包发布
  17. ef 数据库连接字符串加密
  18. fast neural style transfer图像风格迁移基于tensorflow实现
  19. Verilog HDL与C语言的比较
  20. 19、java内存分配 常量池详解

热门文章

  1. maven -- 学习笔记(一)之maven环境搭建
  2. java向mysql数据库插入数据显示乱码的问题
  3. SharePoint Server 2013开发之旅(一):新的开发平台和典型开发场景介绍
  4. Admin Panel – 非常漂亮的后台管理系统模板
  5. Android Fragment完全解析
  6. Node魔法堂:NPM入了个门
  7. [python基础]关于包,类,模块的那些事儿
  8. position属性absolute与relative 详解
  9. 前端模块化开发之seaJs
  10. LINQ的Any方法