我想出了如何制作它,以便在移动版本中查看时标题会在点击时消失,但当您不在移动版本时它也会消失。如果它不在移动版中,如何在点击后消失?
HTML
<header>
<div class="head_container">
<a href="#" class="logo"><h1>Hello</h1></a>
<nav>
<a href="#" id="top_menu">Hello</a>
<ul>
<li><a href="#homepage">HomePage</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</nav>
</div>
<div class="clear"></div>
</header>
桌面CSS
nav {
margin: 0 auto;
float: right;
font: 14px Arial, sans-serif;
position: relative;
}
nav ul {
padding: 0;
margin: 0 auto;
}
nav li {
display: inline-block;
float: left;
padding: 20px 0px;
}
nav li a {
padding: 8px 15px;
margin: 0 .15em;
border-bottom: 1px solid transparent;
}
nav a {
color: #000000;
display: inline-block;
text-decoration: none;
}
nav a#top_menu {
display: none;
}
nav a:hover, nav a:active {
color: #8148c1;
}
CSS For Mobile
nav {
float: none;
line-height: 60px;
}
nav ul {
display: none;
height: auto;
}
nav li {
display: block;
width: 100%;
float: none;
padding: 0;
}
nav li a {
border-bottom: 1px solid #ebebeb;
padding: 0;
}
nav a {
text-align: left;
width: 100%;
text-indent: 20px;
white-space: nowrap;
}
nav a#top_menu {
display: block;
background-position: 20px 17px;
text-indent: -9999em;
position: relative;
width: 100%;
border-bottom: 1px solid transparent;
}
nav a#top_menu:after {
content:"";
background: url('../images/mini.png') no-repeat;
width: 30px;
height: 30px;
display: inline-block;
position: absolute;
right: 15px;
top: 15px;
}
的JavaScript
$(function () {
menu = $("nav ul");
$(top_menu).click(function () {
if ($('nav ul').is(':visible')) {
menu.slideUp()
} else {
menu.slideDown()
}
});
$('nav li a').click(function () {
menu.slideUp()
});
});
答案 0 :(得分:0)
您可以在javascript&amp;中检查视口宽度。只让菜单向上滑动&amp;如果它是一个移动视口。
这应该有效 -
$(function () {
menu = $("nav ul");
var preferredMobileViewPortWidth = "480"; /* you can keep this as your mobile media query width */
var viewPortWidth = $(window).width();
var isMobile = false;
if(viewPortWidth <= preferredMobileViewPortWidth) {
isMobile = true;
}
$(top_menu).click(function () {
if(isMobile) {
if ($('nav ul').is(':visible')) {
menu.slideUp()
} else {
menu.slideDown()
}
}
});
$('nav li a').click(function () {
if(isMobile) {
menu.slideUp();
}
});
});
答案 1 :(得分:0)
尝试检查视口的宽度:
$(document).ready(function () {
viewPort = (window).width();
menu = $("nav ul");
if( viewPort < 500) {
$(top_menu).click(function () {
if ($('nav ul').is(':visible')) {
menu.slideUp()
} else {
menu.slideDown()
}
});
$('nav li a').click(function () {
menu.slideUp()
});
}
});
希望它有所帮助!
答案 2 :(得分:0)
Those scripts可以为您提供帮助。
Modernizr也可以做得很好。