如何制作Bootstrap轮播滑块使用移动左/右滑动

use*_*469 61 html css jquery carousel twitter-bootstrap-3

DEMO JSSFIDDLE

  <div class="col-md-4">
          <!--Carousel-->
          <div id="sidebar-carousel-1" class="carousel slide" data-ride="carousel">
            <ol class="carousel-indicators grey">
              <li data-target="#sidebar-carousel-1" data-slide-to="0" class="active"></li>
              <li data-target="#sidebar-carousel-1" data-slide-to="1"></li>
              <li data-target="#sidebar-carousel-1" data-slide-to="2"></li>
            </ol>
            <div class="carousel-inner">
              <div class="item active">
                <a href="" data-lightbox="image-1" title="">
                <img src="http://shrani.si/f/3D/13b/1rt3lPab/2/img1.jpg" alt="...">
                </a>
              </div>
              <div class="item">
                <a href="" data-lightbox="image-1" title="">
                <img src="http://shrani.si/f/S/jE/UcTuhZ6/1/img2.jpg" alt="...">
                </a>                  </div>
              <div class="item">
                <a href="" data-lightbox="image-1" title="">
                <img src="http://shrani.si/f/h/To/1yjUEZbP/img3.jpg" alt="...">
                </a>                  </div>
            </div>
             <!-- Controls -->
        <a class="left carousel-control" href="#sidebar-carousel-1" data-slide="prev">
          <span class="glyphicon glyphicon-chevron-left"></span>
        </a>
        <a class="right carousel-control" href="#sidebar-carousel-1" data-slide="next">
          <span class="glyphicon glyphicon-chevron-right"></span>
        </a>
                  </div><!--/Carousel--></div>
Run Code Online (Sandbox Code Playgroud)

我想要做的是仅为移动设备添加左/右触摸滑动功能.我怎样才能做到这一点?

另外,如何在移动/ ipad版本中制作出悬停时显示的上一个/下一个按钮?

谢谢

Mar*_*ldi 97

我需要将这个功能添加到我最近正在处理的项目中,并且添加jQuery Mobile只是为了解决这个问题似乎有些过分,所以我想出了一个解决方案并将其放在github上:bcSwipe(Bootstrap Carousel Swipe).

它是一个轻量级的jQuery插件(大约600字节缩小,与8kb的jQuery Mobile触摸事件相比),并且已经在Android和iOS上进行了测试.

这是你如何使用它:

$('.carousel').bcSwipe({ threshold: 50 });
Run Code Online (Sandbox Code Playgroud)

  • @Mark Shiraldi我试图使用Bootstrap Carousel Swipe和脚本,但它无法正常工作 (3认同)
  • 好一个Maaark.我只是尝试使用**Bootstrap v4.0.0-alpha.5和jQuery 3.1.1**.在我的Android智能手机上刷卡工作正常**并按预期在大屏幕上自动渲染. (2认同)

Lia*_*iam 96

我参加派对有点晚了,但这里有一些我一直在使用的jQuery:

$(".carousel").on("touchstart", function(event){
        var xClick = event.originalEvent.touches[0].pageX;
    $(this).one("touchmove", function(event){
        var xMove = event.originalEvent.touches[0].pageX;
        if( Math.floor(xClick - xMove) > 5 ){
            $(this).carousel('next');
        }
        else if( Math.floor(xClick - xMove) < -5 ){
            $(this).carousel('prev');
        }
    });
    $(".carousel").on("touchend", function(){
            $(this).off("touchmove");
    });
});
Run Code Online (Sandbox Code Playgroud)

不需要jQuery mobile或任何其他插件.如果需要调整滑动的灵敏度,请调整5-5.希望这有助于某人.

  • 很好的解决方案,虽然你有'prev'和'next'倒置.比添加完整的移动jQuery解决方案轻得多.谢谢. (7认同)
  • 哇,很酷的解决方案!但我在页面上有几个旋转木马.所以我更喜欢在函数内使用`$(this)`而不是`$(".carousel')`.而且我也交换了`prev`和`next` :) (5认同)
  • 我修改了答案 (5认同)
  • 太好了,谢谢@Liam。这正是我正在寻找的。 (2认同)

Abd*_*ikh 88

更新:

当我不熟悉网页设计时,我想出了这个解决方案.现在我变得更老更聪明了,Mark Shiraldi给出的答案似乎是一个更好的选择.看下一个最佳答案; 这是一个更有效的解决方案.

我最近参与了一个项目,这个例子运作得很好.我给你以下链接.

首先你必须添加jQuery mobile:

http://jquerymobile.com/

这会增加触控功能,然后您只需要进行滑动等事件:

<script>
$(document).ready(function() {
   $("#myCarousel").swiperight(function() {
      $(this).carousel('prev');
    });
   $("#myCarousel").swipeleft(function() {
      $(this).carousel('next');
   });
});
</script>
Run Code Online (Sandbox Code Playgroud)

链接如下,您可以在其中找到我使用的教程:

http://lazcreative.com/blog/how-to/how-to-adding-swipe-support-to-bootstraps-carousel/

  • 由于安装jQuery mobile以解决一个简单问题的严厉性,我正在投票.为简单起见,我建议Mark Shiraldi给出答案,它优雅且代码少. (16认同)
  • 绝对遵循建议并在http://jquerymobile.com/download-builder/上构建仅限触摸事件的自定义jQuery Mobile版本.否则,如果尚未使用jQuery Mobile构建网站,您可能会破坏您的网站. (13认同)

ADI*_*IKA 5

使用 bootstrap 4.2 现在非常简单,您只需要在 carousel div 中将 touch 选项传递为data-touch="true",因为它只接受布尔值。

在您的情况下,将引导程序更新到 4.2 并按确切顺序粘贴(或下载源文件)以下内容:

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
Run Code Online (Sandbox Code Playgroud)

然后在 bootstrap carousel div 中添加触摸选项

    <div id="sidebar-carousel-1" class="carousel slide" data-ride="carousel" data-touch="true">
Run Code Online (Sandbox Code Playgroud)