Home Web Front-end JS Tutorial Bootstrap must learn the carousel plug-in every day_javascript skills

Bootstrap must learn the carousel plug-in every day_javascript skills

May 16, 2016 pm 03:03 PM

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/imghw/default1.png"  data-src="/media/uploads/2014/07/slide1.png"  class="lazy" alt="First slide">
 </div>
 <div class="item">
 <img src="/static/imghw/default1.png"  data-src="/media/uploads/2014/07/slide2.png"  class="lazy" alt="Second slide">
 </div>
 <div class="item">
 <img src="/static/imghw/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>
Copy after login

結果如下圖:

簡單的輪播(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/imghw/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/imghw/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/imghw/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>
Copy after login

結果如下圖:

輪播(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/imghw/default1.png"  data-src="/media/uploads/2014/07/slide1.png"  class="lazy" alt="First slide">
 </div>
 <div class="item">
 <img src="/static/imghw/default1.png"  data-src="/media/uploads/2014/07/slide2.png"  class="lazy" alt="Second slide">
 </div>
 <div class="item">
 <img src="/static/imghw/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>
Copy after login

結果如下圖:

輪播(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/imghw/default1.png"  data-src="/media/uploads/2014/07/slide1.png"  class="lazy" alt="First slide">
 </div>
 <div class="item">
 <img src="/static/imghw/default1.png"  data-src="/media/uploads/2014/07/slide2.png"  class="lazy" alt="Second slide">
 </div>
 <div class="item">
 <img src="/static/imghw/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>
Copy after login

結果如下圖:

輪播(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/imghw/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/imghw/default1.png"  data-src="img/slide2.png"  class="lazy" alt="第二张">
 </div>
 <div class="item">
 <img src="/static/imghw/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>
Copy after login

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,
});

Copy after login

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

//点击按钮执行

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

Copy after login

事件

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

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

Copy after login

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

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

Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

See all articles