search
HomeWeb Front-endJS TutorialBootstrap must learn the carousel plug-in every day_javascript skills

Bootstrap 輪播插件是一種靈活的響應式的向網站添加滑桿的方式。除此之外,內容也是足夠靈活的,可以是影像、內嵌框架、影片或其他您想要放置的任何類型的內容。

如果您想要單獨引用該外掛程式的功能,那麼您需要引用 carousel.js。或者,正如 Bootstrap 外掛程式概覽 一章中所提到,您可以引用 bootstrap.js 或壓縮版的 bootstrap.min.js。

一、實例
下面是一個簡單的幻燈片,使用 Bootstrap 輪播(Carousel)插件顯示了一個循環播放元素的通用組件。為了實現輪播,您只需要添加帶有該標記的程式碼。不需要使用 data 屬性,只需要簡單的基於 class 的開發即可。

<!DOCTYPE html>
<html>
<head>
 <title>Bootstrap 实例 - 简单的轮播(Carousel)插件</title>
 <link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet">
 <script src="/scripts/jquery.min.js"></script>
 <script src="/bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
 
<div id="myCarousel" class="carousel slide">
 <!-- 轮播(Carousel)指标 -->
 <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> 
 <!-- 轮播(Carousel)项目 -->
 <div class="carousel-inner">
 <div class="item active">
 <img src="/static/imghwm/default1.png"  data-src="/media/uploads/2014/07/slide1.png"  class="lazy" alt="First slide">
 </div>
 <div class="item">
 <img src="/static/imghwm/default1.png"  data-src="/media/uploads/2014/07/slide2.png"  class="lazy" alt="Second slide">
 </div>
 <div class="item">
 <img src="/static/imghwm/default1.png"  data-src="/media/uploads/2014/07/slide3.png"  class="lazy" alt="Third slide">
 </div>
 </div>
 <!-- 轮播(Carousel)导航 -->
 <a class="carousel-control left" href="#myCarousel"
 data-slide="prev">&lsaquo;</a>
 <a class="carousel-control right" href="#myCarousel"
 data-slide="next">&rsaquo;</a>
</div>
 
</body>
</html>

結果如下圖:

簡單的輪播(Carousel)外掛程式

二、可選的標題
您可以透過 .item 內的 .carousel-caption 元素為投影片新增標題。只需要在該處放置任何可選的 HTML 即可,它會自動對齊並格式化。下面的實例示範了這一點:

<!DOCTYPE html>
<html>
<head>
 <title>Bootstrap 实例 - 轮播(Carousel)插件的标题</title>
 <link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet">
 <script src="/scripts/jquery.min.js"></script>
 <script src="/bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
 
<div id="myCarousel" class="carousel slide">
 <!-- 轮播(Carousel)指标 -->
 <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> 
 <!-- 轮播(Carousel)项目 -->
 <div class="carousel-inner">
 <div class="item active">
 <img src="/static/imghwm/default1.png"  data-src="/media/uploads/2014/07/slide1.png"  class="lazy" alt="First slide">
 <div class="carousel-caption">标题 1</div>
 </div>
 <div class="item">
 <img src="/static/imghwm/default1.png"  data-src="/media/uploads/2014/07/slide2.png"  class="lazy" alt="Second slide">
 <div class="carousel-caption">标题 2</div>
 </div>
 <div class="item">
 <img src="/static/imghwm/default1.png"  data-src="/media/uploads/2014/07/slide3.png"  class="lazy" alt="Third slide">
 <div class="carousel-caption">标题 3</div>
 </div>
 </div>
 <!-- 轮播(Carousel)导航 -->
 <a class="carousel-control left" href="#myCarousel"
 data-slide="prev">&lsaquo;</a>
 <a class="carousel-control right" href="#myCarousel"
 data-slide="next">&rsaquo;</a>
</div>
 
 
</body>
</html>

結果如下圖:

輪播(Carousel)外掛程式的標題

三、用法

透過 data 屬性:使用 data 屬性可以輕易控制輪播(Carousel)的位置。
屬性 data-slide 接受關鍵字 prev 或 next,用來改變投影片相對於目前位置的位置。
使用 data-slide-to 來向輪播床底一個原始滑動索引,data-slide-to="2" 將把滑桿移到一個特定的索引,索引從 0 開始計數。
data-ride="carousel" 屬性用來標記輪播在頁面載入時就開始動畫播放。

透過 JavaScript:輪播(Carousel)可透過 JavaScript 手動調用,如下所示:
$('.carousel').carousel()

四、選項
有一些選項是透過 data 屬性或 JavaScript 來傳遞的。下表列出了這些選項:

五、方法
以下是一些輪播(Carousel)插件中有用的方法:

六、實例
下面的實例示範了方法的用法:

<!DOCTYPE html>
<html>
<head>
 <title>Bootstrap 实例 - 轮播(Carousel)插件方法</title>
 <link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet">
 <script src="/scripts/jquery.min.js"></script>
 <script src="/bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
 
