---
title: "【转】让 Bootstrap 轮播效果支持左右滑动手势"
author: "wangyanlong0107"
date: "2017-03-10T11:02:52Z"
tags: []
categories: ["Web前端"]
slug: "bootstrap"
draft: false
lastmod: "2017-03-10T11:02:52Z"
id: 2361358
url: "https://bigdragon.iteye.com/blog/2361358"
---

因为最近开发的项目涉及到移动设备上的 [HTML5](http://lib.csdn.net/base/html5 "HTML5知识库") 开发，其中需要实现轮播效果。

然后最快捷的方式，你知道的（Bootstrap），然后原生的 Bootstrap 的 carousel.js 插件并没有支持手势。

然后......自己想办法呗，再然后，就有下面3种解决方案 ：

 

- jQuery Mobile ([http://jquerymobile.com/download/](http://jquerymobile.com/download/))

**\[javascript]** [view plain](http://blog.csdn.net/lihaiping_/article/details/21407451 "view plain") [copy](http://blog.csdn.net/lihaiping_/article/details/21407451 "copy")

 

 [![在CODE上查看代码片](https://code.csdn.net/assets/CODE_ico.png)](https://code.csdn.net/snippets/241270 "在CODE上查看代码片")[![派生到我的代码片](https://code.csdn.net/assets/ico_fork.svg)](https://code.csdn.net/snippets/241270/fork "派生到我的代码片")

1. $("#carousel-generic").swipeleft(function() {
2. $(this).carousel('next');
3. });
4. $("#carousel-generic").swiperight(function() {
5. $(this).carousel('prev');
6. });

 

- TouchSwipe jQuery plugin ([https://github.com/mattbryson/TouchSwipe-Jquery-Plugin](https://github.com/mattbryson/TouchSwipe-Jquery-Plugin))

**\[javascript]** [view plain](http://blog.csdn.net/lihaiping_/article/details/21407451 "view plain") [copy](http://blog.csdn.net/lihaiping_/article/details/21407451 "copy")

 

 [![在CODE上查看代码片](https://code.csdn.net/assets/CODE_ico.png)](https://code.csdn.net/snippets/241270 "在CODE上查看代码片")[![派生到我的代码片](https://code.csdn.net/assets/ico_fork.svg)](https://code.csdn.net/snippets/241270/fork "派生到我的代码片")

1. $("#carousel-generic").swipe({
2. swipeLeft: function() { $(this).carousel('next'); },
3. swipeRight: function() { $(this).carousel('prev'); },
4. });

 

- hammer.js ([http://eightmedia.github.io/hammer.js/](http://eightmedia.github.io/hammer.js/)) +   
  jquery.hammer.js ([https://github.com/EightMedia/jquery.hammer.js](https://github.com/EightMedia/jquery.hammer.js))

**\[javascript]** [view plain](http://blog.csdn.net/lihaiping_/article/details/21407451 "view plain") [copy](http://blog.csdn.net/lihaiping_/article/details/21407451 "copy")

 

 [![在CODE上查看代码片](https://code.csdn.net/assets/CODE_ico.png)](https://code.csdn.net/snippets/241270 "在CODE上查看代码片")[![派生到我的代码片](https://code.csdn.net/assets/ico_fork.svg)](https://code.csdn.net/snippets/241270/fork "派生到我的代码片")

1. $('#carousel-generic').hammer().on('swipeleft', function(){
2. $(this).carousel('next');
3. });
4. $('#carousel-generic').hammer().on('swiperight', function(){
5. $(this).carousel('prev');
6. });

 

单单为了支持滑动手势而导入整个 [jQuery](http://lib.csdn.net/base/jquery "jQuery知识库") Mobile 貌似有些大材小用，

而 TouchSwipe 在两边可点击按钮区域滑动无效，

然后选择了 Hammer，

然后，&lt;完&gt;

 

（参考资料：[http://lazcreative.com/blog/adding-swipe-support-to-bootstrap-carousel-3-0/](http://lazcreative.com/blog/adding-swipe-support-to-bootstrap-carousel-3-0/)）