如何使这个菜单纯CSS?

时间:2015-06-11 07:58:17

标签: javascript jquery html css

如何制作这个纯CSS(没有javascript)?

这是一个小提琴:http://jsfiddle.net/q646Ljg6/4/

HTML:

<div id="navigation" class="navigation">
    <div id="dropmenu" class="dropmenu">
        <login>
            <ul>
                <li> <a href="#" ><span id="cart-button" class="flaticon flaticon-cart" aria-hidden="true"></span></a>

                </li>
                <li> <a href="javascript:void(0);">Dropdown C</a>

                    <ul>
                        <li><a href="#" >Option 1</a>
                        </li>
                        <li><a href="#" >Option 2</a>
                        </li>
                        <li><a href="#" >Option 3</a>
                        </li>
                    </ul>
                </li>
            </ul>
        </login>
        <ul>
            <li> <a href="/">Home</a>

            </li>
            <li> <a href="javascript:void(0);" >Dropdown A</a>

                <div class="sub-menu">
                    <div class="sub-menu-inner">
                        <div>
                            <ul>
                                <li><a href="#" >Dropdown A - 1</a>
                                </li>
                                <li><a href="#" >Dropdown A - 2</a>
                                </li>
                            </ul>
                        </div>
                    </div>
                    <!-- /sub-menu-inner -->
                </div>
                <!-- /sub-menu -->
            </li>

            <li> <a href="javascript:void(0);" >Dropdown B</a>

                <div class="sub-menu">
                    <div class="sub-menu-inner">
                        <div>
                            <ul>
                                <li> <a href="#" >Dropdown B - 1</a>

                                    <ul>
                                        <li><a href="#" >Dropdown B - 1 - 1</a>
                                        </li>
                                    </ul>
                                </li>
                                <li><a href="#" >Dropdown B - 2</a>
                                    <ul>
                                        <li><a href="#" >Dropdown B - 2 - 1</a>
                                        </li>
                                        <li><a href="#" >Dropdown B - 2 - 2</a>
                                        </li>
                                    </ul>
                                </li>
                            </ul>
                        </div>
                    </div>
                    <!-- /sub-menu-inner -->
                </div>
                <!-- /sub-menu -->
            </li>
            <li style="float:none;overflow:hidden;">
                <div class="dark width-max"> <span>
                        <input id="quick-search" data-load="content" data-url="/quicksearch"  class="search" type="text" placeholder="Quick Search" maxlength="50" autocomplete="off">
                        <div id="search-results" class="search-results"></div>
                    </span>

                </div>
            </li>
        </ul>
    </div>
</div>
<div>
content content content content content content content content content content content content content content content content content content content content content content content content </div>

使用Javascript:

var currentTop = -1;
var currentSub = -1;
var currentProfile = -1;
//var topMenu = '';
//var subMenu = '';
var lastToggled = '';

function InitNav() {
    //$body.on('click', '#dropmenu > ul > li > a', ToggleTop);
    $('#dropmenu > ul > li > a', 'body').mouseenter(ToggleTop);
    $('#dropmenu', 'body').mouseleave(ToggleTop);

    $body.on('click', '#dropmenu > ul > li ul > li > a', ToggleSub);
    $('#dropmenu login a', 'body').mouseenter(ToggleProfileIn).mouseleave(ToggleProfileOut);
    $body.on('click', '#dropmenu login a', ToggleProfile);
}

function ToggleTop(e) {
    var $listItems = $('#dropmenu > ul > li');

    if (currentProfile !== -1) { // reset profile if needed
        lastToggled.removeClass('toggled');
    }

    if (currentTop !== -1) { // reset top menu
        $listItems.eq(currentTop).removeClass('toggled');
    }

    var $currentTarg = $(e.currentTarget);
    var $item = $currentTarg.parent('li');
    var index = $item.index();

    if (currentTop === index) { // reset
        $item.removeClass('toggled');
        currentTop = -1;
    } else { // set
        $item.addClass('toggled');
        currentTop = index;
    }


    return false;
}

function ToggleSub(e) {
    var $listItems = $('#dropmenu .sub-menu-inner > div > ul > li');

    if (currentSub !== -1) { // reset
        $listItems.removeClass('selected');
    }

    var $currentTarg = $(e.currentTarget);
    var $item = $currentTarg.closest('#dropmenu .sub-menu-inner > div > ul > li');
    var index = $item.index();

    // set
    $item.addClass('selected');
    currentSub = index;

    return false;
}

function ToggleProfileIn(e) {
    var $listItems = $('#dropmenu > ul > li');

    if (currentTop !== -1) { // reset
        lastToggled = $listItems.eq(currentTop).removeClass('toggled');
    }
}

