GithubHelp home page GithubHelp logo

dropdownmenu's Introduction

使用HTML和CSS、JavaScript、jQuery三种方式实现下拉菜单

HTML结构:

<div id="menu">
    <ul>
        <li><a href="#">首页</a></li>
        <li><a href="#">课程大厅</a>
            <ul>
                <li><a href="#">HTML</a></li>
                <li><a href="#">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>

1. 使用HTML和CSS

#menu > ul > li:hover > ul {
    display: block;
}

#menu > ul > li > ul {
    display: none;
}

2. 使用JavaScript

window.onload = function () {
        var lists = document.getElementsByTagName("li");
        for (var i = 0; i < lists.length; i++) {
            lists[i].onmouseover = function () {
                var ul = this.getElementsByTagName("ul")[0];
                if (ul) {
                    ul.style.display = "block";
                }
            };
            lists[i].onmouseleave = function () {
                var ul = this.getElementsByTagName("ul")[0];
                if (ul) {
                    ul.style.display = "none";
                }
            };
        }
};

3. 使用jQuery

$(function () {
        var timer1;
        $("#menu > ul > li").mouseover(function () {
            var _self = $(this);
            timer = setTimeout(function () {
                _self.find('ul').show(100);
            }, 200);

        }).mouseleave(function () {
            var _self = $(this);
            clearTimeout(timer);
            _self.find('ul').hide(100);
        });
});

dropdownmenu's People

Contributors

qiaoer2017 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.