以下是“利用BootStrap的Carousel.js实现轮播图动画效果”的完整攻略。
步骤一:引入Bootstrap和JQuery库
<!-- 引入Bootstrap样式 -->
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- 引入Bootstrap库文件 -->
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!-- 引入jQuery库文件 -->
<script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>
步骤二:编写轮播图HTML结构
<div id="myCarousel" class="carousel slide">
<!-- 轮播图指示器 -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<!-- 轮播图内容 -->
<div class="carousel-inner">
<div class="item active">
<img src="img/carousel1.jpg" alt="">
<div class="carousel-caption">
<h3>First slide label</h3>
<p>Nulla vitae elit libero, a pharetra augue mollis interdum.</p>
</div>
</div>
<div class="item">
<img src="img/carousel2.jpg" alt="">
<div class="carousel-caption">
<h3>Second slide label</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
</div>
<div class="item">
<img src="img/carousel3.jpg" alt="">
<div class="carousel-caption">
<h3>Third slide label</h3>
<p>Praesent commodo cursus magna, vel scelerisque nisl consectetur.</p>
</div>
</div>
</div>
<!-- 轮播图控制 -->
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div>
步骤三:初始化轮播图
$(document).ready(function(){
// 初始化轮播图
$('#myCarousel').carousel({
interval: 3000 // 轮换间隔时间
});
});
至此,一个简单的轮播图已经完成了。其中,carousel-indicators
表示指示器,carousel-inner
表示轮播内容,carousel-control
表示轮播控制,这三者相互配合实现轮播图的功能。
以下是两个轮播图的示例说明:
示例一:轮播图带标题
<div id="myCarousel" class="carousel slide">
<!-- 轮播图指示器 -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<!-- 轮播图内容 -->
<div class="carousel-inner">
<div class="item active">
<img src="img/carousel1.jpg" alt="">
<div class="carousel-caption">
<h3>First slide label</h3>
<p>Nulla vitae elit libero, a pharetra augue mollis interdum.</p>
</div>
</div>
<div class="item">
<img src="img/carousel2.jpg" alt="">
<div class="carousel-caption">
<h3>Second slide label</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
</div>
<div class="item">
<img src="img/carousel3.jpg" alt="">
<div class="carousel-caption">
<h3>Third slide label</h3>
<p>Praesent commodo cursus magna, vel scelerisque nisl consectetur.</p>
</div>
</div>
</div>
<!-- 轮播图控制 -->
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div>
示例二:自动轮播图
<div id="myCarousel" class="carousel slide">
<!-- 轮播图指示器 -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<!-- 轮播图内容 -->
<div class="carousel-inner">
<div class="item active">
<img src="img/carousel1.jpg" alt="">
</div>
<div class="item">
<img src="img/carousel2.jpg" alt="">
</div>
<div class="item">
<img src="img/carousel3.jpg" alt="">
</div>
</div>
<!-- 轮播图控制 -->
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div>
以上两个示例分别展示了轮播图带标题和自动轮播两种不同的效果。注意,轮播控制中的href
和data-slide
属性指明了轮播图的上一张和下一张,分别为prev
和next
,需要根据情况修改。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:利用BootStrap的Carousel.js实现轮播图动画效果 - Python技术站