function ToggleProfile(e) {
    currentTop = -1;

    var $listItems = $('#dropmenu login > ul > li');

    if (currentProfile !== -1) { // reset
        $listItems.removeClass('toggled');
    }

    var $currentTarg = $(e.currentTarget);
    var $item = $currentTarg.closest('#dropmenu login > ul > li');
    var index = $item.index();

    // set
    currentProfile = index;
    lastToggled = $item.addClass('toggled');

    return false;
}

function ToggleProfileOut(e) {
    if (currentTop !== -1) { // restore
        lastToggled.addClass('toggled');
    }
}

InitNav();

CSS:

.navigation {
    width: 100%;
    height: 36px;
    background-color: rgb(243, 245, 245);
}
.dropmenu {
    position: relative;
    width: 100%;
    height: 36px;
    font-weight: bold;
    background: #3e494b;
    /* menu background color */
}
.dropmenu ul {
    list-style-type: none;
    z-index: 50;
}
/* first level ul style */

/* .dropmenu > ul, */
 .dropmenu .sub-menu-inner {
    margin: 0 auto;
    /*
    background: rgb(130, 160, 46);
    background: rgba(255, 160, 46, 0.92);
    */
}
.dropmenu > ul > li {
    float: left;
    z-index: 50;
}
.dropmenu > ul > li:nth-of-type(1) {
    width: 187px;
}
.dropmenu > ul > li > a {
    display: inline-block;
    padding: 10px 20px;
    color: #b1b6b9;
    /* grey menu text color */
}
.dropmenu > ul > li > a:hover {
    padding: 10px 20px 10px;
    /* correlation with underline */
    /*
    border-top: 3px solid;
    border-top-color: #b1b6b9;
    */
    background-color: #596466;
    /* #3e494b; /* dark grey */
    color: #ffffff;
}
.dropmenu > ul > li.toggled > a, .dropmenu > ul > li.toggled > a:hover {
    padding: 10px 20px 10px;
    /* correlation with underline */
    /*
    border-top: 3px solid;
    border-top-color: #b1b6b9;
    */
    background-color: #596466;
    /* #3e494b; /* dark grey */
    color: #ffffff;
}
/* sub-menu */
 .dropmenu .sub-menu {
    display: none;
    position: absolute;
    /* background: #00a3da; */
    width: 100%;
    left: 0;
    -webkit-box-shadow: 0 4px 5px -5px #000000;
    -moz-box-shadow: 0 4px 5px -5px #000000;
    box-shadow: 0 4px 5px -5px #000000;
}
.toggled .sub-menu {
    display: block;
    width: 100%;
    background: #596466;
    /* sub menu color */
    /* border: 1px solid red; /**/
}
.dropmenu .sub-menu-inner > div {
    width: 100%;
    height: 100%;
    float: left;
    /* border: 1px solid green; /**/
}
/*
.dropmenu .sub-menu-inner > div > ul {

}
/**/
 .dropmenu .sub-menu-inner > div > ul > li {
    display: inline;
    overflow: hidden;
    position: relative;
}
.dropmenu .sub-menu-inner > div > ul > li:nth-of-type(1) {
    margin-left: 187px;
}
.dropmenu .sub-menu-inner > div > ul > li > a {
    padding: 10px 20px;
    color: #d5d5d5;
    /* border: 1px solid red; */
}
.dropmenu .sub-menu-inner > div > ul > li > a:hover {
    padding: 10px 20px 10px;
    /* correlation with underline */
    color: #ffffff;
}
.dropmenu .sub-menu-inner > div > ul > li.selected > a, .dropmenu .sub-menu-inner > div > ul > li.selected > a:hover {
    padding: 10px 20px 10px;
    /* correlation with underline */
    /*
    background-color: rgb(243, 245, 245);
    background-color: rgba(243, 245, 245, 0.9); /* almost white */
    color: #ffffff;
}
.dropmenu .sub-menu-inner:before, .dropmenu .sub-menu-inner:after {
    content:" ";
    display: table;
}
.dropmenu .sub-menu-inner:after {
    clear: both;
}
.dropmenu .sub-menu-inner > div a {
    line-height: 36px;
}
/* drop-sub-menu */
 .dropmenu .sub-menu-inner ul ul {
    display: none;
    position: absolute;
}
.dropmenu .sub-menu-inner ul > li:hover ul {
    display: block;
    left: 0px;
}
.dropmenu .sub-menu-inner ul > li > ul > li > a {
    display: block;
    padding: 0px 20px;
    width: 145px;
    background: #596466;
    /* sub menu color */
    color: #d5d5d5;
}
.dropmenu .sub-menu-inner ul > li > ul > li > a:hover {
    /* background: #798486; /* sub menu hover color */
    color: #ffffff;
}
/* login menu */
 .dropmenu login ul {
    margin: 0;
}
.dropmenu login > ul > li {
    position: relative;
    float: right;
}
.dropmenu login > ul > li > a {
    display: inline-block;
    padding: 10px 20px;
    color: #b1b6b9;
    /* grey menu text color */
}
.dropmenu login > ul > li > a:hover {
    background-color: #596466;
    /* #3e494b; /* dark grey */
    color: #ffffff;
}
#cart-button {
    font-size: 14px;
}
/* login drop-down */
 .dropmenu login ul ul {
    display: none;
    position: absolute;
    right: 0;
    top: 100%;
}
.dropmenu login ul > li:hover ul {
    display: block;
}
/* login sub-menu */
 .dropmenu login > ul > li > ul > li > a {
    display: inline-block;
    width: 100px;
    padding: 10px 20px;
    background: #3e494b;
    /* menu background color */
    color: #b1b6b9;
    /* light grey */
}
.dropmenu login > ul > li > ul > li > a:hover {
    background-color: #596466;
    /* #3e494b; /* dark grey */
    color: #ffffff;
}
.dropmenu login > ul > li.toggled > a, .dropmenu login > ul > li.toggled > a:hover {
    padding: 10px 20px 10px;
    /* correlation with underline */
    background-color: #596466;
    color: #ffffff;
}
.width-max {
    margin: 2px 10px 0 5px;
    float:none;
    overflow:hidden;
}

