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

时间:2016-03-21 08:56:20

标签: javascript jquery magnific-popup vimeo-api froogaloop

当使用froogaloop完成vimeo视频时,我已经实现了一个小脚本来关闭大量弹出窗口,

这是我的代码:

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未捕获的TypeError:无法读取属性   &#39;的postMessage&#39;为null

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

感谢

1 个答案:

答案 0 :(得分:1)

试试这个codepen;)

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

/**
 *	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>