向左/向右滑动以在Bootstrap 3 Carousel上移动

时间:2018-09-02 20:27:54

标签: javascript jquery html5 twitter-bootstrap jquery-mobile

我尝试使用jquery向左/向右向Bootstrap Carousel Slider滑动,但这不起作用。这是我的代码:

<div class="container">

  <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
    <!-- Indicators -->
    <ol class="carousel-indicators">
      <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
      <li data-target="#carousel-example-generic" data-slide-to="1"></li>
      <li data-target="#carousel-example-generic" data-slide-to="2"></li>
      <li data-target="#carousel-example-generic" data-slide-to="3"></li>
      <li data-target="#carousel-example-generic" data-slide-to="4"></li>
    </ol>

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

    <!-- Controls -->
    <a class="left carousel-control" href="#carousel-example-generic" 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="#carousel-example-generic" role="button" data-slide="next">
      <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>

</div>  

JavaScript:

$(".carousel").swipe({

  swipe: function(event, direction, distance, duration, fingerCount, fingerData) {

    if (direction == 'left') $(this).carousel('next');
    if (direction == 'right') $(this).carousel('prev');

  },
  allowPageScroll:"vertical"

});

我想补充一点,我也尝试过使用官方页面上的jquery移动脚本:

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script> 

无论如何我都无法滑动图像。 我在页面上使用的脚本:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

1 个答案:

答案 0 :(得分:0)

使用javascript启用它:

$("#carousel-example-generic").carousel();

$(".item").click(function(){
$("#carousel-example-generic").carousel(1);
});

$(".left carousel-control").click(function(){
$("#carousel-example-generic").carousel("prev");
});

$(".right carousel-control").click(function(){
    $("#carousel-example-generic").carousel("next");
    });