Responsive Dropdown Menu

时间:2017-01-19 作者:Josh Rodgers

我一直在为网站寻找不同的菜单样式,这里有一个非常简单的菜单:http://www.w3schools.com/howto/howto_js_topnav.asp

我喜欢菜单的地方在于它反应灵敏,可以与我的菜单一起开箱即用(大多数情况下)。我遇到的唯一问题是子菜单。这个菜单似乎没有任何子菜单的css,但我想它应该不会太难,对吧?我非常接近,但我想我需要稍微调整一下才能让子菜单正常工作。

JavaScript:

<script>
    /* Toggle between adding and removing the "responsive" class to topnav when the user clicks on the icon */
    function myFunction() {
        var x = document.getElementById("myTopnav");
        if (x.className === "topnav") {
            x.className += " responsive";
        } else {
            x.className = "topnav";
        }
    }
</script>
HTML(尽管我删除了一些默认的WordPress类并列出了URL,以便更容易理解):

<ul class="topnav" id="myTopnav">
    <li><a href="http://url.com/">Home</a></li>
    <li>
        <a href="http://url.com/">Pictures</a>
        <ul class="children">
            <li><a href="http://url.com/">Beaver</a></li>
            <li>
                <a href="http://url.com/">Duck</a>
                <ul class="children">
                    <li><a href="http://url.com/">Fever</a></li>
                </ul>
            </li>
            <li><a href="http://url.com/">Frog</a></li>
        </ul>
    </li>
    <li><a href="http://url.com/">Nominations</a></li>
    <li><a href="http://url.com/">Contact</a></li>
    <li class="icon">
        <a href="javascript:void(0);" onclick="myFunction()">&#9776;</a>
    </li>
</ul>
CSS:

/* Remove margins and padding from the list, and add a black background color */
ul.topnav {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #333;
}

/* Float the list items side by side */
ul.topnav li {float: left;}

/* Style the links inside the list items */
ul.topnav li a {
    display: inline-block;
    color: #f2f2f2;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    transition: 0.3s;
    font-size: 17px;
}

