首页 > Web开发 > 详细

js使用面向对象编写下拉菜单

时间:2019-02-12 12:52:59      阅读:145      评论:0      收藏:0      [点我收藏+]
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>无标题文档</title>
    <style type="text/css">
        * {
            padding: 0;
            margin: 0;
            list-style: none;
        }

        .all {
            width: 330px;
            height: 30px;
            background: url(img/bg.jpg) no-repeat;
            margin: 100px auto;
            line-height: 30px;
            text-align: center;
            padding-left: 10px;
            margin-bottom: 0;
        }

        .all ul li {
            width: 100px;
            height: 30px;
            background: url(img/libg.jpg);
            float: left;
            margin-right: 10px;
            position: relative;
            cursor: pointer;
        }

        .all ul ul {
            position: absolute;
            left: 0;
            top: 30px;
            display: none;
        }
    </style>
</head>

<body>
    <div class="all">
        <ul id="list">
            <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>
    window.onload = function () {
        new ListMenu().init();
    }

    function ListMenu() {
        this.list = list.children;
        this.init = function () {
            for (let i = 0; i < this.list.length; i++) {
                this.list[i].onmouseenter = function () {
                    this.show(this.list[i].children[0]);
                }.bind(this)
                this.list[i].onmouseleave = function () {
                    this.hide(this.list[i].children[0]);
                }.bind(this)
            }
        }
        this.show = function (obj) {
            obj.style.display = "block";
        }
        this.hide = function (obj) {
            obj.style.display = "none";
        }
    }
</script>

 

js使用面向对象编写下拉菜单

原文:https://www.cnblogs.com/1512344358qq/p/10364585.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!