Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
353 views
in Technique[技术] by (71.8m points)

javascript - Materialize side-nav links doesn't work

I'm making an web with materializecss but links I add on the side nav doesn't work what can I do with it,

When I click the link the side-nav close code:

<div class="navbar-fixed">
    <nav>
        <div class="nav-wrapper">
            <a href="#" data-activates="mobile-demo" class="button-collapse"><i class="material-icons">menu</i></a>
            <a href="index.php" class="brand-logo left thin" title="bcgraphixx">Page Title</a>
            <ul class="right hide-on-med-and-down">
                <li><a class="waves-effect" href="index.php">Home</a></li>
                <li><a class="waves-effect" href="about-us.php">About us</a></li>
                <li><a class="waves-effect" href="services.php">Services</a></li>
                <li><a class="waves-effect" href="book.php">booking</a></li>
            </ul>

            <ul class="side-nav" id="mobile-demo">
                <li>
                    <div class="userView">
                        <div class="background">
                            <img src="<?php echo $srcurl; ?>/cover.png">
                        </div>
                        <a href="#!admin"><img class="circle" src="<?php echo $srcurl; ?>/logo.png"></a>
                        <a href="#!bcgraphix" title="bcgraphixTemplate"><span class="black-text name">DIGINEWS</span></a>

                    </div>
                </li>

                <li><a href="##"><center>MENU</center></a></li>
                <li><a class="waves-effect" href="index.php">Home</a></li>
                <li><a class="waves-effect" href="about-us.php">About us</a></li>
                <li><a class="waves-effect" href="services.php">Services</a></li>
                <li><a class="waves-effect" href="book.php">booking</a></li>
            </ul>

            <script>  $(".button-collapse").sideNav(); </script>
        </div>

    </nav>
</div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. Wrap your JS code for sideNav within documentReady event, and set parameter closeOnClick as true.

  2. Bring your side menu (ul element with class"side-nav") at the same level of div with class navbar-fixed . This is to avoid known issues with overlay. See this stackoverflow question and accepted answer

$( document ).ready(function(){
$('.button-collapse').sideNav({
      closeOnClick: true
    }
  );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.0/css/materialize.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.0/js/materialize.min.js"></script>

<div class="navbar-fixed">
    <nav>
        <div class="nav-wrapper">
            <a href="#" data-activates="mobile-demo" class="button-collapse"><i class="material-icons">menu</i></a>
            <a href="#" class="brand-logo right thin" title="bcgraphixx">Page Title</a>
            <ul class="right hide-on-med-and-down">
                <li><a class="waves-effect" href="#">Home</a></li>
                <li><a class="waves-effect" href="#">About us</a></li>
            </ul>
        </div>
    </nav>
</div>
<ul class="side-nav" id="mobile-demo">
   <li><a href="##"><center>MENU</center></a></li>
   <li><a class="waves-effect" href="#!">Home</a></li>
   <li><a class="waves-effect" href="#!">About us</a></li>
</ul>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...