Bootstrap圖片輪播組件使用實(shí)例解析
使用Bootstrap來(lái)編寫圖片輪播組件Carousel,則能夠節(jié)約很多時(shí)間,圖片輪播組件是一個(gè)在網(wǎng)頁(yè)中很常見(jiàn)的技術(shù),但是如果直接編寫的話,需要很長(zhǎng)的JavaScript編碼,同時(shí)也不好控制大小。
同時(shí)說(shuō)一下,Carousel這個(gè)詞的本義是回旋木馬。
一、基本目標(biāo)
在網(wǎng)頁(yè)編寫多張圖片的輪播組件Carousel,鼠標(biāo)放在上面自帶懸停效果,并且在每張圖片下面配有圖片說(shuō)明。
由于筆者的電腦視頻錄制軟件比較渣,也覺(jué)得沒(méi)必要畫太多時(shí)間在這上面,覺(jué)得只要能說(shuō)明問(wèn)題就行,所以下面的GIF失色比較嚴(yán)重,但是基本的效果還算是展示出來(lái)了。
這個(gè)Bootstrap的圖片輪播組件Carousel,不兼容IE6與7,需要IE6支持的話,要去這個(gè)網(wǎng)站中下載Bootstrap的IE6組件支持(點(diǎn)擊打開(kāi)鏈接)。同時(shí),在Google Chrome中圖片文件說(shuō)明會(huì)滲有一點(diǎn)小黑色,不過(guò)不影響瀏覽:

在不同瀏覽器中的展示情況是不同的。IE8的話是這樣的效果:

二、基本思想
見(jiàn)下圖網(wǎng)頁(yè)布局:

三、制作過(guò)程
1、同之前《Bootstrap編寫一個(gè)在當(dāng)前網(wǎng)頁(yè)彈出可關(guān)閉的對(duì)話框 非彈窗》的第一步(點(diǎn)擊打開(kāi)鏈接)
因?yàn)樾枰褂肂ootstrap,所以先在官網(wǎng)(點(diǎn)擊打開(kāi)鏈接)下載組件即可,用于生產(chǎn)環(huán)境的Bootstrap版本,Bootstrap3對(duì)2并不兼容,建議直接根據(jù)其開(kāi)發(fā)文檔使用Bootstrap3。本文也是根據(jù)Bootstrap3制作。同時(shí),Bootstrap3所提供的JavaScript效果需要到j(luò)Query1.11(點(diǎn)擊打開(kāi)鏈接)支持,可以到j(luò)Query官網(wǎng)中下載兼容舊瀏覽器IE6的jQuery1.11,而不是不兼容舊瀏覽器IE6的jQuery2。下載完之后,配置好站點(diǎn)目錄。把Bootstrap3直接解壓到站點(diǎn)目錄,而把jquery-1.11.1.js放到j(luò)s目錄,也就是與bootstrap.js同一目錄,站點(diǎn)文件夾的結(jié)構(gòu)大致如下:

