我通过window.matchMedia 和addlistener 找到了答案。基本上,我为媒体查询添加了一个 javascript 侦听器。只要条件(屏幕大小)越界,javascript就会触发。
我在the jsfiddle 上发布了一个可行的解决方案。
html
<div class="page-container">
<div class="div-menu-toggle" id="div-menu-toggle">
<button type="button" onClick="showmenu();">Menu</button>
</div>
<div class="main-nav-vert visible" id="main-nav-vert">
<ul class="nav-vert">
<li><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a></li>
<li><a href="#">Link 3</a></li>
</ul>
</div>
<div class="main-content" id="main-content">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</div>
</div>
css
.main-nav-vert{
display: block;
float:left;
width:145px;
border:solid 2px #00f;
}
.div-menu-toggle{
display: none;
}
.main-content{
max-width:808px;
width: auto;
padding:0 9px 0 9px;
margin-left: 145px;
}
javascript
var jmq = window.matchMedia( "(max-width: 480px)" )
var keepOpen = false;
jmq.addListener(jmqListener);
window.onload = function() {
jmqListener(jmq);
};
function jmqListener(jmq){
var button = document.getElementById('div-menu-toggle');
var menu = document.getElementById('main-nav-vert');
var content = document.getElementById('main-content');
if (jmq.matches) {
// window width is less than 480px
//show menu button
button.style.display = "block";
if (keepOpen == false){
// if keepOpen flag is not set, hide the menu
menu.style.display = "none";
// set left margin of main-content to fill screen
content.style.marginLeft = "2px";
}
}
else {
// window width is at least 480px
//hide menu button
button.style.display = "none";
// if show the menu
menu.style.display = "block";
// set left margin of main-content to make room
content.style.marginLeft = "145px";
}
}
function showmenu() {
var menu = document.getElementById('main-nav-vert');
var content = document.getElementById('main-content');
// with click on menu button
if(menu.style.display == "block"){
// if menu is already open, hide it
menu.style.display = "none";
keepOpen = false;
// set left margin of main-content to fill screen
content.style.marginLeft = "2px";
}
else {
// menu is closed - show the menu
menu.style.display = "block";
// set a flag that it is open
keepOpen = true;
// set left margin of main-content to make room
content.style.marginLeft = "145px";
}
}