思路:
通过:hover伪类或onmouseenter事件在鼠标经过时改变二级菜单的display
关键知识::hover伪类/onmouseenter
1.纯静态版
具体代码:
background: url(images/bg.jpg); background: url(images/libg.jpg) no-repeat; position: absolute;/*定位使其脱离标准文档流*/ <ul class="main clearfix">
具体效果可见:下拉菜单(静态)
2.JavaScript版
onmouseenter和onmouseover区别:
不论鼠标指针穿过被选元素或其子元素,都会触发onmouseover 事件。
例:li有mouseover事件,它有子元素a,经过a时也会触发onmouseover
只有在鼠标指针穿过被选元素时,才会触发 mouseenter 事件。
注意:onmouseenter和onmouseleave配对
可见实例:onmouseenter事件
具体代码:
background: url(images/bg.jpg); background: url(images/libg.jpg) no-repeat; position: absolute;/*定位使其脱离标准文档流*/ <ul class="main clearfix"> var main = document.getElementsByClassName("main")[0]; for(var i=0;i<lis.length;i++){ lis[i].onmouseenter = function(){ this.children[0].style.display = "block"; lis[i].onmouseleave = function(){ this.children[0].style.display = "none";
具体效果可见:下拉菜单(js)
3.jquery版
具体代码:
background: url(images/bg.jpg); background: url(images/libg.jpg) no-repeat; position: absolute;/*定位使其脱离标准文档流*/ <ul class="main clearfix"> <script type="text/javascript" src="jquery-1.11.1.js"></script> // $(".main li").mouseenter(function(){ // console.log($(this).children("ul")); // $(this).children("ul").show(); // $(".main li").mouseleave(function(){ // $(this).children("ul").hide(); // $(".main li").hover(function(){ // $(this).children("ul").show(); // $(this).children("ul").hide(); // $(".main li").hover(function(){ // var that = $(this).children("ul"); // var isShow = that.css('display'); // if(isShow == "block"){ $(".main li").hover(function(){ $(this).children("ul").slideToggle();
具体效果可见:下拉菜单(jquery)
9.5改进jquery代码:
$(".main li").hover(function(){ $(this).children("ul").stop().slideToggle();
针对jquery中动画保留(即执行多次动画效果)的bug
有关stop函数可参考:jQuery之stop 原文的链接:https://blog.csdn.net/lyxuefeng/article/details/82391062
|