引导引导轮中的图像循环播放

时间:2019-06-11 11:27:38

标签: ruby-on-rails twitter-bootstrap bootstrap-carousel

尝试使用bootstrap carousal来显示附加到工作职位的多个图像时遇到麻烦。这是没有轮换模板的原始代码:

<% if @job.images.attached? %>
<% (0...@job.images.count).each do |image| %>

<%= image_tag(@job.images[image]) %>

<% end %>
    <% else %>
        <%= image_tag "missing.jpg" %>

                <% end %>

下面是我在w3学校中使用的引导程序示例,当我在应用程序上对其进行测试时,它可以很好地工作。

<div id="demo" class="carousel slide" data-ride="carousel">

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

  <!-- The slideshow -->
  <div class="carousel-inner">
    <div class="item active">
      <img src="la.jpg" alt="Los Angeles">
    </div>
    <div class="item">
      <img src="chicago.jpg" alt="Chicago">
    </div>
    <div class="item">
      <img src="ny.jpg" alt="New York">
    </div>
  </div>

  <!-- Left and right controls -->
  <a class="carousel-control-prev" href="#demo" data-slide="prev">
    <span class="carousel-control-prev-icon"></span>
  </a>
  <a class="carousel-control-next" href="#demo" data-slide="next">
    <span class="carousel-control-next-icon"></span>
  </a>

</div>

到目前为止,我已经尝试过:

<div id="demo" class="carousel slide" data-ride="carousel">

  <!-- Indicators -->
  <ul class="carousel-indicators">
    <% (0...@job.images.count).each do |image| %>
    <li data-target="#demo" data-slide-to=#{image} class="active"></li>
    <% end %>

  </ul>

  <!-- The slideshow -->
  <div class="carousel-inner">
    <% (0...@job.images.count).each do |image| %>
    <div class="item active">
    <%= image_tag(@job.images[image]) %>
    </div>
    <% end %>
  </div>

  <!-- Left and right controls -->
  <a class="carousel-control-prev" href="#demo" data-slide="prev">
    <span class="carousel-control-prev-icon"></span>
  </a>
  <a class="carousel-control-next" href="#demo" data-slide="next">
    <span class="carousel-control-next-icon"></span>
  </a>

</div>

图像相互叠放,当我单击下一张幻灯片时,所有图像消失。我不确定确切要放在数据幻灯片类中的内容,我怀疑这是导致问题。

UPDATE

当图像滑块自动播放时,此代码有效,但是手动单击图像滑块会使整个轮齿消失:

<div id="demo" class="carousel slide" data-ride="carousel">

  <!-- Indicators -->
  <ul class="carousel-indicators">
    <% @job.images.each_with_index do |image, index| %>
    <li data-target="#demo" data-slide-to=#{index} <%= index == 0 ? 'class="active"' : '' %>></li>
    <% end %>
  </ul>

  <!-- The slideshow -->
  <div class="carousel-inner">
    <% @job.images.each_with_index do |image, index| %>
    <div class="item <%= index == 0 ? 'active' : '' %>">
    <%= image_tag image %>
    </div>
    <% end %>
  </div>

  <!-- Left and right controls -->
  <a class="carousel-control-prev" href="#demo" data-slide="prev">
    <span class="carousel-control-prev-icon"></span>
  </a>
  <a class="carousel-control-next" href="#demo" data-slide="next">
    <span class="carousel-control-next-icon"></span>
  </a>

</div>

1 个答案:

答案 0 :(得分:0)

首先,您似乎正在将active类赋予幻灯片放映中的每个图像。其次,有更简单的方法可以遍历图像。

类似的事情应该起作用:

<div id="demo" class="carousel slide" data-ride="carousel">

  <!-- Indicators -->
  <ul class="carousel-indicators">
    <% @job.images.each_with_index do |image, index| %>
    <li data-target="#demo" data-slide-to=#{index} <%= index == 0 ? 'class="active"' : '' %>></li>
    <% end %>
  </ul>

  <!-- The slideshow -->
  <div class="carousel-inner">
    <% @job.images.each_with_index do |image, index| %>
    <div class="item <%= index == 0 ? 'active' : '' %>">
    <%= image_tag image %>
    </div>
    <% end %>
  </div>

  <!-- Left and right controls -->
  <a class="carousel-control-prev" href="#demo" data-slide="prev">
    <span class="carousel-control-prev-icon"></span>
  </a>
  <a class="carousel-control-next" href="#demo" data-slide="next">
    <span class="carousel-control-next-icon"></span>
  </a>

</div>