2014-12-07 83 views
0

我有兩個傳送帶see codpen link。我試着當兩個特定的幻燈片顯示對方像上面做一些事情:提前兩張幻燈片匹配時貓頭鷹傳送帶警報

function match(){ 
    var topCarousel = $('#carousel-top').data('owlCarousel'); 
    var bottomCarousel = $('#carousel-bottom').data('owlCarousel'); 
    if(topCarousel.currentItem === 2 && bottomCarousel.currentItem === 2){alert('Match!')}} 

感謝

回答

1

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) 

}); 
+0

非常感謝。您是否有時間更詳細地解釋您的解決方案?我不確定我是否理解兩個輪播是如何匹配的?非常感謝 – James 2014-12-08 10:14:27

+0

請參閱源代碼中的註釋 – dm4web 2014-12-08 12:07:06

+0

好吧,現在我知道註釋掉的代碼不是必需的,現在有更多的意義。這是由於我缺乏oj javascript/jquery知識,但是你能解釋'afterMove:function(i)'中'match()'的第一個實例以及爲什麼'(i)'非常感謝 – James 2014-12-08 12:46:52

相關問題