【问题标题】:Bootstrap Carousel Not working引导轮播不工作
【发布时间】:2012-03-10 07:56:47
【问题描述】:

我正在尝试实现 Twitter Bootstrap 轮播,但到目前为止我一直没有成功。我有以下页面:

<!DOCTYPE html>
<html lang = "en">
<head>
  <meta charset="utf-8">
  <title>Fluxware: Play, learn and build games</title>
  <meta name="description" content="Home page for Fluxware">
  <meta name="author" content="Tim Butram">

  <!-- CSS Stylesheets -->
  <link href="../bootstrap/css/bootstrap.css" rel="stylesheet">

  <link href="../bootstrap/css/bootstrap-responsive.css" rel="stylesheet">

  <!--For shitty webbrowsers -->
  <!--[if lt IE 9]>
    <script src="//html5shim.googlecode.com/sbn/trunk/html5.js></script>
  <![endif]-->

  <!-- Favicon -->
  <link rel="favicon" href="images/favicon.ico">
</head>

<body>

  <!-- Navbar -->

  <div class="navbar">
    <div class="navbar-inner">
      <div class="container">
        <ul class="nav">
          <li class="active"><a href="#">Home</a></li>
          <li><a href="#">Play</a></li>
          <li><a href="#">Learn</a></li>

          <li><a href="#">Build</a></li>
        </ul>
      </div>
    </div>
  </div>

  <!-- Grid -->
  <div class="container">
  <div class="row">

    <div class="span12">
      <div class="hero-unit">
    <div id="myCarousel" class="carousel">
          <div class="carousel-inner">
            <div class="active item"><img src="/images/1.png" /></div>
        <div class="item"><img src="./images/2.png" /></div>
        <div class="item"><img src="./images/3.png" /></div>
          </div>

      <a class="carousel-control left" href="#myCarousel" data-slide="prev">&lsaquo;</a>
      <a class="carousel-control right" href="#myCarousel" data-slide="next">&rsaquo;</a>
        </div>
      </div>

      <div class="row">
        <div class="span4">
           <a class="btn btn-large">Play</a>
       <p>Play games created with Fluxware.</p>

        </div>

    <div class="span4">
      <a class="btn btn-large">Learn</a>
      <p>Learn how to create games and game assets</p>
    </div>

    <div class="span4">
          <a class="btn btn-large">Build</a>

      <p>Use the Fluxware tools to create game.</p>
        </div>
      </div>
    </div>
  </div>
  </div>
  <script src="../bootstrap/js/jquery.js"></script>
  <script src="../bootstrap/js/bootstrap-carousel.js"></script>

  <script src="../bootstrap/js/bootstrap-transition.js"></script>
</body>
</html>

除了轮播外,一切似乎都正确渲染。从阅读引导页面,看来我需要坚持一个

$('.myCarousel').carousel();

某处,但我不知道在哪里、如何或为什么。除此之外,在 Firefox 错误控制台中,我得到了两个错误

$ is undefined

在 bootstrap-carousel.js [第 125 行] 和

$ is not a function

在 bootstrap-transition.js [第 22 行]

【问题讨论】:

  • 据我了解,$('.carousel').carousel();是 jQuery 在 id = carousel 的 DIV 上调用 carousel 方法。所以,我把它放在底部的脚本中。不幸的是,这似乎没有做任何事情,除了创建另一个错误'$未定义'

标签: javascript twitter-bootstrap


【解决方案1】:

显然 jQuery 没有正确加载。
尝试加载页面&lt;head&gt;中的两个脚本。

运行轮播的代码是

<script>
// Load this when the DOM is ready
$(function(){
  // You used .myCarousel here. 
  // That's the class selector not the id selector,
  // which is #myCarousel
  $('#myCarousel').carousel();
});
</script>

您可以将它放在其他两个脚本之后的&lt;head&gt;
或者其他脚本现在在哪里。

【讨论】:

  • 呃。你是正确的,jQuery 没有正确加载。它甚至不在那里。感谢您对我的 js sn-p 位置的帮助。
【解决方案2】:

遇到同样的问题。

  • 对我来说,我不得不使用 jQuery 1.7.1,因为 1.6.4 不起作用。
  • 我必须包含bootstrap-transition

【讨论】:

    【解决方案3】:

    最近遇到了同样的问题,看来在当前版本的Bootstrap中,还必须包含一个叫slide的类。

    <div id="myCarousel" class="carousel slide span12">
    

    【讨论】:

      【解决方案4】:

      你定义为id不是类,所以你应该用jquery的这段代码启动轮播,所以替换

      $('#myCarousel').carousel();
      

      而不是

      $('.myCarousel').carousel();
      

      【讨论】:

        【解决方案5】:

        我使用的是Django's JQuery(我不知道版本),但它不起作用。 但是,它适用于Jquery 1.7.1

        【讨论】:

          【解决方案6】:

          如果您在让 Bootstrap 轮播幻灯片正常工作时遇到困难 -- 这是一个带有可定位控件的模板,底部有一个跟踪幻灯片的进度导航栏。

          [http://codepen.io/TheNickelDime/][1]

          <div class="Slideshow_carousel-wrapper container-fluid">
              <span class="Slideshow-ctl-L" onclick="carousel_left()"></span>
              <div class="carousel slide" data-ride="carousel"  ... </div>    
              <span class="Slideshow-ctl-R" onclick="carousel_right()"></span>
          
          </div>
          
             <div class="container-fluid Slideshow_progress-bar">     
                  <ul id="slideshow_progress" class="Slideshow_progress"></ul>
              </div>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-04-16
            • 1970-01-01
            • 1970-01-01
            • 2021-10-10
            • 1970-01-01
            相关资源
            最近更新 更多