JavaScript轮播无法正常工作

时间:2016-05-31 10:51:43

标签: javascript html css carousel

因此,我试图为演示文稿制作旋转木马全屏幕。我不是一个在编程阶段的专业编码人员。我有这个代码,我有你可以看到和错误和旋转木马都被打破,我不知道为什么。我可以感谢一些指导或帮助。

var $item = $('.carousel .item');
var $wHeight = $(window).height();

$item.height($wHeight); 
$item.addClass('full-screen');

var $numberofSlides = $('.item').length;
var $currentSlide = Math.floor((Math.random() * $numberofSlides));

$('.carousel-indicators li').each(function(){
  var $slideValue = $(this).attr('data-slide-to');
  if($currentSlide == $slideValue) {
    $(this).addClass('active');
    $item.eq($slideValue).addClass('active');
  } else {
    $(this).removeClass('active');
    $item.eq($slideValue).removeClass('active');
  }
});

$('.carousel img').each(function() {
  var $src = $(this).attr('src');
  var $color = $(this).attr('data-color');
  $(this).parent().css({
    'background-image' : 'url(' + $src + ')',
    'background-color' : $color
  });
  $(this).remove();
});

$(window).on('resize', function (){
  $wHeight = $(window).height();
  $item.height($wHeight);
});

$('.carousel').carousel({
  interval: 6000,
  pause: "false"
});
h3 {
  display: inline-block;
  padding: 10px;
  background: #B9121B;
  border-top-left-radius: 10px;
  border-top-right-radius: 10px;
}

.full-screen {
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
}
<!DOCTYPE html>
<html>
<head>
<script src="sliderjava.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="sliderstyle.css">	
</head>
<body>
<div id="mycarousel" class="carousel slide" data-ride="carousel">
  <!-- Indicadores -->
  <ol class="carousel-indicators">
    <li data-target="#mycarousel" data-slide-to="0"></li>
    <li data-target="#mycarousel" data-slide-to="1"></li>
    <li data-target="#mycarousel" data-slide-to="2"></li>
    <li data-target="#mycarousel" data-slide-to="3"></li>
    <li data-target="#mycarousel" data-slide-to="4"></li>
  </ol>

  <!-- Slides -->
  <div class="carousel-inner" role="listbox">
    <div class="item">
        <img src="https://unsplash.it/2000/1250?image=397" data-color="lightblue" alt="First Image">
        <div class="carousel-caption">
            <h3>First Image</h3>
        </div>
    </div>
    <div class="item">
        <img src="https://unsplash.it/2000/1250?image=689" data-color="firebrick" alt="Second Image">
        <div class="carousel-caption">
            <h3>Second Image</h3>
        </div>
    </div>
    <div class="item">
        <img src="https://unsplash.it/2000/1250?image=675" data-color="violet" alt="Third Image">
        <div class="carousel-caption">
            <h3>Third Image</h3>
        </div>
    </div>
    <div class="item">
        <img src="https://unsplash.it/2000/1250?image=658" data-color="lightgreen" alt="Fourth Image">
        <div class="carousel-caption">
            <h3>Fourth Image</h3>
        </div>
    </div>
    <div class="item">
        <img src="https://unsplash.it/2000/1250?image=638" data-color="tomato" alt="Fifth Image">
        <div class="carousel-caption">
            <h3>Fifth Image</h3>
        </div>
    </div>
  </div>

  <!-- Controlos -->
  <a class="left carousel-control" href="#mycarousel" 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="#mycarousel" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

</body>
</html>

2 个答案:

答案 0 :(得分:1)

请添加jquery引用,它应该在页面中的carouse脚本之前。

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.2.min.js"></script>

答案 1 :(得分:0)

您正在使用bootstraps的旋转木马但是您没有附加任何必要的东西以使其工作不幸。请做以下事情以使您的轮播工作。

  1. 附加Bootstrap的CSS。
  2. 附加jQuery库。
  3. 附加Bootstrap的javascript。
  4. var $item = $('.carousel .item');
        var $wHeight = $(window).height();
    
        $item.height($wHeight); 
        $item.addClass('full-screen');
    
        var $numberofSlides = $('.item').length;
        var $currentSlide = Math.floor((Math.random() * $numberofSlides));
    
        $('.carousel-indicators li').each(function(){
          var $slideValue = $(this).attr('data-slide-to');
          if($currentSlide == $slideValue) {
            $(this).addClass('active');
            $item.eq($slideValue).addClass('active');
          } else {
            $(this).removeClass('active');
            $item.eq($slideValue).removeClass('active');
          }
        });
    
        $('.carousel img').each(function() {
          var $src = $(this).attr('src');
          var $color = $(this).attr('data-color');
          $(this).parent().css({
            'background-image' : 'url(' + $src + ')',
            'background-color' : $color
          });
          $(this).remove();
        });
    
        $(window).on('resize', function (){
          $wHeight = $(window).height();
          $item.height($wHeight);
        });
    
        $('.carousel').carousel({
          interval: 6000,
          pause: "false"
        });
    h3 {
        display: inline-block;
        padding: 10px;
        background: #B9121B;
        border-top-left-radius: 10px;
        border-top-right-radius: 10px;
    }
    
    .full-screen {
        background-size: cover;
        background-position: center;
        background-repeat: no-repeat;
    }
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    
    <!DOCTYPE html>
        <html>
        <head>
        <script src="sliderjava.js" type="text/javascript"></script>
        <link rel="stylesheet" type="text/css" href="sliderstyle.css">	
        </head>
        <body>
        <div id="mycarousel" class="carousel slide" data-ride="carousel">
          <!-- Indicadores -->
          <ol class="carousel-indicators">
            <li data-target="#mycarousel" data-slide-to="0"></li>
            <li data-target="#mycarousel" data-slide-to="1"></li>
            <li data-target="#mycarousel" data-slide-to="2"></li>
            <li data-target="#mycarousel" data-slide-to="3"></li>
            <li data-target="#mycarousel" data-slide-to="4"></li>
          </ol>
    
          <!-- Slides -->
          <div class="carousel-inner" role="listbox">
            <div class="item">
                <img src="https://unsplash.it/2000/1250?image=397" data-color="lightblue" alt="First Image">
                <div class="carousel-caption">
                    <h3>First Image</h3>
                </div>
            </div>
            <div class="item">
                <img src="https://unsplash.it/2000/1250?image=689" data-color="firebrick" alt="Second Image">
                <div class="carousel-caption">
                    <h3>Second Image</h3>
                </div>
            </div>
            <div class="item">
                <img src="https://unsplash.it/2000/1250?image=675" data-color="violet" alt="Third Image">
                <div class="carousel-caption">
                    <h3>Third Image</h3>
                </div>
            </div>
            <div class="item">
                <img src="https://unsplash.it/2000/1250?image=658" data-color="lightgreen" alt="Fourth Image">
                <div class="carousel-caption">
                    <h3>Fourth Image</h3>
                </div>
            </div>
            <div class="item">
                <img src="https://unsplash.it/2000/1250?image=638" data-color="tomato" alt="Fifth Image">
                <div class="carousel-caption">
                    <h3>Fifth Image</h3>
                </div>
            </div>
          </div>
    
          <!-- Controlos -->
          <a class="left carousel-control" href="#mycarousel" 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="#mycarousel" role="button" data-slide="next">
            <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
          </a>
        </div>
    
        </body>
        </html>