Search code examples
jqueryapivimeoflexsliderfroogaloop

Attach Froogaloop event to Flexslider current slide


I am building a slider with multiple Vimeo videos using Woo Theme's Flexslider. I am able to get the Flexslider to play and pause based on the vimeo events, using their Froogaloop ($f) library, however I cannot get the next/previous events in Flexslider to pause the video.

It works if each vimeo player is hardcoded as a variable, however I need the slider to be able to support any number of videos. See an example here: http://demo.astronautweb.co/slider/flex/vimeo-multi.html

The example on the Flexslider page hooks the slider event with $f(player) which is an object (I think). This only targets the last slide when I use it in my code. W

When I try to use Flexslider's own slide.currentSlide, I can only target an element, not an object. This is where I have reach the upper limits of my javascript knowledge.

Here's the code:

$(window).load(function(){  

var vimeoPlayers = document.querySelectorAll('iframe'),
    player;

for (var i = 0, length = vimeoPlayers.length; i < length; i++) {
    player = vimeoPlayers[i];
    $f(player).addEvent('ready', ready);
}

function addEvent(element, eventName, callback) {
    (element.addEventListener) ? element.addEventListener(eventName, callback, false) : element.attachEvent(eventName, callback, false);
}

function ready(player_id) {
    var froogaloop = $f(player_id);

    froogaloop.addEvent('play', function(data) {
        $('.flexslider').flexslider("pause");
    });

    froogaloop.addEvent('pause', function(data) {
        $('.flexslider').flexslider("play");
    });
}

var slider = $(".flexslider");

slider.flexslider({
    animation: "slide",
    animationLoop: true,
    slideshowSpeed: 5000,
    video: true, 
    start: function(slider){
        $('body').removeClass('loading');
    },
    before: function(slider){ 

        // this only pauses the last slide
        $f(player).api('pause');

        // this is the Flexslider API for targeting the current slide
        var currentSlide = slider.slides.eq(slider.currentSlide + 1),
            currentFrame = currentSlide.find('iframe');

        console.log(currentFrame);  
    }
});

});


Solution

  • From your example on astronautweb.co it looks like you're using an older version of the Froogaloop API. Grabbing the latest should make this a lot easier https://github.com/vimeo/player-api/tree/master/javascript

    // Enable the API on each Vimeo video
        $('iframe.vimeo-player').each(function(){
      $f(this).addEvent('ready');
    });
    
    // Call fitVid before FlexSlider initializes, so the proper initial height can be retrieved.
    $(".flexslider")
        .fitVids()
      .flexslider({
        animation: "slide",
        slideshow: false,
        animationLoop: false,
        smoothHeight: true,
        start: function(slider) {
          $('body').removeClass('loading');
        },
        before: function(slider) {
    
          currentSlide = slider.currentSlide;
          player_id = currentSlide + 1
          currentVideo = 'player_' + player_id;
    
          $f(currentVideo).api('pause');
    
        }
    });
    

    Here's another example of the Froogaloop API 2.0