使用Swipe jQuery Bootstrap 3的旋转木马幻灯片

时间:2016-06-30 00:50:05

标签: javascript jquery html twitter-bootstrap

我试图用这个线程的解决方案来做到这一点:

How to make Bootstrap carousel slider use mobile left/right swipe

但是对我不起作用,这是我的代码:

<!DOCTYPE html>
<html lang="es">
  <head>
    <meta charset="utf-8">
     <link href="assets/css/bootstrap.css" rel="stylesheet">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="assets/js/jquery.mobile-1.4.5.js"></script>
    <script src="assets/js/jquery.mobile-1.4.5.min.js"></script>
   </head>
   <body>
    <section class="wrap" id="month" >
    <div id="myCarousel" class="carousel slide" data-ride="carousel">
      <!-- Indicators -->
      <ol class="carousel-indicators">
        <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
        <li data-target="#myCarousel" data-slide-to="1"></li>
        <li data-target="#myCarousel" data-slide-to="2"></li>
        <li data-target="#myCarousel" data-slide-to="3"></li>
      </ol>

      <!-- Wrapper for slides -->
      <div class="carousel-inner" role="listbox">
        <div class="item active">
          <img src="" alt="Chania">
        </div>

        <div class="item">
          <img src="" alt="Chania2">
        </div>

        <div class="item">
          <img src="" alt="Flower1">
        </div>

        <div class="item">
          <img src="" alt="Flower2">
        </div>
      </div>

      <!-- Left and right controls -->
      <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
        <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
        <span class="sr-only">Previous</span>
      </a>
      <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
        <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
      </a>
    </div>
    </section>
    <script src="assets/js/bootstrap.min.js"></script>
    <SCRIPT>

        $(document).ready(function() {  
           $("#myCarousel").swiperight(function() {  
              $("#myCarousel").carousel('prev');  
            });  
           $("#myCarousel").swipeleft(function() {  
              $("#myCarousel").carousel('next');  
           });  
        }); 
    </SCRIPT>
</body>

2 个答案:

答案 0 :(得分:1)

为什么不使用slick slider?它支持在桌面/移动设备上滑动。

答案 1 :(得分:0)

$(".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 ){
            $(".carousel").carousel('prev');
        }
        else if( Math.floor(xClick - xMove) < -5 ){
            $(".carousel").carousel('next');
        }
    });
    $(".carousel").on("touchend", function(){
            $(this).off("touchmove");
    });
});

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