自动将指标添加到引导轮播 [英] Add indicators automatically to bootstrap carousel

查看:12
本文介绍了自动将指标添加到引导轮播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如您所见,我在示例中有引导轮播,并且我不想在html中添加指标,我只想添加js以适用于每个轮播,并且我在互联网上找到了一个函数,它工作得很好,但是只为一个轮播工作

I have bootstrap carousel in example as youu see and I don't want to add indicators in html I just want to add in js to work for every carousel and I found a function on the internet and it's work very well but it's work just for one carousel

我有两个轮播#carousel-1 #carousel-2 ,但是我的功能仅适用于#carousel-1 .知道如果我给同一个班级,我的问题就可以解决,但是轮播ID和指标的 li.data-target 必须相同.

I have two carousel #carousel-1 and #carousel-2 but my function work is just for #carousel-1.I know if I give same class my problem will be solved but carousel id and li.data-target of indicators must be same.

如何使用js自动添加 carousel-indicators 以查看其指标?

How can I add carousel-indicators automatically with js to see their indicators ?

  var myCarousel = $("#carousel-1");
  myCarousel.append("<ol class='carousel-indicators'></ol>");
  var indicators = $(".carousel-indicators"); 
  myCarousel.find(".carousel-inner").children(".item").each(function(index) {
      (index === 0) ? 
      indicators.append("<li data-target='#carousel-1' data-slide-to='"+index+"' class='active'></li>") : 
      indicators.append("<li data-target='#carousel-1' data-slide-to='"+index+"'></li>");
  });     

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

.carousel{
  margin:20px;
}

<!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">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <!-- Bootstrap -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">


    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <h1>SLİDER 1</h1>

    <!-- SLIDER 1-->
    <div id="carousel-1" class="carousel slide" data-ride="carousel">

  <div class="carousel-inner" role="listbox">
    <div class="item active">
      <img src="https://cdn.pixabay.com/photo/2016/12/26/02/13/bishan-1931390_960_720.jpg" alt="...">
    </div>
    <div class="item">
      <img src="https://cdn.pixabay.com/photo/2016/12/26/15/34/simplicity-1932331_960_720.jpg" alt="...">
      <div class="carousel-caption">
        
      </div>
    </div>
  </div>

  <a class="left carousel-control" href="#carousel-1" 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-1" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>
    <!-- SLIDER 1 END-->
    
    <h1>SLİDER 2</h1>
    
    <!-- SLIDER 2-->
    <div id="carousel-2" class="carousel slide" data-ride="carousel">

  <div class="carousel-inner" role="listbox">
    <div class="item active">
      <img src="https://cdn.pixabay.com/photo/2016/12/26/02/13/bishan-1931390_960_720.jpg" alt="...">
    </div>
    <div class="item">
      <img src="https://cdn.pixabay.com/photo/2016/12/26/15/34/simplicity-1932331_960_720.jpg" alt="...">
      <div class="carousel-caption">
        
      </div>
    </div>
  </div>

  <a class="left carousel-control" href="#carousel-2" 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-2" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>
    <!-- SLIDER 2 END-->
    
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  </body>
</html>

推荐答案

您可以通过这种方式使它动态化.此解决方案可以在您拥有的任意数量的轮播中使用,而无需进行任何更改:)

You can make it dynamic this way. This solution will work in any number of carousel you have without any changes :)

$(document).ready(function(){  
    var myCarousels = $(".carousel");
    myCarousels.each(function( index, element  ) {
        var myCarousel = $("#"+$(element).attr('id')); 
        myCarousel.append("<ol class='carousel-indicators'></ol>");
        var indicators = $("#"+$(element).attr('id') + " .carousel-indicators"); 
        $("#"+$(element).attr('id') +" .carousel-inner").children(".item").each(function(index) {
            console.log(index);
            (index === 0) ? 
            indicators.append("<li data-target='#"+$(element).attr('id')+"' data-slide-to='"+index+"' class='active'></li>") : 
            indicators.append("<li data-target='#"+$(element).attr('id')+"' data-slide-to='"+index+"'></li>");
        });
    });
});

这是工作示例: https://jsfiddle.net/bhumi/98L5Lu4p/1/

这篇关于自动将指标添加到引导轮播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