Bootstrap每天必學(xué)之折疊
本文主要來學(xué)習(xí)一下JavaScript插件--折疊。
1、過渡效果
關(guān)于過渡效果
對(duì)于簡(jiǎn)單的過渡效果,只需將transition.js和其它JS文件一起引入即可。如果你使用的是編譯(或壓縮)好的bootstrap.js文件,就無(wú)需再單獨(dú)將其引入了。
What's inside
Transition.js是針對(duì) is a basic helper for transitionEnd事件的一個(gè)基本助手工具,也是對(duì)CSS過渡效果的模擬。它被其它插件用來檢測(cè)當(dāng)前瀏覽器對(duì)CSS過渡效果是否支持。
2、折疊
對(duì)為支持折疊功能的組件,例如accordions和導(dǎo)航,賦予基本樣式和靈活的支持。
插件依賴
折疊插件依賴過渡效果插件。
案例
使用折疊插件,通過擴(kuò)展panel組件從而構(gòu)建了一個(gè)簡(jiǎn)單的accordion組件。
先來看一下效果。
接下來看一下代碼的實(shí)現(xiàn)。
<div class="container" style="margin-top:140px;"> <div class="panel-group" id="accordion"> <div class="panel panel-default"> <div class="panel-heading"> <h4 class="panel-title"> <a data-toggle="collapse" data-toggle="collapse" data-parent="#accordion" href="#collapseOne"> Collapsible Group Item </a> </h4> </div> <div id="collapseOne" class="panel-collapse collapse in"> <div class="panel-body"> Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. </div> </div> </div> <div class="panel panel-default"> <div class="panel-heading"> <h4 class="panel-title"> <a data-toggle="collapse" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo"> Collapsible Group Item #2 </a> </h4> </div> <div id="collapseTwo" class="panel-collapse collapse"> <div class="panel-body"> Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. </div> </div> </div> <div class="panel panel-default"> <div class="panel-heading"> <h4 class="panel-title"> <a data-toggle="collapse" data-toggle="collapse" data-parent="#accordion" href="#collapseThree"> Collapsible Group Item #3 </a> </h4> </div> <div id="collapseThree" class="panel-collapse collapse"> <div class="panel-body"> Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. </div> </div> </div> </div> </div>
第一步:首先最外面那層panel-group這層下面包括幾個(gè)小組。
第二步:看一下幾個(gè)簡(jiǎn)單的組
<div class="panel panel-default"> <div class="panel-heading"> <h4 class="panel-title"> <a data-toggle="collapse" data-toggle="collapse" data-parent="#accordion" href="#collapseOne"> Collapsible Group Item #1 </a> </h4> </div> <div id="collapseOne" class="panel-collapse collapse in"> <div class="panel-body"> </div> </div> </div>
通過代碼也比較清楚的可以看出一個(gè)panel的結(jié)構(gòu)。
panel-header和pandl-body,然后panel-header里面可以包含標(biāo)題,鏈接。通過鏈接和panel-body相連。
第三步:你可以發(fā)現(xiàn)在panel-group中有一個(gè)id="accordion",然后下面每個(gè)標(biāo)題下鏈接中有個(gè)data-parent="#accordion"。
如果去掉的話,那么效果就是點(diǎn)擊其他鏈接后,原來的panel并不會(huì)再縮起來了。
你可以通過另一個(gè)方式來展示折疊的效果。
<div class="container" style="margin-top:140px;"> <button type="button" class="btn btn-danger" data-toggle="collapse" data-target="#demo"> simple collapsible </button> <div id="demo" class="collapse in">Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.</div>
用法
折疊插件通過幾個(gè)簡(jiǎn)單的類來控制樣式
.collapse 隱藏內(nèi)容
.collapse in 顯示內(nèi)容
.collapsing。 It is added when the transition starts, and removed when it finishes意思大概可能就是折疊被添加后過渡效果就有了,然后如果被移除了它就結(jié)束了。
通過data屬性
僅僅通過向頁(yè)面元素添加data-toggle="collapse" 和data-target就可以為其賦予控制可折疊頁(yè)面元素的能力。data-target屬性接受一個(gè)CSS選擇器作為其控制對(duì)象。請(qǐng)確保為可折疊頁(yè)面元素添加collapse class。如果你希望可折疊頁(yè)面元素的默認(rèn)狀態(tài)是展開的,請(qǐng)?zhí)砑觟n class。
為了給一組可折疊頁(yè)面元素添加控制器,添加data-parent="#selector"即可。請(qǐng)參考上面的例子即可。
通過JavaScript
<button type="button" class="btn btn-danger" onClick="Open()">打開</button> <button type="button" class="btn btn-danger" onClick="Hide()">折疊</button> <div id="demo" class="collapse in">Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.</div> <div class="panel-group" id="accordion" style="margin-top:240px;">
<script type="text/javascript"> $(function(){ $("#demo").collapse({ toggle: false }) }) function Open(){ $("#demo").collapse("show"); } function Hide(){ $("#demo").collapse("hide"); } </script>
來看一下上面的效果
方法
賦予頁(yè)面元素可折疊功能。接受一個(gè)可選的object作為參數(shù)。
$("#demo").collapse({toggle: false})
這樣元素在初始化的時(shí)候會(huì)是展開的。
1.collapse('toggle')展示或隱藏一個(gè)可折疊的頁(yè)面元素。
2.collapse('show')展示一個(gè)可折疊頁(yè)面元素。
3.collapse('hide')隱藏一個(gè)可折疊頁(yè)面元素。
事件
Bootstrap中的折疊插件對(duì)外暴露了一組可以監(jiān)聽的事件。
這樣就為元素綁定了隱藏時(shí)的事件。
如果大家還想深入學(xué)習(xí),可以點(diǎn)擊這里進(jìn)行學(xué)習(xí),再為大家附兩個(gè)精彩的專題:Bootstrap學(xué)習(xí)教程 Bootstrap實(shí)戰(zhàn)教程
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
- Bootstrap 手風(fēng)琴菜單的實(shí)現(xiàn)代碼
- bootstrap手風(fēng)琴制作方法詳解
- 簡(jiǎn)潔實(shí)用的BootStrap jQuery手風(fēng)琴插件
- Angular.js與Bootstrap相結(jié)合實(shí)現(xiàn)手風(fēng)琴菜單代碼
- 全面解析Bootstrap手風(fēng)琴效果
- bootstrap折疊調(diào)用collapse()后data-parent不生效的快速解決辦法
- Bootstrap基本插件學(xué)習(xí)筆記之折疊(22)
- Bootstrap每天必學(xué)之折疊(Collapse)插件
- Bootstrap創(chuàng)建可折疊的組件
- bootstrap手風(fēng)琴折疊示例代碼分享
相關(guān)文章
JS IOS/iPhone的Safari瀏覽器不兼容Javascript中的Date()問題如何解決
這篇文章主要介紹了JS IOS/iPhone的Safari瀏覽器不兼容Javascript中的Date()問題的解決方案,非常不錯(cuò),感興趣的朋友參考下吧2016-11-11實(shí)例講解DataTables固定表格寬度(設(shè)置橫向滾動(dòng)條)
下面小編就為大家?guī)硪黄獙?shí)例講解DataTables固定表格寬度(設(shè)置橫向滾動(dòng)條)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07小程序?qū)崿F(xiàn)頁(yè)面多級(jí)來回切換的示例代碼
這篇文章主要為大家詳細(xì)介紹了小程序如何頁(yè)面多級(jí)來回切換支持滑動(dòng)和點(diǎn)擊操作,文中的實(shí)現(xiàn)步驟講解詳細(xì),快跟隨小編一起動(dòng)手嘗試一下吧2022-07-07微信小程序?qū)崿F(xiàn)默認(rèn)第一個(gè)選中變色效果
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)默認(rèn)第一個(gè)選中變色效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07微信小程序?qū)崿F(xiàn)搜索指定景點(diǎn)周邊美食、酒店
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)搜索指定景點(diǎn)周邊美食、酒店的功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05js unicode 編碼解析關(guān)于數(shù)據(jù)轉(zhuǎn)換為中文的兩種方法
這篇文章主要介紹了js unicode 編碼解析關(guān)于數(shù)據(jù)轉(zhuǎn)換為中文的兩種方法,需要的朋友可以參考下2014-04-04微信小程序教程系列之頁(yè)面跳轉(zhuǎn)和參數(shù)傳遞(6)
這篇文章主要為大家詳細(xì)介紹了微信小程序教程系列之頁(yè)面跳轉(zhuǎn)和參數(shù)傳遞,微信小程序提供了3種頁(yè)面跳轉(zhuǎn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04