Bootstrap開關(guān)(switch)控件學(xué)習(xí)筆記分享
bootstrap-switch插件是一個(gè)針對(duì)Bootstrap實(shí)現(xiàn)的開關(guān)(switch)按鈕控件,可以支持尺寸、顏色等屬性的自定義。開關(guān)式按鈕在國(guó)內(nèi)網(wǎng)站上使用的并不是很多,Bootstrap 的應(yīng)用在國(guó)外非常流行,不知道是我們不喜歡還是使用它很麻煩很難適合網(wǎng)站來(lái)使用。但這種開頭式按鈕在手機(jī)等移動(dòng)設(shè)備上的應(yīng)用是最廣泛的,屏幕的特性促使它更好的發(fā)展。
功能說(shuō)明:
介紹chekbox與radio的兩個(gè)表單的簡(jiǎn)單使用,其他更多的效果與功能可以瀏覽demo,點(diǎn)擊按鈕以滑動(dòng)的方式進(jìn)行on/off切換。
使用說(shuō)明:
1.引入CSS與JS文件
<link rel="stylesheet" href="static/stylesheets/bootstrap-switch.css" /> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="static/js/bootstrap-switch.js"></script>
2.html內(nèi)容添加
<div class="make-switch" data-on="info" data-off="success"> <input type="checkbox" checked> </div> <div class="make-switch" data-on="success" data-off="warning"> <input type="checkbox" checked> </div> <div class="make-switch" data-on="warning" data-off="danger"> <input type="checkbox" checked> </div> <div class="make-switch" data-on="danger" data-off="default"> <input type="checkbox" checked> </div> <div class="make-switch" data-on="default" data-off="primary"> <input type="checkbox" checked> </div>
1). div完全是為了給checkbox添加式樣。
2). input就很簡(jiǎn)單了,就是普通的標(biāo)簽。
3).make-switch:對(duì)使用插件的checkbox添加CSS樣式。
4).data-on:為on狀態(tài)時(shí)的CSS樣式。
5).data-off:為off狀態(tài)時(shí)的CSS樣式。
3.radio單選框的使用:
<label for="option11">Option 1</label> <div class="make-switch radio2"> <input id="option11" type="radio" name="radio2" value="option11"> </div> <label for="option12">Option 2</label> <div class="make-switch radio2"> <input id="option12" type="radio" name="radio2" value="option12" checked="checked"> </div> <label for="option13">Option 3</label> <div class="make-switch radio2"> <input id="option13" type="radio" name="radio2" value="option13"> </div>
radio單選框的使用方法是相同的,因?yàn)閱芜x框先中其中一個(gè)是其他狀態(tài)都要改變,所以要添加相應(yīng)的JS代碼;
<script> $('.radio2').on('switch-change', function () { $('.radio2').bootstrapSwitch('toggleRadioStateAllowUncheck', true); }); </script>
4.啟動(dòng)
$("div[class='switch']").each(function() { $this = $(this); var onColor = $this.attr("onColor"); var offColor = $this.attr("offColor"); var onText = $this.attr("onText"); var offText = $this.attr("offText"); var labelText = $this.attr("labelText"); var $switch_input = $(" :only-child", $this); $switch_input.bootstrapSwitch({ onColor : onColor, offColor : offColor, onText : onText, offText : offText, labelText : labelText }); });
1). 通過jquery獲取所有的switch div,從而獲取其屬性onColor 、offColor 等等
2). 緊接著,獲取div包含的子元素input。
3). 通過bootstrapSwitch方法對(duì)input進(jìn)行加載。
這樣我們就以簡(jiǎn)單的實(shí)現(xiàn)我們想要的開關(guān)按鈕效果了。
如果大家還想深入學(xué)習(xí),可以點(diǎn)擊這里進(jìn)行學(xué)習(xí),再為大家附一個(gè)精彩的專題:Bootstrap學(xué)習(xí)教程
以上就是關(guān)于Bootstrap開關(guān)(switch)控件的使用方法,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
5個(gè)書寫JavaScript代碼的壞習(xí)慣,看看你中槍了沒?
這篇文章主要介紹了5個(gè)書寫JavaScript代碼的壞習(xí)慣,看看你中槍了沒?,本文指出了你沒有使用命名空間、變量定義的東一個(gè)西一個(gè)、Javascript的變量范圍、Javascript的面向?qū)ο?、new關(guān)鍵字等問題,需要的朋友可以參考下2014-11-11js實(shí)現(xiàn)點(diǎn)贊按鈕功能的實(shí)例代碼
這篇文章主要介紹了js實(shí)現(xiàn)點(diǎn)贊按鈕功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的工作或?qū)W習(xí)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03javascript 獲取iframe里頁(yè)面中元素值的方法
本篇文章主要是對(duì)javascript獲取iframe里頁(yè)面中元素值的方法進(jìn)行了介紹,需要的朋友可以過來(lái)參考下,希望對(duì)大家有所幫助2014-02-02使用ECharts實(shí)現(xiàn)狀態(tài)區(qū)間圖
這篇文章主要為大家詳細(xì)介紹了使用ECharts實(shí)現(xiàn)狀態(tài)區(qū)間圖,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10十個(gè)你必須要會(huì)的TypeScript技巧分享
學(xué)習(xí)Typescript通常是一個(gè)重新發(fā)現(xiàn)的過程。這篇文章為大家整理了十個(gè)你必須要會(huì)的TypeScript技巧,希望對(duì)大家學(xué)習(xí)TypeScript有所幫助2023-06-06setinterval()與clearInterval()JS函數(shù)的調(diào)用方法
這篇文章主要介紹了setinterval()與clearInterval()JS函數(shù)的調(diào)用方法,實(shí)例分析了setinterval()與clearInterval()的語(yǔ)法結(jié)構(gòu)及使用技巧,需要的朋友可以參考下2015-01-01