2、以下是網(wǎng)頁(yè)的全代碼,下面一部分一部分進(jìn)行說(shuō)明:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no">
<link href="css/bootstrap.css" rel="stylesheet" media="screen">
<script type="text/javascript" src="js/jquery-1.11.1.js"></script>
<script type="text/javascript" src="js/bootstrap.js"></script>
<title>圖片輪播Carousel</title>
</head>
<body>
<div class="container">
<div class="page-header">
<h1>
圖片輪播Carousel
</h1>
</div>
<div style="width: 640px; height: 480px; margin-right: auto; margin-left: auto;">
<div id="carousel" class="carousel slide" data-ride="carousel" data-interval="1000">
<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"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>
<div class="carousel-inner" role="listbox">
<div class="item active">
<a href="images/img0.jpg"><img src="images/img0.jpg" alt="img0"></a>
<div class="carousel-caption">
<h3>
img0
</h3>
<p>
我是img0的圖片說(shuō)明
</p>
</div>
</div>
<div class="item">
<a href="images/img10.jpg"><img src="images/img10.jpg" alt="img10"></a>
<div class="carousel-caption">
<h3>
img10
</h3>
<p>
我是img10的圖片說(shuō)明
</p>
</div>
</div>
<div class="item">
<a href="images/img2.jpg"><img src="images/img2.jpg" alt="img2"></a>
<div class="carousel-caption">
<h3>
img2
</h3>
<p>
我是img2的圖片說(shuō)明
</p>
</div>
</div>
</div>
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span> </a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span> </a>
</div>
</div>
</div>
</body>
</html>
(1)<head>部分
<head> <!--聲明網(wǎng)頁(yè)編碼,自動(dòng)適應(yīng)瀏覽器的尺寸,要使用bootstrap的css,需要jquery支持,要使用bootstrap的js,標(biāo)題--> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no"> <link href="css/bootstrap.css" rel="stylesheet" media="screen"> <script type="text/javascript" src="js/jquery-1.11.1.js"></script> <script type="text/javascript" src="js/bootstrap.js"></script> <title>圖片輪播Carousel</title> </head>
(2)<body>部分
先聲明一個(gè)容器container,這個(gè)容器能使網(wǎng)頁(yè)的所有元素自動(dòng)歸于網(wǎng)頁(yè)中央,之后在這個(gè)容器中編寫元素。
首先編寫頁(yè)頭,聲明一個(gè)頁(yè)頭,之后其里面寫入一段文本。
<div class="page-header"> <h1> 圖片輪播Carousel </h1> </div>
之后定義一個(gè)未命名的圖層div,主要是用來(lái)規(guī)范圖片輪播組件用的。bootstrap的圖片輪播組件大小不能對(duì)其里面的元素,加入width與height參數(shù)進(jìn)行規(guī)定。這樣圖片輪播組件會(huì)失真。同時(shí)這個(gè)組件要居中,必須在div的style屬性中使用margin-right: auto; margin-left: auto;來(lái)約束,額外加入align="center"是根本一點(diǎn)效果都沒(méi)有。
最后是圖片組件各部分的詳細(xì)說(shuō)明:
<div style="width: 640px; height: 480px; margin-right: auto; margin-left: auto;">
<!--圖片輪播組件的名稱為carousel,data-ride元素是bootstrap要求存在的,data-interval的值是每隔1000毫秒,也就是1秒換一張圖片,此值太小組件會(huì)失真-->
<div id="carousel" class="carousel slide" data-ride="carousel" data-interval="1000">
<!--這里定義有幾張圖片,如果再多一張圖片就再下面多加一項(xiàng),data-slide-to的值加一,首張圖片也就是第0張圖片必須要有class="active"否則組件無(wú)法工作-->
<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"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>
<div class="carousel-inner" role="listbox">
<!--以下是各張的圖片的詳細(xì)編輯,首張圖片的class值必須為item active,余下的皆為item-->
<div class="item active">
<!--意為點(diǎn)擊img0.jpg這張圖片就打開(kāi)img0.jpg的超級(jí)鏈接,如果不需要超級(jí)鏈接,則去掉<a>標(biāo)簽-->
<a href="images/img0.jpg"><img src="images/img0.jpg" alt="img0"></a>
<div class="carousel-caption">
<!--圖片下的文字說(shuō)明-->
<h3>
img0
</h3>
<p>
我是img0的圖片說(shuō)明
</p>
</div>
</div>
<div class="item">
<a href="images/img10.jpg"><img src="images/img10.jpg" alt="img10"></a>
<div class="carousel-caption">
<h3>
img10
</h3>
<p>
我是img10的圖片說(shuō)明
</p>
</div>
</div>
<div class="item">
<a href="images/img2.jpg"><img src="images/img2.jpg" alt="img2"></a>
<div class="carousel-caption">
<h3>
img2
</h3>
<p>
我是img2的圖片說(shuō)明
</p>
</div>
</div>
</div>
<!--這里是組件中向左想右的兩個(gè)按鈕,固定存在的框架代碼-->
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span> </a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span> </a>
</div>
</div>
如果大家還想深入學(xué)習(xí),可以點(diǎn)擊這里進(jìn)行學(xué)習(xí),再為大家附3個(gè)精彩的專題:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript+jQuery實(shí)現(xiàn)360開(kāi)機(jī)時(shí)間顯示效果
這篇文章主要介紹了javascript+jQuery實(shí)現(xiàn)360開(kāi)機(jī)時(shí)間顯示效果,在文中給大家提到了js實(shí)現(xiàn)時(shí)間倒計(jì)時(shí)的代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11
ClearTimeout消除閃動(dòng)實(shí)例代碼
本文給大家介紹ClearTimeout消除閃動(dòng)相關(guān)知識(shí),本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-02-02
IE6/7 and IE8/9/10(IE7模式)依次隱藏具有absolute或relative的父元素和子元素后再顯示
多數(shù)情況下隱藏(設(shè)置display:none)一個(gè)元素,無(wú)需依次將其內(nèi)的所有子元素都隱藏。非要這么做,有時(shí)會(huì)碰到意想不到的bug。2011-07-07
js動(dòng)態(tài)刪除div元素基本思路及實(shí)現(xiàn)代碼
這篇文章主要介紹了js動(dòng)態(tài)刪除div元素基本思路及實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-05-05
TypeScript Type Innference(類型判斷)
TypeScript 是微軟開(kāi)發(fā)的 JavaScript 的超集,TypeScript兼容JavaScript,可以載入JavaScript代碼然后運(yùn)行。接下來(lái)通過(guò)本文給大家介紹TypeScript Type Innference(類型判斷)的相關(guān)知識(shí),需要的朋友參考下2016-03-03
javascript 主動(dòng)派發(fā)事件總結(jié)
有時(shí)需要模仿用戶的一些動(dòng)作(鼠標(biāo)/鍵盤操作),最常見(jiàn)的莫過(guò)于鼠標(biāo)點(diǎn)擊。一一列舉2011-08-08
JavaScript箭頭函數(shù)與普通函數(shù)的區(qū)別示例詳解
這篇文章主要為大家介紹了JavaScript箭頭函數(shù)與普通函數(shù)的區(qū)別示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
純javascript實(shí)現(xiàn)分頁(yè)(兩種方法)
有的時(shí)候頁(yè)面需要很多不同的表組成的數(shù)據(jù),該怎么分頁(yè)呢?使用數(shù)據(jù)庫(kù)分頁(yè)很簡(jiǎn)單,那么如何使用js實(shí)現(xiàn)分頁(yè)呢?接下來(lái),小編幫大家解決這個(gè)問(wèn)題,需要的朋友一起來(lái)學(xué)習(xí)吧2015-08-08

