http://codepen.io/anon/pen/xbZLGb
使用afterMove
事件來獲得當前的索引轉盤,然後兩個請致電功能match()
。
// the current value of the index for both the carousel
var currIndTop=0;
var currIndBottom=0;
$(document).ready(function(){
$('#carousel-top').owlCarousel({
singleItem : true,
pagination : false,
navigation: true,
navigationText: [
"<i class='icon left'></i>",
"<i class='icon right'></i>"
],
// an event that is triggered when the slide changes
afterMove:function(i){
// get top carousel
var $car = $('#carousel-top').data('owlCarousel');
// get current index for top carousel
currIndTop = $car.currentItem;
match();
}
});
// similarly to the previous carousel
$('#carousel-bottom').owlCarousel({
singleItem : true,
pagination : false,
navigation: true,
navigationText: [
"<i class='icon left'></i>",
"<i class='icon right'></i>"
],
afterMove:function(i){
var $car = $('#carousel-bottom').data('owlCarousel');
currIndBottom = $car.currentItem;
match();
}
});
function match(){
// this is unnecessary. I forgot to delete
// var topCarousel = $('#carousel-top').data('owlCarousel');
// var bottomCarousel = $('#carousel-bottom').data('owlCarousel');
if(currIndBottom === 2 && currIndBottom === 2){alert('Match!')}
}
$(".header-top,.header-bottom")
.velocity("transition.slideLeftIn", { stagger: 300 })
.delay(750)
});
非常感謝。您是否有時間更詳細地解釋您的解決方案?我不確定我是否理解兩個輪播是如何匹配的?非常感謝 – James 2014-12-08 10:14:27
請參閱源代碼中的註釋 – dm4web 2014-12-08 12:07:06
好吧,現在我知道註釋掉的代碼不是必需的,現在有更多的意義。這是由於我缺乏oj javascript/jquery知識,但是你能解釋'afterMove:function(i)'中'match()'的第一個實例以及爲什麼'(i)'非常感謝 – James 2014-12-08 12:46:52