【发布时间】:2015-06-22 01:44:31
【问题描述】:
我有一个仅在 CSS 媒体查询中显示的交互式元素,它将另一个元素(导航元素)的显示从无切换到阻塞。在媒体查询之外(任何显示宽度大于 580 像素),导航元素显示设置为块。当我在媒体查询中切换显示并将导航设置为不显示时,然后将浏览器的大小重新调整为大于 580 像素的宽度,导航元素仍设置为不显示。如何使在媒体查询中切换导航的显示不会影响其在媒体查询之外的显示?
<!-- Toggle Display
// click on the element
function toggle(e, id) {
var el = document.getElementById(id);
el.style.display = (el.style.display == 'none' || el.style.display == '') ? 'block' : 'none';
// save it for hiding
toggle.el = el;
// stop the event here
if (e.stopPropagation) e.stopPropagation();
e.cancelBubble = true;
return false;
}
// click outside the element
document.onclick = function() {
if (toggle.el) {
toggle.el.style.display = 'none';
}
}
//-->
nav {
color: white ;
float: right ;
margin: 0 10px 0 0 ;
display: block ;
}
nav a {
margin-right: 25px ;
padding: 5px 4px 2px 4px ;
display: inline-block ;
}
.nav-text {
display: none ; /* replaced by icons */
font-size: 18px ;
}
@media all and (max-width: 580px) {
nav {
display: none ;
float: left ;
width: 100% ;
margin: 3px 0 0 0 ;
clear: both ;
}
.menu {
display: block ;
}
nav a {
display: block ;
background: gray ;
color: white ;
padding: 10px 20px ;
width: 100% ;
border-bottom: 1px solid #ccc ;
}
}
<header>
<div onclick="toggle(event, 'nav')" class="menu" tabindex=0>
<!-- Menu icon -->
</div>
<nav id="nav">
<a href="/"><span class="nav-text">Home</span></a>
<a href="#"><span class="nav-text">About</span></a>
</nav>
</header>
【问题讨论】:
标签: javascript html css media-queries