自动将指示器添加到引导轮播



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

我有两个旋转木马#carousel-1#carousel-2但我的功能工作只是为了#carousel-1。我知道如果我给同一个班级,我的问题就会得到解决,但轮播 ID 和指标li.data-target必须相同。

如何使用 js 自动添加carousel-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>

你可以通过这种方式让它动态化。此解决方案将适用于您拥有的任意数量的轮播,无需进行任何更改:)

$(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/

function appendIndicator(carousel)
{
var myCarousel = $(carousel);
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 +"' data-slide-to='"+index+"' class='active'></li>") : 
indicators.append("<li data-target='" + carousel + "' data-slide-to='"+index+"'></li>");
});     
}
$('.carousel').carousel();
appendIndicator('#carousel-1');
appendIndicator('#carousel-2');

演示 JSFiddle

相关内容

  • 没有找到相关文章

最新更新