实现css滑块不起作用

时间:2016-01-01 21:00:17

标签: javascript jquery html css materialize

嘿,伙计们正在为我的网站使用materialize css。当我插入滑块的代码时,它只显示一个灰色的背景。这是我的代码:

<div class="slider">
<ul class="slides">
  <li>
    <img src="http://lorempixel.com/580/250/nature/1">
    <div class="caption center-align">
      <h3>This is our big Tagline!</h3>
      <h5 class="light grey-text text-lighten-3">Here's our small slogan.</h5>
    </div>
  </li>
  <li>
    <img src="http://lorempixel.com/580/250/nature/2">
    <div class="caption left-align">
      <h3>Left Aligned Caption</h3>
      <h5 class="light grey-text text-lighten-3">Here's our small slogan.</h5>
    </div>
  </li>
</ul>

1 个答案:

答案 0 :(得分:8)

由于您错过了jQuery Initialization,您的代码无效。在您的JS文件中包含以下script或在<script>页面中添加以下html标记。

<script>
   $(document).ready(function () {
       $('.slider').slider({full_width: true});
   });
</script>

以下是单html页中的完整工作代码。

<!DOCTYPE html>
<html ng-app>

    <head>
        <title>Slider</title>
        <!-- Compiled and minified CSS -->
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.3/css/materialize.min.css">
        <!--Let browser know website is optimized for mobile-->
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    </head>

    <body>
        <!--your code start-->
        <div class="slider">
            <ul class="slides">
                <li>
                    <img src="http://lorempixel.com/580/250/nature/1">
                    <div class="caption center-align">
                        <h3>This is our big Tagline!</h3>
                        <h5 class="light grey-text text-lighten-3">Here's our small slogan.</h5>
                    </div>
                </li>
                <li>
                    <img src="http://lorempixel.com/580/250/nature/2">
                    <div class="caption left-align">
                        <h3>Left Aligned Caption</h3>
                        <h5 class="light grey-text text-lighten-3">Here's our small slogan.</h5>
                    </div>
                </li>
            </ul>
        </div>
        <!--your code end-->

        <!--Import jQuery before materialize.js-->
        <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>

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

        <!--Materializecss Slider-->
        <script>
            $(document).ready(function () {
                $('.slider').slider({full_width: true});
            });
        </script>         
    </body>

</html>