无法让这个响应式菜单工作x(

时间:2017-10-10 01:30:09

标签: css menu dropdown responsive

在移动设备上运行良好,但在全屏模式下,我无法在悬停时选择下拉选项,因为该选项会消失。

我的代码:

    $('.handle').on('click', function(){
        $('nav ul').toggleClass('showing');
    });
body {
	margin: 0;
	padding: 0;
	font-family: sans-serif;
}
.toggle,
[id^=drop] {
	display: none;
}

/* Giving a background-color to the nav container. */
nav { 
	margin:0;
	padding: 0;
	background-color: #254441;
}

#logo {
	display: block;
	padding: 0 30px;
	float: left;
	font-size:20px;
	line-height: 60px;
}

/* Since we'll have the "ul li" "float:left"
 * we need to add a clear after the container. */

nav:after {
	content:"";
	display:table;
	clear:both;
}

/* Removing padding, margin and "list-style" from the "ul",
 * and adding "position:reltive" */
nav ul {
	float: right;
	padding:0;
	margin:0;
	list-style: none;
	position: relative;
	}
	
/* Positioning the navigation items inline */
nav ul li {
	margin: 0px;
	display:inline-block;
	float: left;
	background-color: #254441;
	}

/* Styling the links */
nav a {
	display:block;
	padding:14px 20px;	
	color:#FFF;
	font-size:17px;
	text-decoration:none;
}


