展开和折叠响应式菜单

时间:2015-01-10 15:39:01

标签: html css

在移动设备上使用切换功能折叠和展开时,我可以使此菜单默认折叠的最佳方法是什么。

我更喜欢这只是CSS,但我不确定该怎么做。

这是我工作的来源。 http://www.script-tutorials.com/demos/335/index.html

/* common and top level styles */
#nav span {
    display: none;
}
#nav, #nav ul {
    list-style: none outside none;
    margin: 0;
    padding: 0;
}
#nav {
    background-color: #F5F5F5;
    border-bottom: 5px solid #333333;
    float: left;
    margin-left: 1%;
    margin-right: 1%;
    position: relative;
    width: 98%;
}
#nav ul.subs {
    background-color: #FFFFFF;
    box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2);
    color: #333333;
    display: none;
    left: 0;
    padding: 2%;
    position: absolute;
    top: 54px;
    width: 96%;
}
#nav > li {
    border-bottom: 5px solid transparent;
    float: left;
    margin-bottom: -5px;
    text-align: left;
    -moz-transition: all 300ms ease-in-out 0s;
    -ms-transition: all 300ms ease-in-out 0s;
    -o-transition: all 300ms ease-in-out 0s;
    -webkit-transition: all 300ms ease-in-out 0s;
    transition: all 300ms ease-in-out 0s;
}
#nav li a {
    display: block;
    text-decoration: none;
    -moz-transition: color 450ms ease-in-out 0s, background-color 450ms ease-in-out 0s;
    -ms-transition: color 450ms ease-in-out 0s, background-color 450ms ease-in-out 0s;
    -o-transition: color 450ms ease-in-out 0s, background-color 450ms ease-in-out 0s;
    -webkit-transition: color 450ms ease-in-out 0s, background-color 450ms ease-in-out 0s;
    transition: color 450ms ease-in-out 0s, background-color 450ms ease-in-out 0s;
    white-space: normal;
}
#nav > li > a {
    color: #333333;
    display: block;
    font-size: 1.3em;
    line-height: 49px;
    padding: 0 15px;
    text-transform: uppercase;
}
#nav > li:hover > a, #nav > a:hover {
    background-color: #F55856;
    color: #FFFFFF;
}
#nav li.active > a {
    background-color: #333333;
    color: #FFFFFF;
}

/* submenu */
#nav li:hover ul.subs {
    display: block;
}
#nav ul.subs > li {
    display: inline-block;
    float: none;
    padding: 10px 1%;
    vertical-align: top;
    width: 33%;
}
#nav ul.subs > li a {
    color: #777777;
    line-height: 20px;
}
#nav ul li a:hover {
    color: #F55856;
}
#nav ul.subs > li > a {
    font-size: 1.3em;
    margin-bottom: 10px;
    text-transform: uppercase;
}
#nav ul.subs > li li {
    float: none;
    padding-left: 8px;
    -moz-transition: padding 150ms ease-out 0s;
    -ms-transition: padding 150ms ease-out 0s;
    -o-transition: padding 150ms ease-out 0s;
    -webkit-transition: padding 150ms ease-out 0s;
    transition: padding 150ms ease-out 0s;
}
#nav ul.subs > li li:hover {
    padding-left: 15px;
}

/* responsive rules */
@media all and (max-width : 980px) {

#nav {
  display: none;
}

#burger:hover #nav {
  display: block;
}


    #nav > li {
        float: none;
        border-bottom: 0;
        margin-bottom: 0;
    }
    #nav ul.subs {
        position: relative;
        top: 0;
    }
    #nav li:hover ul.subs {
        display: none;
    }
    #nav li #s1:target + ul.subs,
    #nav li #s2:target + ul.subs {
        display: block;
    }

    #nav ul.subs > li {
        display: block;
        width: auto;
    }
}

 <ul id="nav">
        <li><a href="index.html#">Home</a></li>
        <li><a href="index.html#s1">Menu 1</a>
            <span id="s1"></span>
            <ul class="subs">
                <li><a href="index.html#">Header a</a>
                    <ul>
                        <li><a href="index.html#">Submenu x</a></li>
                        <li><a href="index.html#">Submenu y</a></li>
                        <li><a href="index.html#">Submenu z</a></li>
                    </ul>
                </li>
                <li><a href="index.html#">Header b</a>
                    <ul>
                        <li><a href="index.html#">Submenu x</a></li>
                        <li><a href="index.html#">Submenu y</a></li>
                        <li><a href="index.html#">Submenu z</a></li>
                    </ul>
                </li>
            </ul>
        </li>
        <li class="active"><a href="index.html#s2">Menu 2</a>
            <span id="s2"></span>
            <ul class="subs">
                <li><a href="index.html#">Header c</a>
                    <ul>
                        <li><a href="index.html#">Submenu x</a></li>
                        <li><a href="index.html#">Submenu y</a></li>
                        <li><a href="index.html#">Submenu z</a></li>
                    </ul>
                </li>
                <li><a href="index.html#">Header d</a>
                    <ul>
                        <li><a href="index.html#">Submenu x</a></li>
                        <li><a href="index.html#">Submenu y</a></li>
                        <li><a href="index.html#">Submenu z</a></li>
                    </ul>
                </li>
            </ul>
        </li>
        <li><a href="index.html#">Menu 3</a></li>
        <li><a href="index.html#">Menu 4</a></li>
        <li><a href="index.html#">Menu 5</a></li>

    </ul>

1 个答案:

答案 0 :(得分:0)

我已将导航包裹在div中。添加了一个按钮,并在移动设备尺寸时打开和关闭导航。以及在全宽时隐藏汉堡按钮。

<div class="nav-bar">
  <div id="burger">
    <input type="button" data-name="show" value="Toggle" id="toggle">
  </div>
  <!--rest of navigation follows here-->
</div>


<script>
$(document).ready(function () {
        $("#burger").click(function () {
            if ($(this).data('name') == 'show') {
                $("#nav").animate({
                    height: '0px'
                }).hide()

                $(this).data('name', 'hide')
            } else {
                $("#nav").animate({
                    height: '100%'
                }).show()

                $(this).data('name', 'show')
            }
        });
    });
</script>