<div id="myCarousel" class="carousel slide">
 <!-- 轮播(Carousel)指标 -->
 <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> 
 <!-- 轮播(Carousel)项目 -->
 <div class="carousel-inner">
 <div class="item active">
 <img src="/static/imghwm/default1.png"  data-src="/media/uploads/2014/07/slide1.png"  class="lazy" alt="First slide">
 </div>
 <div class="item">
 <img src="/static/imghwm/default1.png"  data-src="/media/uploads/2014/07/slide2.png"  class="lazy" alt="Second slide">
 </div>
 <div class="item">
 <img src="/static/imghwm/default1.png"  data-src="/media/uploads/2014/07/slide3.png"  class="lazy" alt="Third slide">
 </div>
 </div>
 <!-- 轮播(Carousel)导航 -->
 <a class="carousel-control left" href="#myCarousel"
 data-slide="prev">&lsaquo;</a>
 <a class="carousel-control right" href="#myCarousel"
 data-slide="next">&rsaquo;</a>
 <!-- 控制按钮 -->
 <div style="text-align:center;">
 <input type="button" class="btn start-slide" value="Start">
 <input type="button" class="btn pause-slide" value="Pause">
 <input type="button" class="btn prev-slide" value="Previous Slide">
 <input type="button" class="btn next-slide" value="Next Slide">
 <input type="button" class="btn slide-one" value="Slide 1">
 <input type="button" class="btn slide-two" value="Slide 2"> 
 <input type="button" class="btn slide-three" value="Slide 3">
 </div>
</div>
<script>
 $(function(){
 // 初始化轮播
 $(".start-slide").click(function(){
 $("#myCarousel").carousel('cycle');
 });
 // 停止轮播
 $(".pause-slide").click(function(){
 $("#myCarousel").carousel('pause');
 });
 // 循环轮播到上一个项目
 $(".prev-slide").click(function(){
 $("#myCarousel").carousel('prev');
 });
 // 循环轮播到下一个项目
 $(".next-slide").click(function(){
 $("#myCarousel").carousel('next');
 });
 // 循环轮播到某个特定的帧
 $(".slide-one").click(function(){
 $("#myCarousel").carousel(0);
 });
 $(".slide-two").click(function(){
 $("#myCarousel").carousel(1);
 });
 $(".slide-three").click(function(){
 $("#myCarousel").carousel(2);
 });
 });
</script>
 
 
</body>
</html>

結果如下圖:

輪播(Carousel)插件方法

七、事件

下表列出了輪播(Carousel)插件中要用到的事件。這些事件可在函數中當鉤子使用。

實例
下面的實例示範了事件的用法:

<!DOCTYPE html>
<html>
<head>
 <title>Bootstrap 实例 - 轮播(Carousel)插件事件</title>
 <link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet">
 <script src="/scripts/jquery.min.js"></script>
 <script src="/bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
 
<div id="myCarousel" class="carousel slide">
 <!-- 轮播(Carousel)指标 -->
 <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> 
 <!-- 轮播(Carousel)项目 -->
 <div class="carousel-inner">
 <div class="item active">
 <img src="/static/imghwm/default1.png"  data-src="/media/uploads/2014/07/slide1.png"  class="lazy" alt="First slide">
 </div>
 <div class="item">
 <img src="/static/imghwm/default1.png"  data-src="/media/uploads/2014/07/slide2.png"  class="lazy" alt="Second slide">
 </div>
 <div class="item">
 <img src="/static/imghwm/default1.png"  data-src="/media/uploads/2014/07/slide3.png"  class="lazy" alt="Third slide">
 </div>
 </div>
 <!-- 轮播(Carousel)导航 -->
 <a class="carousel-control left" href="#myCarousel"
 data-slide="prev">&lsaquo;</a>
 <a class="carousel-control right" href="#myCarousel"
 data-slide="next">&rsaquo;</a>
</div>
<script>
 $(function(){
 $('#myCarousel').on('slide.bs.carousel', function () {
 alert("当调用 slide 实例方法时立即触发该事件。");
 });
 });
</script>
 
</body>
</html>

結果如下圖:

輪播(Carousel)外掛事件

根據上面的教學自己製作的實例:

輪播插件就是將幾張同等大小的大圖,依照順序依序播放。

//基本实例。

<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="/static/imghwm/default1.png"  data-src="img/slide1.png"  class="lazy" alt="Bootstrap must learn the carousel plug-in every day_javascript skills">
 </div>
 <div class="item">
 <img src="/static/imghwm/default1.png"  data-src="img/slide2.png"  class="lazy" alt="第二张">
 </div>
 <div class="item">
 <img src="/static/imghwm/default1.png"  data-src="img/slide3.png"  class="lazy" alt="第三张">
 </div>
 </div>

 <a href="#myCarousel" data-slide="prev" class="carousel-controlleft">&lsaquo;</a>
 <a href="#myCarousel" data-slide="next" class="carousel-controlright">&rsaquo;</a>
</div>

data 属性解释:

1.data-slide 接受关键字 prev 或 next,用来改变幻灯片相对于当前位置的位置;
2.data-slide-to 来向轮播底部创建一个原始滑动索引, data-slide-to="2"将把滑动块移动到一个特定的索引,索引从 0 开始计数。
3.data-ride="carousel"属性用户标记轮播在页面加载时开始动画播放。

如果在 JavaScript 调用就直接使用键值对方法,并去掉 data-;
//设置自定义属性

$('#myCarousel').carousel({
 //设置自动播放/2 秒
 interval : 2000,
 //设置暂停按钮的事件
 pause : 'hover',
 //只播一次
 wrap : false,
});

轮播插件还提供了一些方法,如下:

//点击按钮执行

$('button').on('click', function() {
 //点击后,自动播放
 $('#myCarousel').carousel('cycle');
 //其他雷同
});

事件

$('#myCarousel').on('slide.bs.carousel', function() {
 alert('当调用 slide 实例方式时立即触发');
});

$('#myCarousel').on('slid.bs.carousel', function() {
 alert('当轮播完成一个幻灯片触发');
});

更多内容可以参考:Bootstrap学习教程

以上就是本文的全部内容,希望对大家学习Bootstrap轮播(Carousel)插件有所帮助。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C   and JavaScript: The Connection ExplainedC and JavaScript: The Connection ExplainedApr 23, 2025 am 12:07 AM

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools