Bootstrap幻燈片輪播圖支持觸屏左右手勢(shì)滑動(dòng)的實(shí)現(xiàn)方法
最近ytkah在學(xué)習(xí)用bootstrap搭建網(wǎng)站,Bootstrap能自適應(yīng)pc端和手機(jī)端,并且移動(dòng)設(shè)備優(yōu)先,適合現(xiàn)如今移動(dòng)營(yíng)銷。bootstrap是封裝好的框架,需要某些功能只需調(diào)用相應(yīng)的組件就可以,但有些還是沒加入,比如幻燈輪播觸屏左右滑動(dòng)手勢(shì)就不支持,大家用的設(shè)備基本是觸屏的了,能用滑動(dòng)交互在小屏幕上體驗(yàn)會(huì)更好,那么如何實(shí)現(xiàn)呢?一個(gè)比較簡(jiǎn)單的方法是增加一個(gè)滑動(dòng)手勢(shì)js插件:hammer.js,網(wǎng)上有很多cdn調(diào)用地址,像//cdn.bootcss.com/hammer.js/2.0.8/hammer.min.js,我們?cè)趆ead中加載一下然后再通過javascript把swipe功能調(diào)用出來就可以了。下面是幻燈片的原始html代碼
<div data-ride="carousel" class="carousel slide" id="carousel-example-generic"> <ol class="carousel-indicators"> <li class="" data-slide-to="0" data-target="#carousel-example-generic"></li> <li data-slide-to="1" data-target="#carousel-example-generic" class="active"></li> <li data-slide-to="2" data-target="#carousel-example-generic" class=""></li> </ol> <div role="listbox" class="carousel-inner"> <div class="item"> <img alt="First slide" src="http://ibootstrap-file.b0.upaiyun.com/lorempixel.com/1600/500/sports/1/default.jpg" data-holder-rendered="true"> </div> <div class="item active"> <img alt="Second slide [1140x500]" src="http://ibootstrap-file.b0.upaiyun.com/lorempixel.com/1600/500/sports/2/default.jpg" data-holder-rendered="true"> </div> <div class="item"> <img alt="Third slide [1140x500]" src="http://ibootstrap-file.b0.upaiyun.com/lorempixel.com/1600/500/sports/3/default.jpg" data-holder-rendered="true"> </div> </div> <a data-slide="prev" role="button" href="#carousel-example-generic" class="left carousel-control"> <span aria-hidden="true" class="glyphicon glyphicon-chevron-left"></span> <span class="sr-only">Previous</span> </a> <a data-slide="next" role="button" href="#carousel-example-generic" class="right carousel-control"> <span aria-hidden="true" class="glyphicon glyphicon-chevron-right"></span> <span class="sr-only">Next</span> </a> </div>
關(guān)鍵的步驟來了,我們需要寫一個(gè)javascript命令調(diào)用hammer.js中的swipe功能
<script>
$(function(){
var myElement= document.getElementById('carousel-example-generic')
var hm=new Hammer(myElement);
hm.on("swipeleft",function(){
$('#carousel-example-generic').carousel('next')
})
hm.on("swiperight",function(){
$('#carousel-example-generic').carousel('prev')
})
})
</script>
div的id一定要對(duì)應(yīng),上面是carousel-example-generic,javascript中也要這個(gè),否則不能實(shí)現(xiàn)。
需要注意的是,jquery版本最好是1.9版本的jquery-1.9.1.min.js,否則可能在電腦上可以實(shí)現(xiàn)手勢(shì)滑動(dòng),而在手機(jī)上無法觸摸滑動(dòng)
javascript命令這個(gè)是關(guān)鍵,不會(huì)寫不會(huì)改就不好玩了。做個(gè)標(biāo)記,方便日后查詢
以上所述是小編給大家介紹的Bootstrap幻燈片輪播圖支持觸屏左右手勢(shì)滑動(dòng)的實(shí)現(xiàn)方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- BootStrap實(shí)現(xiàn)手機(jī)端輪播圖左右滑動(dòng)事件
- Bootstrap每天必學(xué)之響應(yīng)式導(dǎo)航、輪播圖
- Bootstrap開發(fā)實(shí)戰(zhàn)之響應(yīng)式輪播圖
- 使用BootStrap建立響應(yīng)式網(wǎng)頁——通欄輪播圖(carousel)
- 利用BootStrap的Carousel.js實(shí)現(xiàn)輪播圖動(dòng)畫效果
- BootStrap實(shí)現(xiàn)輪播圖效果(收藏)
- 在bootstrap中實(shí)現(xiàn)輪播圖實(shí)例代碼
- Bootstrap實(shí)現(xiàn)基于carousel.js框架的輪播圖效果
- bootstrap輪播圖示例代碼分享
- bootstrap實(shí)現(xiàn)輪播圖效果
相關(guān)文章
javascript 冒泡排序 正序和倒序?qū)崿F(xiàn)代碼
javascript 冒泡排序 正序和倒序?qū)崿F(xiàn)代碼,需要的朋友可以參考下。2010-12-12
JavaScript實(shí)現(xiàn)氣球打字的小游戲
這篇文章主要介紹了JavaScript實(shí)現(xiàn)氣球打字的小游戲,下面文章的思路來源于打字游戲,具體實(shí)現(xiàn)過程,需要的朋友可以參考一下,希望對(duì)你的學(xué)習(xí)有所幫助2022-04-04
BootStrap中Datetimepicker和uploadify插件應(yīng)用實(shí)例小結(jié)
這篇文章主要介紹了BootStrap中Datetimepicker和uploadify插件應(yīng)用實(shí)例小結(jié)的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-05-05
Bootstrap源碼解讀媒體對(duì)象、列表組和面板(10)
這篇文章主要源碼解讀了Bootstrap媒體對(duì)象、列表組和面板,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
JavaScript實(shí)現(xiàn)省市聯(lián)動(dòng)效果
這篇文章主要介紹了JavaScript實(shí)現(xiàn)省市聯(lián)動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
淺析Javascript中bind()方法的使用與實(shí)現(xiàn)
下面小編就為大家?guī)硪黄獪\析Javascript中bind()方法的使用與實(shí)現(xiàn)。小編覺得挺2016-05-05
javascript 表格內(nèi)容排序 簡(jiǎn)單操作示例代碼
本文為大家詳細(xì)介紹下javascript實(shí)現(xiàn)表格內(nèi)容排序,喜歡的朋友可以參考下2014-01-01
Js參數(shù)RSA加密傳輸之jsencrypt.js的使用
這篇文章主要介紹了Js參數(shù)RSA加密傳輸之jsencrypt.js的使用,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
javascript正則表達(dá)式中的replace方法詳解
replace方法的語法是:stringObj.replace(rgExp, replaceText) 其中stringObj是字符串(string),reExp可以是正則表達(dá)式對(duì)象(RegExp)也可以是字符串(string),replaceText是替代查找到的字符串。。2015-04-04

