为什么这个 Bootstrap 轮播滑动功能对我不起作用?
Why won't this Bootstrap Carousel swipe function work for me?
我找到了这个代码笔:https://codepen.io/andrearufo/pen/rVWpyE
我很想用它,但出于某种原因它对我不起作用,即使我不做任何更改就复制它。这是为什么?
我没有忘记嵌入 bootstrap 和 jquery。
我还尝试了其他解决方案来实现滑动功能,但出于某种原因,其中 none 行得通。唯一可以滑动的旋转木马(我在 Google Chrome 开发者工具中试过)是在这个代码笔上。
$(".carousel").swipe({
swipe: function(event, direction, distance, duration, fingerCount, fingerData) {
if (direction == 'left') $(this).carousel('next');
if (direction == 'right') $(this).carousel('prev');
},
allowPageScroll:"vertical"
});
<div class="container">
<div class="page-header">
<h1>Bootstrap default carousel with swipe and touch gestures</h1>
<p class="lead">How to add the swipes gesture to comand the Bootstrap carousel on mobile devices.</p>
<p>Made by <strong>Andrea Rufo</strong>, more info and tutorial on <a href="http://www.orangedropdesign.com/"> OrangeDropDesign.com</a></p>
</div>
<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="https://unsplash.it/1400/600?image=114">
</div>
<div class="item">
<img src="https://unsplash.it/1400/600?image=745">
</div>
<div class="item">
<img src="https://unsplash.it/1400/600?image=315">
</div>
<div class="item">
<img src="https://unsplash.it/1400/600?image=622">
</div>
<div class="item">
<img src="https://unsplash.it/1400/600?image=401">
</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>
这是您可能会错过的示例 JQuery
或 Swipe
LIB。
$(".carousel").swipe({
swipe: function(event, direction, distance, duration, fingerCount, fingerData) {
if (direction == 'left') $(this).carousel('next');
if (direction == 'right') $(this).carousel('prev');
},
allowPageScroll:"vertical"
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery.touchswipe/1.6.4/jquery.touchSwipe.min.js"></script>
<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="https://unsplash.it/1400/600?image=114">
</div>
<div class="item">
<img src="https://unsplash.it/1400/600?image=315">
</div>
<div class="item">
<img src="https://unsplash.it/1400/600?image=622">
</div>
<div class="item">
<img src="https://unsplash.it/1400/600?image=401">
</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>
我希望你能得到帮助
(function ($) {
var touchStartX = null;
$('.carousel').each(function () {
var $carousel = $(this);
$(this).on('touchstart', function (event) {
var e = event.originalEvent;
if (e.touches.length == 1) {
var touch = e.touches[0];
touchStartX = touch.pageX;
}
}).on('touchmove', function (event) {
var e = event.originalEvent;
if (touchStartX != null) {
var touchCurrentX = e.changedTouches[0].pageX;
if ((touchCurrentX - touchStartX) > 60) {
touchStartX = null;
$carousel.carousel('prev');
} else if ((touchStartX - touchCurrentX) > 60) {
touchStartX = null;
$carousel.carousel('next');
}
}
}).on('touchend', function () {
touchStartX = null;
});
});
})(jQuery);
这是工作演示的 link - https://jsfiddle.net/L5begcz5/
我找到了这个代码笔:https://codepen.io/andrearufo/pen/rVWpyE
我很想用它,但出于某种原因它对我不起作用,即使我不做任何更改就复制它。这是为什么?
我没有忘记嵌入 bootstrap 和 jquery。
我还尝试了其他解决方案来实现滑动功能,但出于某种原因,其中 none 行得通。唯一可以滑动的旋转木马(我在 Google Chrome 开发者工具中试过)是在这个代码笔上。
$(".carousel").swipe({
swipe: function(event, direction, distance, duration, fingerCount, fingerData) {
if (direction == 'left') $(this).carousel('next');
if (direction == 'right') $(this).carousel('prev');
},
allowPageScroll:"vertical"
});
<div class="container">
<div class="page-header">
<h1>Bootstrap default carousel with swipe and touch gestures</h1>
<p class="lead">How to add the swipes gesture to comand the Bootstrap carousel on mobile devices.</p>
<p>Made by <strong>Andrea Rufo</strong>, more info and tutorial on <a href="http://www.orangedropdesign.com/"> OrangeDropDesign.com</a></p>
</div>
<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="https://unsplash.it/1400/600?image=114">
</div>
<div class="item">
<img src="https://unsplash.it/1400/600?image=745">
</div>
<div class="item">
<img src="https://unsplash.it/1400/600?image=315">
</div>
<div class="item">
<img src="https://unsplash.it/1400/600?image=622">
</div>
<div class="item">
<img src="https://unsplash.it/1400/600?image=401">
</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>
这是您可能会错过的示例 JQuery
或 Swipe
LIB。
$(".carousel").swipe({
swipe: function(event, direction, distance, duration, fingerCount, fingerData) {
if (direction == 'left') $(this).carousel('next');
if (direction == 'right') $(this).carousel('prev');
},
allowPageScroll:"vertical"
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery.touchswipe/1.6.4/jquery.touchSwipe.min.js"></script>
<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="https://unsplash.it/1400/600?image=114">
</div>
<div class="item">
<img src="https://unsplash.it/1400/600?image=315">
</div>
<div class="item">
<img src="https://unsplash.it/1400/600?image=622">
</div>
<div class="item">
<img src="https://unsplash.it/1400/600?image=401">
</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>
我希望你能得到帮助
(function ($) {
var touchStartX = null;
$('.carousel').each(function () {
var $carousel = $(this);
$(this).on('touchstart', function (event) {
var e = event.originalEvent;
if (e.touches.length == 1) {
var touch = e.touches[0];
touchStartX = touch.pageX;
}
}).on('touchmove', function (event) {
var e = event.originalEvent;
if (touchStartX != null) {
var touchCurrentX = e.changedTouches[0].pageX;
if ((touchCurrentX - touchStartX) > 60) {
touchStartX = null;
$carousel.carousel('prev');
} else if ((touchStartX - touchCurrentX) > 60) {
touchStartX = null;
$carousel.carousel('next');
}
}
}).on('touchend', function () {
touchStartX = null;
});
});
})(jQuery);
这是工作演示的 link - https://jsfiddle.net/L5begcz5/