Search code examples
jquerycarouselflexslider

Flexslider with dynamic content. e.g. images and / or video


I amd using Flexslider to display images and video together but I am trying to figure out how to have it so that if a video is not present to show just the images and vice a versa. Currently, if both are present: images and video it works but as soon as you remove the video, the slider stops loading.

Basic code from their demo:

    // Vimeo API nonsense
    var player = document.getElementById('player_1');
    $f(player).addEvent('ready', ready);

    function addEvent(element, eventName, callback) {
        if (element.addEventListener) {
            element.addEventListener(eventName, callback, false)
        } else {
            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");
        });
    }
    // Call fitVid before FlexSlider initializes, so the proper initial height can be retrieved.

$(".flexslider")
    .fitVids()
    .flexslider({
      animation: "slide",
      useCSS: false,
      animationLoop: false,
      smoothHeight: true,
      before: function(slider){
        $f(player).api('pause');
      }
  });

and

<div class="flexslider">
  <ul class="slides">
    <li>
      <iframe id="player_1" src="http://player.vimeo.com/video/39683393?api=1&player_id=player_1" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
    </li>
    <li>
      <img src="slide2.jpg" />
    </li>
    <li>
      <img src="slide3.jpg" />
    </li>
    <li>
      <img src="slide4.jpg" />
    </li>
  </ul>
</div>

Solution

  • I realised that the following:

    before: function(slider){
            $f(player).api('pause');
          }
    

    was causing a conflict when no video was present so I created two players one which woulds accept video and images (which is the original code) and a second player just for images. Using:

    $(".flexslider-img").flexslider({
          animation: "slide",
          useCSS: false,
          animationLoop: false,
          smoothHeight: true
      });
    

    Then, as I am using wordpress I used post formats to outputrelevant code. for example, if the post contained a video, the user would elect the "Video" as the post format, for which the relevant code used:

    // Vimeo API nonsense
        var player = document.getElementById('player_1');
        $f(player).addEvent('ready', ready);
    
        function addEvent(element, eventName, callback) {
            if (element.addEventListener) {
                element.addEventListener(eventName, callback, false)
            } else {
                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");
            });
        }
        // Call fitVid before FlexSlider initializes, so the proper initial height can be retrieved.
    
    $(".flexslider-video")
        .fitVids()
        .flexslider({
          animation: "slide",
          useCSS: false,
          animationLoop: false,
          smoothHeight: true,
          before: function(slider){
            $f(player).api('pause');
          }
      });
    

    No doubt there's a better way of doing this, but it worked for me.