How to manage different events with event listener for different elements?

时光怂恿深爱的人放手 提交于 2019-12-06 11:15:48

You will need to work with adding parameters to the functions in a way to re-use as much code as possible. The way i see it you can add playerid in all related functions and use that to minimize code within the ready(player_id) by utilizing the player_id as main id to help access all related video items.

$(document).ready(function () {
 var player = $('#myVid')[0];
 var player2 = $('#myVid2')[0];
 $f(player).addEvent('ready', ready); 
 $f(player2).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);

    function onFinish() {
      froogaloop.addEvent('finish', function(data) {
      toggleOverlay(player_id);
      resetVideo(player_id);
  });
 }
 onFinish();
}

});

function toggleOverlay(playerid){
  $("#" + playerid).parent("#specialBox").parent().css("opacity",".8");
  $("#" + playerid).parent("#specialBox").parent().toggle();
  $("#" + playerid).parent("#specialBox").toggle();
}

function resetVideo(vidID)
{
   var player;
   player=$('#' + vidID);
   $f(player[0]).api('unload');
}

Example using froogaloop2.js lib[old]: http://codepen.io/Nasir_T/pen/pEmEJE

Example using player.js from vimeo[new improved]: http://codepen.io/Nasir_T/pen/GNKjbe

Let me know if you need more help.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!