Bootstrap Carousel不会滑动

时间:2015-02-19 18:25:31

标签: jquery css twitter-bootstrap carousel

我正在使用bootsrap轮播:

<header id="carousel" class="carousel slide">
        <!-- Indicators -->
        <ol class="carousel-indicators">
            <li data-target="#carousel" data-slide-to="0" class="active"></li>
            <li data-target="#carousel" data-slide-to="1"></li>
            <li data-target="#carousel" data-slide-to="2"></li>
        </ol>

        <!-- Wrapper for slides -->
        <div class="carousel-inner">
            <div class="item active">
                <div class="fill" style="background-image:url('images/fotografie1.JPG');"></div>
                <div class="carousel-caption">
                    <h2>Caption 1</h2>
                    <p>hihihaio</p>
                </div>
            </div>
            <div class="item">
                <div class="fill" style="background-image:url('images/fotografie1.JPG');"></div>
                <div class="carousel-caption">
                    <h2>Caption 2</h2>
                </div>
            </div>
            <div class="item">
                <div class="fill" style="background-image:url('images/fotografie1.JPG');"></div>
                <div class="carousel-caption">
                    <h2>Caption 3</h2>
                </div>
            </div>
        </div>

        <!-- Controls -->
         <a class="left carousel-control" href="#carousel" role="button" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="right carousel-control" href="#carousel" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>
    </header>

图片没有滑动,您也无法点击进入上一张或下一张图片。我有正确的(更新的)版本和我的头脑。以下是我的想法。

<!-- Script to Activate the Carousel -->
<script>

$('.carousel').carousel({
    interval: 5000 //changes the speed
})
</script>

我已经找到了同样问题的其他人,他们的解决方案与我的解决方案无关。当我在谷歌浏览器中打开并使用元素检查器时,我收到了这些错误,这是什么意思?

enter image description here

2 个答案:

答案 0 :(得分:1)

确保在使用JQuery Javascript之前加载了JQuery,例如Bootstrap。

你的代码很好。

JSFiddle

右:(在加载Jquery之后引用JQuery)

<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>

错误:(在加载JQuery之前引用JQuery)

<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

答案 1 :(得分:1)

您的HTML似乎不包含运行Bootstrap Javascript和其他jQuery依赖组件所需的jQuery。

在包含Bootstrap.js等其他所有组件之前,必须在身体末尾包含jQuery(请参阅下面的示例Bootstrap模板)。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap 101 Template</title>

    <!-- Bootstrap stylesheet-->
    <link href="css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>
    YOUR CAROUSEL HERE (marked with class='carousel')

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

    <!-- Bootstrap.js (Include all compiled plugins (below), or include individual files as needed) -->
    <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>

    <!-- Script to Activate the Carousel -->
    <script>  
        $('.carousel').carousel({
            interval: 5000 //changes the speed
        });
    </script>
  </body>
</html>