美文网首页程序员
下拉菜单jQuery实现效果

下拉菜单jQuery实现效果

作者: 常威爆打来福 | 来源:发表于2017-07-14 12:53 被阅读0次

一 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实现效果

    本文链接:https://www.haomeiwen.com/subject/vuvnhxtx.html