vimeo 视频完成后关闭弹出窗口

close popup after vimeo video finish

我已经实现了一个小脚本来在 vimeo 视频结束时使用 froogaloop 关闭 magnific 弹出窗口,

这是我的代码:

var SlampLightbox = (function(undefined){

    var mp; //store Magnific Popup global object

    var mp_exixts = function(){
        if( $.fn.magnificPopup ){
            mp = $.magnificPopup.instance
            return true;
        }else{
            return false;
        }
    }

    var open_from_hash = function(){
        var hash = window.location.hash.slice(1); //cache hash

        if( hash.length  > 1 && hash != '#!'){
            var mark_pos = hash.indexOf('?');
            if( mark_pos != -1)
                hash = hash.substring(0, mark_pos);

            var selector = 'a[name='+hash+']';
            $(selector).click(); //trigger click event on the element to open magnificPopup

         }
    }

    var open = function($element){
        $element.magnificPopup({
            delegate: 'a',
            type: 'iframe',
            tLoading: '',
            iframe: {
              markup: '<div class="slamp-mfp-iframe-scaler">'+
                        '<button title="Close (Esc)" type="button" class="slamp-mfp-close">x</button>'+
                        '<iframe id="vimeoplayer" class="mfp-iframe" frameborder="0" allowfullscreen></iframe>'+
                      '</div>', // HTML markup of popup, `mfp-close` will be replaced by the close button
     patterns: {
       vimeo:{
          index: 'vimeo.com/',
          id: '/',
          src: '//player.vimeo.com/video/%id%?autoplay=1&api=1&player_id=vimeoplayer'
       }
     }
            },
            callbacks: {
                markupParse: function(template, values, item) {

        _attachVideoEvent(template, values, item);


                }

            }
        })
    }

    var _close = function(){
        mp.close();
    }

var _attachVideoEvent = function(template, values, item){

    var playerOrigin = '*';
    var player = $f( template.find("iframe")[0] );

    if( player.length == 0 )
    return;

    var onFinish = function(){
        _close();
    }

    player.addEvent('ready', function() {
        player.addEvent('finish', onFinish);
    });



  }

    return {
        init: function($element){//input must be a jQuery object

            if( mp_exixts() ){
                open($element);

                if( $element.length == 0)
                    return;

                open_from_hash(); //open a video specified in the hash, if any

                $(document.body).on('click', '.slamp-mfp-close', _close);
            }else{
                console.error("MagnificPopup doesn't exists");
                return false;
            }

            },
        mp: mp
    }
})(undefined);
window.SlampLightbox = SlampLightbox; //global function

您可以在这里查看:

http://codepen.io/anon/pen/oxZpPL

但它只工作一次,因为我第二次点击 img 时得到

javascript 错误:

VM983 froogaloop2.min.js:1 Uncaught TypeError: Cannot read property 'postMessage' of null

但我不明白为什么,这是我的错? o froogaloop 错误? 请帮助我理解

谢谢

试试这个 codepen ;)

在这里,当我们附加 _attachVideoEvent 时,它会做一些 postMessage 的事情,这会引发错误并且 JavaScript 执行中断;我们已经延迟了这个绑定,以便弹出打开然后我们进行绑定。它仍然会抛出错误,但没问题。

/**
 * JS module for open elements in lightbox
 *
 * @dependencies MagnificPopup
 **/
var SlampLightbox = (function(undefined) {

  var mp; //store Magnific Popup global object

  var mp_exixts = function() {
    if ($.fn.magnificPopup) {
      mp = $.magnificPopup.instance
      return true;
    } else {
      return false;
    }
  }

  var open_from_hash = function() {
    var hash = window.location.hash.slice(1); //cache hash

    if (hash.length > 1 && hash != '#!') {
      var mark_pos = hash.indexOf('?');
      if (mark_pos != -1)
        hash = hash.substring(0, mark_pos);

      var selector = 'a[name=' + hash + ']';
      $(selector).click(); //trigger click event on the element to open magnificPopup

    }
  }

  var open = function($element) {
    $element.magnificPopup({
      delegate: 'a',
      type: 'iframe',
      tLoading: '',
      iframe: {
        markup: '<div class="slamp-mfp-iframe-scaler">' +
          '<button title="Close (Esc)" type="button" class="slamp-mfp-close">x</button>' +
          '<iframe id="vimeoplayer" class="mfp-iframe" frameborder="0" allowfullscreen></iframe>' +
          '</div>', // HTML markup of popup, `mfp-close` will be replaced by the close button
        patterns: {
          vimeo: {
            index: 'vimeo.com/',
            id: '/',
            src: '//player.vimeo.com/video/%id%?autoplay=1&api=1&player_id=vimeoplayer'
          }
        }
      },
      callbacks: {
        /*
     beforeOpen: function(){
      $(window).on("navigate", function (event, data) {
        var direction = data.state.direction;
        console.log( direction );
        if (direction == 'back') {
       $.magnificPopup.close();
        }
      });

     },
          */
        markupParse: function(template, values, item) {
          setTimeout(function() {
            _attachVideoEvent(template, values, item);
          });
        },
        elementParse: function(item) {
          var $item = item.el; //opened jQuery object

          if ($item.hasClass("video-thumb")) { //google analytics track event
            var video_name = $item.attr("name");

            if (history.pushState)
              history.pushState(null, null, '#' + video_name);
            else
              location.replace(('' + window.location).split('#')[0] + '#' + video_name);

            if (typeof ga != 'undefined')
              ga('send', 'event', 'Lightbox open', 'Video page', video_name);

          }
        },
        close: function() {
          if (window.location.hash != '') {
            if (history.pushState)
              history.pushState(null, null, '#!');
            else
              location.replace(('' + window.location).split('#')[0] + '#!');
          }
        }

      }
    })
  }

  var _close = function() {
    mp.close();
  }

  var _attachVideoEvent = function(template, values, item) {

    var playerOrigin = '*';
    var player = $f(template.find("iframe")[0]);

    if (player.length == 0)
      return;

    var onFinish = function() {
      _close();
    }

    player.addEvent('ready', function() {
      player.addEvent('finish', onFinish);
    });
  }

  return {
    init: function($element) { //input must be a jQuery object

      if (mp_exixts()) {
        open($element);

        if ($element.length == 0)
          return;

        open_from_hash(); //open a video specified in the hash, if any

        $(document.body).on('click', '.slamp-mfp-close', _close);
      } else {
        console.error("MagnificPopup doesn't exists");
        return false;
      }

    },
    mp: mp
  }
})(undefined);
window.SlampLightbox = SlampLightbox; //global function


$(document).ready(function() {
  SlampLightbox.init($('.video_header'));
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.1.0/magnific-popup.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.1.0/jquery.magnific-popup.min.js"></script>
<script src="https://f.vimeocdn.com/js/froogaloop2.min.js"></script>

<div class="video_header">
  <a class="thumb-video" name="video" href="https://vimeo.com/159375756">
    <img class="img-responsive" height="250" src="http://test.slamp.it/wp-content/themes/slamp/partials/templates/bob-wilson/img/timeline/Robert-Wilson-project-SlampHQ_thumb.jpg">
  </a>
</div>