/* Change background color of links on hover */
ul.topnav li a:hover {background-color: #555;}

/* Hide the list item that contains the link that should open and close the topnav on small screens */
ul.topnav li.icon {display: none;}

/* When the screen is less than 680 pixels wide, hide all list items, except for the first one ("Home"). Show the list item that contains the link to open and close the topnav (li.icon) */
@media screen and (max-width:680px) {
    ul.topnav li:not(:first-child) {display: none;}
    ul.topnav li.icon {
        float: right;
        display: inline-block;
    }
}

/* The "responsive" class is added to the topnav with JavaScript when the user clicks on the icon. This class makes the topnav look good on small screens */
@media screen and (max-width:680px) {
    ul.topnav.responsive {position: relative;}
    ul.topnav.responsive li.icon {
        position: absolute;
        right: 0;
        top: 0;
    }
    ul.topnav.responsive li {
        float: none;
        display: inline;
    }
    ul.topnav.responsive li a {
        display: block;
        text-align: left;
    }
}
子菜单CSS:

/* CSS I added */
ul.topnav .page_item_has_children ul {
    display: none;
}

ul.topnav .page_item_has_children:hover ul {
    display: block;
}

ul.topnav .page_item_has_children:hover li {
    width: 100%;
}
我不确定我做错了什么,但我希望菜单的功能如下:http://blog.teamtreehouse.com/wp-content/uploads/2015/01/css-dropdown.png

现在,当我将鼠标悬停在菜单上时,即使我在菜单上设置了最大宽度,二级菜单也会覆盖整个屏幕。

我想我把事情弄得比实际情况复杂得多,有人有什么想法吗?

现场示例:http://joshrodg.com/site/

1 个回复
最合适的回答,由SO网友:Josh Rodgers 整理而成

好啊

所以,在闲逛之后,我找到了一些工作。

我基本上结合了我在这里找到的菜单:https://webdesignerhut.com/css-dropdown-menu/ 与我原来的菜单的反应。

最后的代码看起来像。。。

JavaScript:

<script>
    /* Toggle between adding and removing the "responsive" class to topnav when the user clicks on the icon */
    function myFunction() {
        var x = document.getElementById("myTopnav");
        if (x.className === "topnav") {
            x.className += " responsive";
        } else {
            x.className = "topnav";
        }
    }
</script>
HTML(尽管我删除了一些默认的WordPress类并列出了URL,以便更容易理解):

<nav>
    <ul class="topnav" id="myTopnav">
        <li><a href="http://url.com/">Home</a></li>
            <li>
                <a href="http://url.com/">Pictures</a>
                <ul class="children">
                    <li><a href="http://url.com/">Beaver</a></li>
                    <li>
                        <a href="http://url.com/">Duck</a>
                        <ul class="children">
                            <li><a href="http://url.com/">Fever</a></li>
                        </ul>
                    </li>
                    <li><a href="http://url.com/">Frog</a></li>
                </ul>
            </li>
            <li><a href="http://url.com/">Nominations</a></li>
            <li><a href="http://url.com/">Contact</a></li>
            <li class="icon">
                <a href="javascript:void(0);" onclick="myFunction()">&#9776;</a>
            </li>
        <li class="icon">
            <a href="javascript:void(0);" onclick="myFunction()">&#9776;</a>
        </li>
    </ul>
</nav>
CSS:/*隐藏包含应在小屏幕上打开和关闭topnav的链接的列表项*/ul。topnav li。图标{显示:无;}

/* When the screen is less than 680 pixels wide, hide all list items, except for the first one ("Home"). Show the list item that contains the link to open and close the topnav (li.icon) */
@media screen and (max-width:680px) {
    ul.topnav li:not(:first-child) {display: none;}
    ul.topnav li.icon {
        float: right;
        display: inline-block;
    }
}

/* The "responsive" class is added to the topnav with JavaScript when the user clicks on the icon. This class makes the topnav look good on small screens */
@media screen and (max-width:680px) {
    ul.topnav.responsive {position: relative;}
    ul.topnav.responsive li.icon {
        position: absolute;
        right: 0;
        top: 0;
    }
    ul.topnav.responsive li {
        float: none;
        display: inline;
    }
    ul.topnav.responsive li a {
        display: block;
        text-align: left;
    }
}

/* Giving a background-color to the nav container. */
nav {
    margin: 100px 0;
    background-color: #E64A19;
}

/* Removing padding, margin and "list-style" from the "ul",
* and adding "position:reltive" */
nav ul {
    padding:0;
    margin:0;
    list-style: none;
    position: relative;
}

/* Positioning the navigation items inline */
nav ul li {
    margin: 0px -7px 0 0;
    display:inline-block;
    background-color: #E64A19;
}

/* Styling the links */
nav a {
    display:block;
    padding:0 10px;
    color:#FFF;
    font-size:20px;
    line-height: 60px;
    text-decoration:none;
}
/* Background color change on Hover */
nav a:hover {
    background-color: #000000;
}

@media screen and (min-width:681px) {
    /* Hide Dropdowns by Default
    * and giving it a position of absolute */
    nav ul ul {
        display: none;
        position: absolute;
        top: 100%;
    }

    /* Display Dropdowns on Hover */
    nav ul li:hover > ul {
        display:inherit;
    }

    /* First Tier Dropdown */
    nav ul ul li {
        min-width:170px;
        display:list-item;
        position: relative;
    }

    /* Second, Third and more Tiers
    * We move the 2nd and 3rd etc tier dropdowns to the left
    * by the amount of the width of the first tier.
    */
    nav ul ul ul {
        position: absolute;
        top:0;
        left:100%;
    }

    /* Change \' +\' in order to change the Dropdown symbol */
    nav li > a:after { content:  \' +\'; }
    nav li > a:only-child:after { content: \'\'; }
}
代码笔:http://codepen.io/joshrodgers/pen/GrWZBj

呜呜!

相关推荐

如何将.css文件应用于虚拟页面?

如果使用以下插件在保持主题完整的情况下创建虚拟页面wp-virtual-page-tutorial, 怎么可能。css文件是否应用于此虚拟页面?如何使用将虚拟页名称用作条件的if语句来实现这一点?任何帮助都将不胜感激。以下是创建虚拟页面的代码:Class VPTutorial { function __construct() { register_activation_hook( __FILE__, array( $this, \'activate\' ) );&#