利用JS实现新浪下拉菜单

JavaScript019

利用JS实现新浪下拉菜单,第1张

<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>

你这个效果的下拉菜单,不需要用 JavaScript,只用CSS就能实现。

加上一句

li:hover ul{ display:block}

就行了。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

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

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

<style type="text/css">* {

margin: 0px

padding: 0px

}

body {

font-family: Verdana, Geneva, sans-serif

font-size: 14px

}

#nav {

width: 600px

height: 40px

background-color: #eee

margin: 0 auto

}

ul {

list-style: none

}

ul li {

float: left

line-height: 40px

text-align: center

width: 100px

}

a {

text-decoration: none

color: #000

display: block

}

a:hover {

color: #F00

background-color: #666

}

ul li ul li {

float: none

background-color: #eee

margin: 2px 0px

}

ul li ul {

display: none

}

li:hover ul{ display:block}

</style>

</head>

<body>

<div id="nav">

<ul>

<li><a href="#">首页</a></li>

<li><a href="#">课程大厅</a>

<ul>

<li><a href="#">JavaScript</a></li>

<li><a href="#">Html/CSS</a></li>

</ul>

</li>

<li><a href="#">学习中心</a>

<ul>

<li><a href="#">视频学习</a></li>

<li><a href="#">实例练习</a></li>

<li><a href="#">问与答</a></li>

</ul>

</li>

<li><a href="#">经典案例</a></li>

<li><a href="#">关于我们</a></li>

</ul>

</div>

</body>

</html>