Bootstrap カルーセルはslid.bs.carousel
eventを提供します。このイベントは、カルーセルがスライド遷移を完了したときに発生します。
疑似コードでは、答えは次のようになります。
----- on slide changed (slid.bs.carousel)
> if active slide is the last one, hide the right control
> if not, show it
> if active slide is the first one, hide the left control
> if not, show it
HTML
<div id="carousel-example-generic" class="carousel slide">
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1" class=""></li>
<li data-target="#carousel-example-generic" data-slide-to="2" class=""></li>
</ol>
<div class="carousel-inner">
<div class="item active">
<img data-src="holder.js/900x500/auto/#777:#555/text:First slide" alt="First slide" src="...">
</div>
<div class="item">
<img data-src="holder.js/900x500/auto/#666:#444/text:Second slide" alt="Second slide" src="...">
</div>
<div class="item">
<img data-src="holder.js/900x500/auto/#555:#333/text:Third slide" alt="Third slide" src="...">
</div>
</div>
<a class="left carousel-control" href="#carousel-example-generic" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div>
jQuery
// get the carousel
var $carousel = $(".carousel");
// pause it
$carousel.carousel('pause');
// get right & left controls
var $rightControl = $carousel.find(".right.carousel-control");
var $leftControl = $carousel.find(".left.carousel-control");
// hide the left control (first slide)
$leftControl.hide();
// get 'slid' event (slide changed)
$carousel.on('slid.bs.carousel', function() {
// get active slide
var $active = $carousel.find(".item.active");
// if the last slide,
if (!$active.next().length) {
// hide the right control
$rightControl.fadeOut();
// if not,
} else {
// show the right control
$rightControl.fadeIn();
}
// if the first slide,
if (!$active.prev().length) {
// hide the left control
$leftControl.fadeOut();
// if not,
} else {
// show it
$leftControl.fadeIn();
}
});
JSFIDDLE
古いバージョンの Bootstrap では、イベントはslid
.