使用HTML、CSS和jQuery创建响应式菜单

5
我正在按照这个指南http://demos.inspirationalpixels.com/responsive-menu/创建一个响应式菜单,当宽度小于980px时,菜单(三条横杠)会显示但无法打开。一切都正常工作,我做错了什么?有人能告诉我吗?在我的网站上不起作用,请看这里see this以下是我的HTML代码。

jQuery(document).ready(function() {
    jQuery('.toggle-nav').click(function(e) {
        jQuery(this).toggleClass('active');
        jQuery('.menu ul').toggleClass('active');
 
        e.preventDefault();
    });
});
body {
    margin: 0;
    padding: 0;
    background-image: url("img-bg-mobile.jpg");
    background-repeat: no-repeat;
    font-family: open sans;
}

.toggle-nav {
    display: none;
}

@media screen and (min-width: 1000px) {
    nav {
    height: auto;
    width:auto;
    margin-left:17%;
}

nav ul{
    list-style: none;
    margin:0px;
    padding:0px;
}

nav ul li {
    float: left;
    margin: 2%;
}

nav ul li a{
    transition:all linear 0.15s;
    text-decoration: none;
    font-family: open sans;
    color: white;
    font-size: 1.5em;
    text-align: center;
}

nav ul li a:hover {
    color: black;
}

nav ul li ul {
    display: none;

}

nav ul li ul a {
    color: #ff3c1f;
}

nav ul li:hover ul
{
  display:block;
  height:auto;
  width: 110px;
  position:absolute;
  margin:0;
}

}

@media screen and (max-width: 980px) {

.menu {
    position:relative;
    display:inline-block;
}

.menu ul {
    width:100%;
    position:absolute;
    top:120%;
    left:0px;
    padding:10px 18px;
    box-shadow:0px 1px 1px rgba(0,0,0,0.15);
    border-radius:3px;
    background:#303030;
}

.menu ul.active {
    display:none;
}

.menu ul:after {
        width:0px;
        height:0px;
        position:absolute;
        top:0%;
        left:22px;
        content:'';
        transform:translate(0%, -100%);
        border-left:7px solid transparent;
        border-right:7px solid transparent;
        border-bottom:7px solid #303030;
    }
.menu li {
        margin:5px 0px 5px 0px;
        float:none;
        display:block;
    }

.menu a {
        display:block;
    }
 
.toggle-nav {
    padding:20px;
    float:left;
    display:inline-block;
    box-shadow:0px 1px 1px rgba(0,0,0,0.15);
    border-radius:3px;
    background:#303030;
    text-shadow:0px 1px 0px rgba(0,0,0,0.5);
    color:#777;
    font-size:20px;
    transition:color linear 0.15s;
    }
 
.toggle-nav:hover, .toggle-nav.active {
    text-decoration:none;
    color:#66a992;
    }





}
    



article {
    clear: both;
    font-family: open sans;
    font-size: 1em;
}

article p {
    color: white;
    margin-left: 10%;
    margin-right: 10%;

}

footer {
    color: white;
    margin-left: 10%;
    margin-right: 10%;
}

.arrow {
    font-size: 11px;
    margin-left: 1px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="description" content="Yoo Free Recharge App That gives talktime for trying out cool appps">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1" maximum-scale=2.0">
        <title>Yoo Get Free Recharge</title>
        <link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
        <link href="styles.css" rel="stylesheet" type="text/css">
        
    </head>
    
    <body>

        
        
        <!-- header start from here-->
    <header>
        <nav class="menu">
            <ul class="active">
            <li class=logo>
            <a href="http://freers250.com"><img class=yoologo  src="yoosmall.png" title="Yoo Get Free Recharge"></a>
            </li>
            <li class="mainmenu">
            <a href="http://getfr.free250rs.com" title="Home">HOME</a>    
            </li>
            <li class="mainmenu">
            <a href="http://yoo.com/advertise" title="Advertise">ADVERTISE</a>
            </li>   
            <li id="mainmenu" class="sub-menu">
                <a href="http://theyoo.com/whoweare" title="Who we are">WHO WE ARE<span class="arrow">&#9660;</span></a>
                <ul>
                    <li><a href="#">About Us</a></li>
                    <li><a href="#">Vision</a></li>
                    <li><a href="#">Team</a></li>
                </ul>
            </li>
            <li class="mainmenu">
                <a href="http://theyoo.com/pricing" title="pricing">PRICING</a>
            </li>
            <li class="mainmenu">
                <a href="http://theyoo.com/contact" title="Who we are">CONTACT</a>
            </li>
            </ul>

            <a class="toggle-nav" href="#">&#9776</a>
    </nav>
</header>



    
        

        <article>
        
        <p>Yoo Free Recharge App - Yoo is an app that gives free recharge/talktime for trying out new and cool apps you can also refer your friends and get money for that too you can get upto 125Rs. Per Friend <a href="https://play.google.com/store/apps/details?id=com.freerechargeapp.free250rs.free_recharge_app" title="Yoo Free Recharge App" target="_blank"> Download Now</a></p>
        
        </article>

        
        <footer>        
        <div style="font-family:'open sans';" > &copy; Yoo Infotech. All right reserved. </div>
        </footer>
        

        
        <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
        <script src="script.js"></script>
    </body>

</html>


3
在这个片段中,对我来说它正在展现开放。 - Praveen Kumar Purushothaman
在Chrome、Firefox和Safari中对我来说都运行良好。 - Dorvalla
@Adam 我已经做了太多次了。 - Ayaz Alam
@Praveen 是的,这里可以工作,但是正如您所看到的,菜单是向上打开的,而不是向下打开的,它应该像这样打开 http://demos.inspirationalpixels.com/responsive-menu/。 - Ayaz Alam
@Dorvalla 为什么我的网站上它不起作用? - Ayaz Alam
显示剩余7条评论
3个回答

0

你在 CSS 中漏掉了这个

@media screen and (max-width: 980px) {
....
.menu ul {
    width: 100%;
    position: absolute;
    top: 120%;
    left: 0px;
    padding: 10px 18px;
    box-shadow: 0px 1px 1px rgba(0,0,0,0.15);
    border-radius: 3px;
    background: #303030;
}
...
}

@AyazAlam 昨天我测试你的代码片段时,它并不存在,菜单是在导航图标按钮上方打开的。今天它存在了,并且正常工作,是在导航图标按钮下方打开吗? - partypete25

0

0

我按照这个教程制作了我的响应式头部菜单

这是它在此输入图像描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接