2 个答案:

答案 0 :(得分:1)

<强>更新:

<ul class="menu">
    <li id="levela">A Level 1
        <ul id="sub1" class="wide">
            <li>A Level 2</li>
            <li>A Level 2</li>
        </ul>
    </li>
    <li id="levelb">B Level 1
        <ul id="sub2" class="wide">
            <li>B Level 2</li>
            <li>B Level 2
                <ul class="wide_sub">
                    <li>B Level 3</li>
                    <li>B Level 3</li>
                </ul>
            </li>
        </ul>
    </li>
        <li style="position: relative;">C Level 1
        <ul class="normal">
            <li>C Level 2</li>
            <li>C Level 2
                <ul class="normal_sub">
                    <li>C Level 3</li>
                    <li>C Level 3</li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

的CSS:

*, html, body {
    margin: 0;
    padding: 0;
    line-height: 40px;
}

ul.menu {
    list-style:none;
    position: relative;
    width: 100%;
    display: block;
    height: 40px;
    display: inline-block;
        background-color: yellow;
}

ul.menu > li {
    display: block;
    float: left;
    padding: 0 10px;
    cursor: pointer;
    height: 100%;
}

ul.wide {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    margin: 0 auto;
    background-color: grey;
    width: 100%;
    height: 50px;
    list-style: none;
}

ul.wide li {
    float: left;
    padding: 5px 10px;
        position: relative;
}

ul.wide li:hover ul.wide_sub {
    display: block;
}

ul.wide_sub {
    display: none;
    position: aboslute;
    top: 100%;
    left: 0;
    list-style: none;
        background-color: green;
}

ul.wide_sub li {
    float: none;
}

ul.normal {
    display: none;
    position: absolute;
    background-color: red;
    top: 100%;
    left: 0;
    list-style: none;
}

ul.normal li {
    float: none;
    position: relative;
}

ul.normal_sub {
    display: none;
    position: absolute;
    left:100%;
    top: 0;
    list-style: none;
    width: 200px;
    background-color: blue;
}

ul.normal li:hover ul.normal_sub {
    display: block;
}

.menu li:hover ul.normal {
    display: block;
} 

#levela:hover #sub1 {
    display: block;
}

#levelb:hover #sub2 {
    display: block;
}

http://jsfiddle.net/michaelyuen/twdjobmL/1/

希望有所帮助

答案 1 :(得分:0)

HTML

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
// setup a wifi configuration
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\"pigo\"";
wc.preSharedKey = "\"12345678\"";
wc.status = WifiConfiguration.Status.ENABLED;
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
// connect to and enable the connection
int netId = wifiManager.addNetwork(wc);
wifiManager.enableNetwork(netId, true);
wifiManager.setWifiEnabled(true);
stepinfo.setText("Initiating Connection . . .");

CSS:

<ul>
    <li>
        <a href="#">test</a>
        <ul>
            <li>
                   <a href="#">hi!</a>
            </li>
        </ul>
    </li>
</ul>