【问题标题】:Expand and collapse responsive menu展开和折叠响应式菜单
【发布时间】:2015-03-08 19:11:16
【问题描述】:

在移动设备大小时,我可以让这个菜单默认折叠并使用切换来折叠和展开的最佳方法是什么。

我希望这只是 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>

【问题讨论】:

  • default collapse when at a mobile size 是什么意思?
  • 你必须使用 js,而不仅仅是纯 CSS。但要这样做,您可以在单击事件触发时添加一个类,并在第二次单击以隐藏或淡出()时再次将其删除,然后将 CSS 样式添加到新声明的类中。
  • @jmore009 当它以移动分辨率显示时,我可以添加一个菜单汉堡图标来展开菜单并确保在加载时它是折叠的。
  • @Sdghasemi 有没有办法完全在带有显示的 CSS 中完成?类似于切换导航子菜单的方式?
  • 唯一的方法是使用 HTML 标签,在点击时 CSS 可以识别输入:选中(看看here)。但是有点丑,不推荐。 Node.js 更加跨浏览器。

标签: html css


【解决方案1】:

我已将导航包装在一个 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 2014-11-07
    相关资源
    最近更新 更多