2016-09-10 75 views
0

我想用下拉菜單實現sidenav。目前的情況是,它始終可以全屏顯示,並隱藏在小屏幕上。CSS - 實現SideNav不關閉

請注意,下拉功能正在工作,這讓我相信jQuery正在工作。

HTML片段:

<!DOCTYPE html> 
<html> 
<head> 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 

    <script src="https://code.jquery.com/jquery-2.2.1.min.js"></script> 

    <!-- Compiled and minified CSS --> 
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.7/css/materialize.min.css"> 

    <!-- Compiled and minified JavaScript --> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.7/js/materialize.min.js"></script> 


    <style type="text/css"> 

    body { 
    display: flex; 
    min-height: 100vh; 
    flex-direction: column; 
    } 

    main { 
    flex: 1 0 auto; 
    } 

    </style> 
<script language="javascript"> 
    $('.button-collapse').sideNav({ 
     menuWidth: 300, // Default is 240 
     closeOnClick: true // Closes side-nav on <a> clicks, useful for Angular/Meteor 
    } 
); 
    $('.collapsible').collapsible(); 
</script> 



</head> 

    <body> 
<main> 

    <!-- Side Bar --> 
<nav> 

    <!-- Navigation Bar --> 

<div class="nav-wrapper"> 
     <a href="/" class="brand-logo center">Wagba</a> 
     <ul id="nav-mobile" class="left hide-on-med-and-down"> 
     <a href="#" data-activates="slide-out" class="button-collapse"><i class="mdi-navigation-menu"></i></a> 
     </ul> 


<ul class="hide-on-med-and-down"> 
    <li><a href="#!">First Sidebar Link</a></li> 
    <li><a href="#!">Second Sidebar Link</a></li> 
    </ul> 
    <ul id="slide-out" class="side-nav fixed"> 
     <li class="bold"><a href="#!" class="waves-effect waves-teal">First Sidebar Link</a></li> 
     <li class="bold"><a href="#!" class="">Second Sidebar Link</a></li> 
     <li class="no-padding"> 
     <ul class="collapsible collapsible-accordion"> 
      <li> 
      <a class="collapsible-header waves-effect waves-teal">Dropdown<i class="mdi-navigation-arrow-drop-down"></i></a> 
      <div class="collapsible-body"> 
       <ul> 
       <li><a href="#!">First</a></li> 
       <li><a href="#!">Second</a></li> 
       <li><a href="#!">Third</a></li> 
       <li><a href="#!">Fourth</a></li> 
       </ul> 
      </div> 
      </li> 
     </ul> 
     </li> 
    </ul> 

    </div> 
</nav> 
<!-- Navigation Bar End --> 

回答

3

您需要內 $(文件)。就緒(函數(){ ... })寫劇本的初始化;

檢查下面的改正頁:

<!DOCTYPE html> 
 
<html> 
 
<head> 
 

 
    <script src="https://code.jquery.com/jquery-2.2.1.min.js"></script> 
 
<link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> 
 
    <!-- Compiled and minified CSS --> 
 
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.7/css/materialize.min.css"> 
 

 
    <!-- Compiled and minified JavaScript --> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.7/js/materialize.min.js"></script> 
 

 

 
    <style type="text/css"> 
 

 
    body { 
 
    display: flex; 
 
    min-height: 100vh; 
 
    flex-direction: column; 
 
    } 
 

 
    main { 
 
    flex: 1 0 auto; 
 
    } 
 

 
    </style> 
 
<script language="javascript"> 
 
$(document).ready(function(){ 
 
\t $('.button-collapse').sideNav({ 
 
\t \t menuWidth: 300, // Default is 240 
 
\t \t closeOnClick: true // Closes side-nav on <a> clicks, useful for Angular/Meteor 
 
\t \t } 
 
\t ); 
 
\t $('.collapsible').collapsible(); 
 
    }); 
 
</script> 
 

 

 

 
</head> 
 

 
    <body> 
 
<main> 
 

 
    <!-- Side Bar --> 
 
<nav> 
 

 
    <!-- Navigation Bar --> 
 

 
<div class="nav-wrapper"> 
 
     <a href="/" class="brand-logo center">Wagba</a> 
 
     <a href="#" data-activates="slide-out" class="button-collapse"><i class="material-icons">menu</i></a> 
 

 

 

 
<ul class="hide-on-med-and-down"> 
 
    <li><a href="#!">First Sidebar Link</a></li> 
 
    <li><a href="#!">Second Sidebar Link</a></li> 
 
    </ul> 
 
    <ul id="slide-out" class="side-nav"> 
 
     <li class="bold"><a href="#!" class="waves-effect waves-teal">First Sidebar Link</a></li> 
 
     <li class="bold"><a href="#!" class="">Second Sidebar Link</a></li> 
 
     <li class="no-padding"> 
 
     <ul class="collapsible collapsible-accordion"> 
 
      <li> 
 
      <a class="collapsible-header waves-effect waves-teal">Dropdown<i class="mdi-navigation-arrow-drop-down"></i></a> 
 
      <div class="collapsible-body"> 
 
       <ul> 
 
       <li><a href="#!">First</a></li> 
 
       <li><a href="#!">Second</a></li> 
 
       <li><a href="#!">Third</a></li> 
 
       <li><a href="#!">Fourth</a></li> 
 
       </ul> 
 
      </div> 
 
      </li> 
 
     </ul> 
 
     </li> 
 
    </ul> 
 

 
    </div> 
 
</nav> 
 
<!-- Navigation Bar End -->