随机图像进入旋转木马

时间:2018-05-09 06:06:39

标签: javascript html slider carousel

我创建了一个滑块,它工作正常,但是当我滑动到下一个内容时,它会在滑块中显示随机图像。我希望在将这些图像添加到代码中时显示这些图像。我不想随意滑动图像。

Javascript

$('#myCarousel').carousel({
    interval: 000,
})

$('.carousel .item').each(function(){
    var next = $(this).next();
    if (!next.length) {
        next = $(this).siblings(':first');
    }
    next.children(':first-child').clone().appendTo($(this));

    for (var i=0;i<2;i++) {
        next=next.next();
        if (!next.length) {
            next = $(this).siblings(':first');
        }

        next.children(':first-child').clone().appendTo($(this));
    }
});

HTML

 <div class="carousel slide" id="myCarousel3" data-ride="carousel" data-interval="000" data-pause="hover">
            <div class="carousel-inner">

                <div class="item active">
                    <div class="col-xs-3"><img src="computers/img/dell.jpeg" alt=" " class="img-responsive thumb" />
                    </div>
                </div>

                <div class="item">
                    <div class="col-xs-3"><img src="computers/img/acer.jpg" alt=" " class="img-responsive thumb" />
                    </div>
                </div>
                <div class="item">
                    <div class="col-xs-3"><img src="computers/img/asus.jpg" alt=" " class="img-responsive thumb" />
                    </div>
                </div>
            </div>
            <a class="left carousel-control" href="#myCarousel3" data-slide="prev"><i class="glyphicon glyphicon-chevron-left"></i></a>
            <a class="right carousel-control" href="#myCarousel3" data-slide="next"><i class="glyphicon glyphicon-chevron-right"></i></a>   
        </div>      

2 个答案:

答案 0 :(得分:2)

在你的for循环中

只需更改

    for (var i=0;i<1;i++) {

我&lt; 2至i&lt; 1;我希望这是你想要的,因为我试过这个并得到结果是顺序的。
请检查
https://codepen.io/atulraj89/pen/zjRwxR
如果你想让结果成为别的东西,请解释一下。

答案 1 :(得分:0)

所以我在使用Jquery和bootstrap的Bootstrap上找到了一个例子。

HTML:

<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
    <ol class="carousel-indicators">
      <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
      <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
      <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
    </ol>
    <div class="carousel-inner" role="listbox">
      <!-- Slide One - Set the background image for this slide in the line below -->
      <div class="carousel-item active" style="background-image: url('computers/img/dell.jpeg')">
        <div class="carousel-caption d-none d-md-block">
          <h3>First Slide</h3>
          <p>This is a computer from Dell</p>
        </div>
      </div>
      <!-- Slide Two - Set the background image for this slide in the line below -->
      <div class="carousel-item" style="background-image: url('computers/img/acer.jpg')">
        <div class="carousel-caption d-none d-md-block">
          <h3>Second Slide</h3>
          <p>This is a computer from Acer</p>
        </div>
      </div>
      <!-- Slide Three - Set the background image for this slide in the line below -->
      <div class="carousel-item" style="background-image: url('computers/img/asus.jpg')">
        <div class="carousel-caption d-none d-md-block">
          <h3>Third Slide</h3>
          <p>This is a computer from Asus</p>
        </div>
      </div>
    </div>
    <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
      <span class="carousel-control-prev-icon" aria-hidden="true"></span>
      <span class="sr-only">Previous</span>
    </a>
    <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
      <span class="carousel-control-next-icon" aria-hidden="true"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>

CSS:

.carousel-item {
  height: 65vh;
  min-height: 300px;
  background: no-repeat center center scroll;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}

在HTML中,您需要添加CSS,Jquery和bootstrap js。

<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<script src="vendor/jquery/jquery.min.js"></script>
<link href="css/your_css.css" rel="stylesheet">

来源:https://startbootstrap.com/template-overviews/half-slider/