js下拉菜单怎么做 求大神

JavaScript015

js下拉菜单怎么做 求大神,第1张

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8">

<title>无标题文档</title>

<style type="text/css">

*{ padding:0margin:0list-style:none}

.all{ width:330pxheight:30pxbackground:url(img/bg.jpg) no-repeatmargin:100px autoline-height:30pxtext-align:centerpadding-left:10pxmargin-bottom:0}

.all ul li{ width:100pxheight:30pxbackground:url(img/libg.jpg)float:leftmargin-right:10pxposition:relativecursor:pointer}

.all ul ul{ position:absoluteleft:0top:30pxdisplay:none}

</style>

</head>

<body>

<div class="all" id="list">

<ul>

<li>一级菜单

<ul>

<li>二级菜单</li>

<li>二级菜单</li>

<li>二级菜单</li>

</ul>

</li>

<li>一级菜单

<ul>

<li>二级菜单</li>

<li>二级菜单</li>

<li>二级菜单</li>

</ul>

</li>

<li>一级菜单

<ul>

<li>二级菜单</li>

<li>二级菜单</li>

<li>二级菜单</li>

</ul>

</li>

</ul>

</div>

</body>

</html>

<script>

// 获取对象 遍历对象操作 显示模块 隐藏模块

function List(id) { // 获取对象

this.id = document.getElementById(id) // 取 id 值

this.lis = this.id.children[0].children // 获取一级菜单所有的li

}

// init 初始化

List.prototype.init = function() { // 遍历所有的li 显示和隐藏

var that = this

for(var i=0i<this.lis.lengthi++)

{

this.lis[i].index = i

this.lis[i].onmouseover = function() {

that.show(this.children[0]) // 显示出来

}

this.lis[i].onmouseout = function() {

that.hide(this.children[0]) // 隐藏起来

}

}

}

// 显示模块

List.prototype.show = function(obj) {

obj.style.display = "block"

}

// 隐藏模块

List.prototype.hide = function(obj) {

obj.style.display = "none"

}

var list = new List("list") // 实例化了一个对象 叫 list

list.init()

</script>

<style>

        .clearfix::after {

            display: block

            content: ""

            clear: both

        }

        ul,

        li {

            list-style: none

        }

        .nav>li {

            float: left

            margin: 0 10px

        }

        .nav>li ul {

            display: none

        }

    </style>

</head>

<body>

    <ul class="nav clearfix">

        <li>

            <a href="">微博</a>

            <ul>

                <li>私信</li>

                <li>评论</li>

                <li>@我</li>

            </ul>

        </li>

        <li>

            <a href="">微博</a>

            <ul>

                <li>私信</li>

                <li>评论</li>

                <li>@我</li>

            </ul>

        </li>

        <li>

            <a href="">微博</a>

            <ul>

                <li>私信</li>

                <li>评论</li>

                <li>@我</li>

            </ul>

        </li>

        <li>

            <a href="">微博</a>

            <ul>

                <li>私信</li>

                <li>评论</li>

                <li>@我</li>

            </ul>

        </li>

    </ul>

    <script>

        //案例分析:导航栏里面的li都要有鼠标经过效果 所以需要循环注册鼠标事件

        //核心原理:当鼠标经过li里面的第二个孩子ul显示 当鼠标离开 ul隐藏

        var nav = document.querySelector('.nav')

        var lis = nav.children

        for (var i = 0 i < lis.length i++) {

            lis[i].onmouseover = function() {

                this.children[1].style.display = 'block'

            }

            lis[i].onmouseout = function() {

                this.children[1].style.display = 'none'

            }

        }

    </script>