nav ul li ul li:hover { background: #000000; }

/* Background color change on Hover */
nav a:hover { 
	background-color: #000000; 
}

/* Hide Dropdowns by Default
 * and giving it a position of absolute */
nav ul ul {
	display: none;
	position: absolute; 
	/* has to be the same number as the "line-height" of "nav a" */
	top: 60px; 
}
	
/* Display Dropdowns on Hover */
nav ul li:hover > ul {
	display:inherit;
}
	
/* Fisrt Tier Dropdown */
nav ul ul li {
	width:170px;
	float:none;
	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 li {
	position: relative;
	top:-60px;
	/* has to be the same number as the "width" of "nav ul ul li" */ 
	left:170px; 
}

	
/* Change ' +' in order to change the Dropdown symbol */
li > a:after { content:  ' +'; }
li > a:only-child:after { content: ''; }


/* Media Queries
--------------------------------------------- */

@media all and (max-width : 768px) {

	#logo {
		display: block;
		padding: 0;
		width: 100%;
		text-align: center;
		float: none;
	}

	nav {
		margin: 0;
	}

	/* Hide the navigation menu by default */
	/* Also hide the  */
	.toggle + a,
	.menu {
		display: none;
	}

	/* Stylinf the toggle lable */
	.toggle {
		display: block;
		background-color: #254441;
		padding:14px 20px;	
		color:#FFF;
		font-size:17px;
		text-decoration:none;
		border:none;
	}

	.toggle:hover {
		background-color: #000000;
	}

	/* Display Dropdown when clicked on Parent Lable */
	[id^=drop]:checked + ul {
		display: block;
	}

	/* Change menu item's width to 100% */
	nav ul li {
		display: block;
		width: 100%;
		}

	nav ul ul .toggle,
	nav ul ul a {
		padding: 0 40px;
	}

	nav ul ul ul a {
		padding: 0 80px;
	}

	nav a:hover,
 	nav ul ul ul a {
		background-color: #000000;
	}
  
	nav ul li ul li .toggle,
	nav ul ul a,
  nav ul ul ul a{
		padding:14px 20px;	
		color:#FFF;
		font-size:17px; 
	}
  
  
	nav ul li ul li .toggle,
	nav ul ul a {
		background-color: #212121; 
	}

	/* Hide Dropdowns by Default */
	nav ul ul {
		float: none;
		position:static;
		color: #ffffff;
		/* has to be the same number as the "line-height" of "nav a" */
	}
		
	/* Hide menus on hover */
	nav ul ul li:hover > ul,
	nav ul li:hover > ul {
		display: none;
	}
		
	/* Fisrt Tier Dropdown */
	nav ul ul li {
		display: block;
		width: 100%;
	}

	nav ul ul ul li {
		position: static;
		/* has to be the same number as the "width" of "nav ul ul li" */ 

	}

}

@media all and (max-width : 330px) {

	nav ul li {
		display:block;
		width: 94%;
	}

}
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<body>
    <nav>
        <div id="logo">Your Logo here</div>

        <label for="drop" class="toggle">Menu</label>
        <input type="checkbox" id="drop" />
            <ul class="menu">
                <li><a href="#">Home</a></li>
                <li>
                    <!-- First Tier Drop Down -->
                    <label for="drop-1" class="toggle">WordPress +</label>
                    <a href="#">WordPress</a>
                    <input type="checkbox" id="drop-1"/>
                    <ul>
                        <li><a href="#">Themes and stuff</a></li>
                        <li><a href="#">Plugins</a></li>
                        <li><a href="#">Tutorials</a></li>
                    </ul> 

                </li>
                <li>

                <!-- First Tier Drop Down -->
                <label for="drop-2" class="toggle">Web Design +</label>
                <a href="#">Web Design</a>
                <input type="checkbox" id="drop-2"/>
                <ul>
                    <li><a href="#">Resources</a></li>
                    <li><a href="#">Links</a></li>
                    <li>
                       
                    <!-- Second Tier Drop Down -->        
                    <label for="drop-3" class="toggle">Tutorials +</label>
                    <a href="#">Tutorials</a>         
                    <input type="checkbox" id="drop-3"/>

                    <ul>
                        <li><a href="#">HTML/CSS</a></li>
                        <li><a href="#">jQuery</a></li>
                        <li><a href="#">Other</a></li>
                    </ul>
                    </li>
                </ul>
                </li>
                <li><a href="#">Graphic Design</a></li>
                <li><a href="#">Inspiration</a></li>
                <li><a href="#">Contact</a></li>
                <li><a href="#">About</a></li>
            </ul>
        </nav>

这是一个小提琴,所以你可以看到我在说什么:

http://jsfiddle.net/p1mrtuex/9/

请帮忙!

3 个答案:

答案 0 :(得分:0)

下拉菜单离导航更远。将nav ul ul更改为40px。它将下拉菜单移近导航栏,现在您可以选择下拉列表。

    $('.handle').on('click', function(){
            $('nav ul').toggleClass('showing');
        });
    body {
    	margin: 0;
    	padding: 0;
    	font-family: sans-serif;
    }
    .toggle,
    [id^=drop] {
    	display: none;
    }

    /* Giving a background-color to the nav container. */
    nav { 
    	margin:0;
    	padding: 0;
    	background-color: #254441;
    }

    #logo {
    	display: block;
    	padding: 0 30px;
    	float: left;
    	font-size:20px;
    	line-height: 60px;
    }

    /* Since we'll have the "ul li" "float:left"
     * we need to add a clear after the container. */

    nav:after {
    	content:"";
    	display:table;
    	clear:both;
    }

    /* Removing padding, margin and "list-style" from the "ul",
     * and adding "position:reltive" */
    nav ul {
    	float: right;
    	padding:0;
    	margin:0;
    	list-style: none;
    	position: relative;
    	}
    	
    /* Positioning the navigation items inline */
    nav ul li {
    	margin: 0px;
    	display:inline-block;
    	float: left;
    	background-color: #254441;
    	}

    /* Styling the links */
    nav a {
    	display:block;
    	padding:14px 20px;	
    	color:#FFF;
    	font-size:17px;
    	text-decoration:none;
    }


    nav ul li ul li:hover { background: #000000; }

    /* Background color change on Hover */
    nav a:hover { 
    	background-color: #000000; 
    }

    /* Hide Dropdowns by Default
     * and giving it a position of absolute */
    nav ul ul {
        display: none;
        top: 20px;
        padding-top: 20px;
    }
    	
    /* Display Dropdowns on Hover */
    nav ul li:hover > ul {
        position: absolute;
        display: block;
    }
    	
    /* Fisrt Tier Dropdown */
    nav ul ul li {
    	width:170px;
    	float:none;
    	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 li {
    	position: relative;
    	top:-60px;
    	/* has to be the same number as the "width" of "nav ul ul li" */ 
    	left:170px; 
    }

    	
    /* Change ' +' in order to change the Dropdown symbol */
    li > a:after { content:  ' +'; }
    li > a:only-child:after { content: ''; }


    /* Media Queries
    --------------------------------------------- */

    @media all and (max-width : 768px) {

    	#logo {
    		display: block;
    		padding: 0;
    		width: 100%;
    		text-align: center;
    		float: none;
    	}

    	nav {
    		margin: 0;
    	}

    	/* Hide the navigation menu by default */
    	/* Also hide the  */
    	.toggle + a,
    	.menu {
    		display: none;
    	}

    	/* Stylinf the toggle lable */
    	.toggle {
    		display: block;
    		background-color: #254441;
    		padding:14px 20px;	
    		color:#FFF;
    		font-size:17px;
    		text-decoration:none;
    		border:none;
    	}

    	.toggle:hover {
    		background-color: #000000;
    	}

    	/* Display Dropdown when clicked on Parent Lable */
    	[id^=drop]:checked + ul {
    		display: block;
    	}

    	/* Change menu item's width to 100% */
    	nav ul li {
    		display: block;
    		width: 100%;
    		}

    	nav ul ul .toggle,
    	nav ul ul a {
    		padding: 0 40px;
    	}

    	nav ul ul ul a {
    		padding: 0 80px;
    	}

    	nav a:hover,
     	nav ul ul ul a {
    		background-color: #000000;
    	}
      
    	nav ul li ul li .toggle,
    	nav ul ul a,
      nav ul ul ul a{
    		padding:14px 20px;	
    		color:#FFF;
    		font-size:17px; 
    	}
      
      
    	nav ul li ul li .toggle,
    	nav ul ul a {
    		background-color: #212121; 
    	}

    	/* Hide Dropdowns by Default */
    	nav ul ul {
    		float: none;
    		position:static;
    		color: #ffffff;
    		/* has to be the same number as the "line-height" of "nav a" */
    	}
    		
    	/* Hide menus on hover */
    	nav ul ul li:hover > ul,
    	nav ul li:hover > ul {
    		display: none;
    	}
    		
    	/* Fisrt Tier Dropdown */
    	nav ul ul li {
    		display: block;
    		width: 100%;
    	}

    	nav ul ul ul li {
    		position: static;
    		/* has to be the same number as the "width" of "nav ul ul li" */ 

    	}
      
      
      /*to overwrite desktop default behaviour*/
      nav ul li > ul, nav ul li:hover > ul {
        position: relative;
        top: 0;
        padding-top: 0;
      }
      


    }

    @media all and (max-width : 330px) {

    	nav ul li {
    		display:block;
    		width: 94%;
    	}

    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <body>
        <nav>
            <div id="logo">Your Logo here</div>

            <label for="drop" class="toggle">Menu</label>
            <input type="checkbox" id="drop" />
                <ul class="menu">
                    <li><a href="#">Home</a></li>
                    <li>
                        <!-- First Tier Drop Down -->
                        <label for="drop-1" class="toggle">WordPress +</label>
                        <a href="#">WordPress</a>
                        <input type="checkbox" id="drop-1"/>
                        <ul>
                            <li><a href="#">Themes and stuff</a></li>
                            <li><a href="#">Plugins</a></li>
                            <li><a href="#">Tutorials</a></li>
                        </ul> 

                    </li>
                    <li>

                    <!-- First Tier Drop Down -->
                    <label for="drop-2" class="toggle">Web Design +</label>
                    <a href="#">Web Design</a>
                    <input type="checkbox" id="drop-2"/>
                    <ul>
                        <li><a href="#">Resources</a></li>
                        <li><a href="#">Links</a></li>
                        <li>
                           
                        <!-- Second Tier Drop Down -->        
                        <label for="drop-3" class="toggle">Tutorials +</label>
                        <a href="#">Tutorials</a>         
                        <input type="checkbox" id="drop-3"/>

                        <ul>
                            <li><a href="#">HTML/CSS</a></li>
                            <li><a href="#">jQuery</a></li>
                            <li><a href="#">Other</a></li>
                        </ul>
                        </li>
                    </ul>
                    </li>
                    <li><a href="#">Graphic Design</a></li>
                    <li><a href="#">Inspiration</a></li>
                    <li><a href="#">Contact</a></li>
                    <li><a href="#">About</a></li>
                </ul>
            </nav>

干杯

答案 1 :(得分:0)

这是因为您的下拉位置低于导航的悬停状态。而不是

nav ul ul {
    display: none;
    position: absolute;
    top: 60px;
}

nav ul li:hover > ul {
    display: inherit;
}

尝试更改为:

nav ul ul {
    display: none;
    top: 40px;
    padding-top: 20px;
}

nav ul li:hover > ul {
    position: absolute;
    display: block;
}

要设置移动导航样式,请覆盖默认桌面行为

@media all and (max-width : 768px) {
  nav ul li:hover > ul {
    position: relative;
    top: 0;
    padding-top: 0;
  }
}

示例:

&#13;
&#13;
$('.handle').on('click', function(){
        $('nav ul').toggleClass('showing');
    });
&#13;
body {
	margin: 0;
	padding: 0;
	font-family: sans-serif;
}
.toggle,
[id^=drop] {
	display: none;
}

/* Giving a background-color to the nav container. */
nav { 
	margin:0;
	padding: 0;
	background-color: #254441;
}

#logo {
	display: block;
	padding: 0 30px;
	float: left;
	font-size:20px;
	line-height: 60px;
}

/* Since we'll have the "ul li" "float:left"
 * we need to add a clear after the container. */

nav:after {
	content:"";
	display:table;
	clear:both;
}

/* Removing padding, margin and "list-style" from the "ul",
 * and adding "position:reltive" */
nav ul {
	float: right;
	padding:0;
	margin:0;
	list-style: none;
	position: relative;
	}
	
/* Positioning the navigation items inline */
nav ul li {
	margin: 0px;
	display:inline-block;
	float: left;
	background-color: #254441;
	}

/* Styling the links */
nav a {
	display:block;
	padding:14px 20px;	
	color:#FFF;
	font-size:17px;
	text-decoration:none;
}


nav ul li ul li:hover { background: #000000; }

/* Background color change on Hover */
nav a:hover { 
	background-color: #000000; 
}

/* Hide Dropdowns by Default
 * and giving it a position of absolute */
nav ul ul {
    display: none;
    top: 40px;
    padding-top: 20px;
}
	
/* Display Dropdowns on Hover */
nav ul li:hover > ul {
    position: absolute;
    display: block;
}
	
/* Fisrt Tier Dropdown */
nav ul ul li {
	width:170px;
	float:none;
	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 li {
	position: relative;
	top:-60px;
	/* has to be the same number as the "width" of "nav ul ul li" */ 
	left:170px; 
}

	
/* Change ' +' in order to change the Dropdown symbol */
li > a:after { content:  ' +'; }
li > a:only-child:after { content: ''; }


/* Media Queries
--------------------------------------------- */

@media all and (max-width : 768px) {

	#logo {
		display: block;
		padding: 0;
		width: 100%;
		text-align: center;
		float: none;
	}

	nav {
		margin: 0;
	}

	/* Hide the navigation menu by default */
	/* Also hide the  */
	.toggle + a,
	.menu {
		display: none;
	}

	/* Stylinf the toggle lable */
	.toggle {
		display: block;
		background-color: #254441;
		padding:14px 20px;	
		color:#FFF;
		font-size:17px;
		text-decoration:none;
		border:none;
	}

	.toggle:hover {
		background-color: #000000;
	}

	/* Display Dropdown when clicked on Parent Lable */
	[id^=drop]:checked + ul {
		display: block;
	}

	/* Change menu item's width to 100% */
	nav ul li {
		display: block;
		width: 100%;
		}

	nav ul ul .toggle,
	nav ul ul a {
		padding: 0 40px;
	}

	nav ul ul ul a {
		padding: 0 80px;
	}

	nav a:hover,
 	nav ul ul ul a {
		background-color: #000000;
	}
  
	nav ul li ul li .toggle,
	nav ul ul a,
  nav ul ul ul a{
		padding:14px 20px;	
		color:#FFF;
		font-size:17px; 
	}
  
  
	nav ul li ul li .toggle,
	nav ul ul a {
		background-color: #212121; 
	}

	/* Hide Dropdowns by Default */
	nav ul ul {
		float: none;
		position:static;
		color: #ffffff;
		/* has to be the same number as the "line-height" of "nav a" */
	}
		
	/* Hide menus on hover */
	nav ul ul li:hover > ul,
	nav ul li:hover > ul {
		display: none;
	}
		
	/* Fisrt Tier Dropdown */
	nav ul ul li {
		display: block;
		width: 100%;
	}

	nav ul ul ul li {
		position: static;
		/* has to be the same number as the "width" of "nav ul ul li" */ 

	}
  
  
  /*to overwrite desktop default behaviour*/
  nav ul li > ul, nav ul li:hover > ul {
    position: relative;
    top: 0;
    padding-top: 0;
  }
  


}

@media all and (max-width : 330px) {

	nav ul li {
		display:block;
		width: 94%;
	}

}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<body>
    <nav>
        <div id="logo">Your Logo here</div>

        <label for="drop" class="toggle">Menu</label>
        <input type="checkbox" id="drop" />
            <ul class="menu">
                <li><a href="#">Home</a></li>
                <li>
                    <!-- First Tier Drop Down -->
                    <label for="drop-1" class="toggle">WordPress +</label>
                    <a href="#">WordPress</a>
                    <input type="checkbox" id="drop-1"/>
                    <ul>
                        <li><a href="#">Themes and stuff</a></li>
                        <li><a href="#">Plugins</a></li>
                        <li><a href="#">Tutorials</a></li>
                    </ul> 

                </li>
                <li>

                <!-- First Tier Drop Down -->
                <label for="drop-2" class="toggle">Web Design +</label>
                <a href="#">Web Design</a>
                <input type="checkbox" id="drop-2"/>
                <ul>
                    <li><a href="#">Resources</a></li>
                    <li><a href="#">Links</a></li>
                    <li>
                       
                    <!-- Second Tier Drop Down -->        
                    <label for="drop-3" class="toggle">Tutorials +</label>
                    <a href="#">Tutorials</a>         
                    <input type="checkbox" id="drop-3"/>

                    <ul>
                        <li><a href="#">HTML/CSS</a></li>
                        <li><a href="#">jQuery</a></li>
                        <li><a href="#">Other</a></li>
                    </ul>
                    </li>
                </ul>
                </li>
                <li><a href="#">Graphic Design</a></li>
                <li><a href="#">Inspiration</a></li>
                <li><a href="#">Contact</a></li>
                <li><a href="#">About</a></li>
            </ul>
        </nav>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

你有这条规则:

nav ul ul {
    display: none;
    position: absolute; 
    /* has to be the same number as the "line-height" of "nav a" */
    top: 60px; 
}

如果您将top: 60px;更改为top: 40px;,则会向上移动,以便可以选择。

这是一个小提琴:http://jsfiddle.net/hm41j6u1/1/