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">‹</a> <a class="carousel-control right" href="#myCarousel" data-slide="next">›</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">‹</a> <a class="carousel-control right" href="#myCarousel" data-slide="next">›</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">‹</a> <a class="carousel-control right" href="#myCarousel" data-slide="next">›</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">‹</a> <a class="carousel-control right" href="#myCarousel" data-slide="next">›</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">‹</a> <a href="#myCarousel" data-slide="next" class="carousel-controlright">›</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)插件有所帮助。

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was

jQuery is a great JavaScript framework. However, as with any library, sometimes it’s necessary to get under the hood to discover what’s going on. Perhaps it’s because you’re tracing a bug or are just curious about how jQuery achieves a particular UI

This post compiles helpful cheat sheets, reference guides, quick recipes, and code snippets for Android, Blackberry, and iPhone app development. No developer should be without them! Touch Gesture Reference Guide (PDF) A valuable resource for desig


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools
