下拉菜单jQuery实现效果

一 mouseenter跟mouseover事件的区别


<html>
<head>
    <meta charset="UTF-8">
    <script src="js/jquery-1.11.1.min.js"></script>
    <script type="text/javascript">
        x=0;
        y=0;
        $(document).ready(function(){
            $("div.over").mouseover(function(){
                $(".over span").text(x+=1);
            });
            $("div.enter").mouseenter(function(){
                $(".enter span").text(y+=1);
            });
        });
    </script>
</head>
<body>
<p>不论鼠标指针穿过被选元素或其子元素,都会触发 mouseover 事件。</p>
<p>只有在鼠标指针穿过被选元素时,才会触发 mouseenter 事件。</p>
<div class="over" style="background-color:lightgray;padding:20px;width:40%;float:left">
    <h2 style="background-color:white;">被触发的 Mouseover 事件:<span></span></h2>
</div>
<div class="enter" style="background-color:lightgray;padding:20px;width:40%;float:right">
    <h2 style="background-color:white;">被触发的 Mouseenter 事件:<span></span></h2>
</div>
</body>
</html>

区别:
mouseover/mouseout 事件,鼠标经过的时候会触发多次,每遇到一个子元素就会触发一次。
mouseenter/mouseleave事件,鼠标经过的时候只会触发一次。
二 下拉菜单的实现


<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
        ul {
            list-style: none;
        }

        .wrap {
            width: 330px;
            height: 30px;
            margin: 100px auto 0;
            background-image: url(imgs/bg.jpg);
            padding-left: 10px;
        }

        .wrap li {
            float: left;
            width: 100px;
            height: 30px;
            margin-right: 10px;
            position: relative;
        }

        .wrap a {
            color: black;
            text-decoration: none;
            display: block;
            width: 100px;
            height: 30px;
            text-align: center;
            line-height: 30px;
            background-image: url(imgs/libg.jpg);
        }
        .wrap li ul {
            position: absolute;
            display: none;
        }
    </style>
    <script src="js/jquery-1.11.1.min.js"></script>
    <script>
        $(document).ready(function () {

/*          方法一:
            $(".wrap li").mouseenter(function () {
                $(this).children("ul").show();
            });
            $(".wrap li").mouseleave(function () {
                $(this).children("ul").hide();
            });
*/
/*           方法二:
             $(".wrap li").hover(function () {
             $(this).children("ul").show();
             },function () {
             $(this).children("ul").hide();
             });*/
/*              方法三:
                $(".wrap li").hover(function () {
                var $this = $(this).children("ul");
                var isshow=$this.css("display");
                if (isshow === "block"){
                    $this.hide();
                }else {
                    $this.show();
                }
            });*/
            //方法四:
             $(".wrap li").hover(function () {
             $(this).children("ul").slideToggle();
             });

        });
    </script>
</head>
<body>
<div class="wrap">
    <ul>
        <li>
            <a href="#">一级菜单1</a>
            <ul>
                <li><a href="#">二级菜单1</a></li>
                <li><a href="#">二级菜单2</a></li>
                <li><a href="#">二级菜单3</a></li>
            </ul>
        </li>
        <li>
            <a href="#">一级菜单1</a>
            <ul>
                <li><a href="#">二级菜单1</a></li>
                <li><a href="#">二级菜单2</a></li>
                <li><a href="#">二级菜单3</a></li>
            </ul>
        </li><li>
        <a href="#">一级菜单1</a>
        <ul>
            <li><a href="#">二级菜单1</a></li>
            <li><a href="#">二级菜单2</a></li>
            <li><a href="#">二级菜单3</a></li>
        </ul>
    </li>
    </ul>
</div>
</body>
</html>

总结:
1 设置div,并在div中利用<ul>以及子标签前<li><a>标签写出基本菜单
2 设置菜单样式

  • 清除内外边距以及列表样式
  • 设置div大小以及位置,背景
  • 设置<li>标签样式,左浮动,大小,位置,定位
  • 设置<a>标签样式,颜色,去掉下划线,大小,字体位置,背景,定位
  • 设置<li>标签下,二级菜单栏<ul>标签定位
    3 二级菜单效果实现
  • 方法一
            $(".wrap li").mouseenter(function () {
                $(this).children("ul").show();
            });
            $(".wrap li").mouseleave(function () {
                $(this).children("ul").hide();
            });
  • 方法二
 $(".wrap li").hover(function () {
             $(this).children("ul").show();
             },function () {
             $(this).children("ul").hide();
             });
  • 方法三
 $(".wrap li").hover(function () {
                var $this = $(this).children("ul");
                var isshow=$this.css("display");
                if (isshow === "block"){
                    $this.hide();
                }else {
                    $this.show();
                }
  • 方法四
$(".wrap li").hover(function () {
             $(this).children("ul").slideToggle();
             });

三 DOM对象跟jQuery对象相互转换

jQuery对象转换成DOM对象:

方式一:$(“#btn”)[0]

方式二:$(“#btn”).get(0)

DOM对象转换成jQuery对象:

$(document) -> 把DOM对象转成了jQuery对象
var btn = document.getElementById(“bt n”);

btn     -> $(btn);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • (续jQuery基础(1)) 第5章 DOM节点的复制与替换 (1)DOM拷贝clone() 克隆节点是DOM的常...
    凛0_0阅读 5,212评论 0 8
  • 1.JQuery 基础 改变web开发人员创造搞交互性界面的方式。设计者无需花费时间纠缠JS复杂的高级特性。 1....
    LaBaby_阅读 4,914评论 0 1
  • 1.JQuery 基础 改变web开发人员创造搞交互性界面的方式。设计者无需花费时间纠缠JS复杂的高级特性。 1....
    LaBaby_阅读 5,239评论 0 2
  • DOM创建节点及节点属性 通过JavaScript可以很方便的获取DOM节点,从而进行一系列的DOM操作。但实际上...
    阿r阿r阅读 4,622评论 0 9
  • 1:jQuery节点创建与属性的处理 创建元素节点:可以有几种方式,后面会慢慢接触。常见的就是直接把这个节点的结构...
    码农小杨阅读 3,751评论 0 1