alt-yt-player.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * @Author: Joachim M. Giæver
  3. * URL: https://git.giaever.org/Magy/alt-youtube-player
  4. */
  5. // Hack to load Youtube iframe_api
  6. jQuery.getScript("https://www.youtube.com/iframe_api")
  7. var ytd = $.Deferred();
  8. window.onYouTubeIframeAPIReady = function () {
  9. ytd.resolve(window.YT);
  10. };
  11. $(document).ready(function () {
  12. // Store all instances of «Youtube Players»
  13. var ytp = [];
  14. // Change effect on IN and OUT here.
  15. // Will affect on both «playerOpen» and «playerClose»
  16. $.fn.outEffect = $.fn.fadeOut;
  17. $.fn.inEffect = $.fn.fadeIn;
  18. // When a player opens
  19. var playerOpen = function (i, player) {
  20. if (!ytp[i])
  21. return;
  22. // Remove thumb and show iframe
  23. player.find('.thumb').outEffect();
  24. player.find('iframe').inEffect({
  25. complete: function () {
  26. // When fully opened, plat video and change cursor
  27. if (ytp[i].getPlayerState() != YT.PlayerState.PLAYING)
  28. ytp[i].playVideo()
  29. player.removeClass('play loading').addClass('stop');
  30. }
  31. });
  32. // Close all the players that might be open.
  33. $('.player').each(function (idx) {
  34. if (i == idx) return;
  35. playerClose(idx, $(this));
  36. })
  37. }
  38. // When a player is closing
  39. var playerClose = function (i, player) {
  40. if (!ytp[i])
  41. return;
  42. // Hide iframe and show thumg
  43. player.find('iframe').outEffect()
  44. player.find('.thumb').inEffect(function() {
  45. // Stop the video; if playing
  46. if (ytp[i].getPlayerState() == YT.PlayerState.PLAYING)
  47. ytp[i].pauseVideo();
  48. player.removeClass('stop loading').addClass('play');
  49. });
  50. }
  51. // Make sure Iframe has the same size as the thumb-img
  52. var adoptThumbSizeToIframe = function (player) {
  53. var ytvid = player.find('iframe');
  54. var thumb = player.find('.thumb');
  55. var hidden = thumb.is(':hidden');
  56. // Hack to read size
  57. if (hidden)
  58. thumb.show();
  59. player.css({
  60. height: thumb.height(),
  61. });
  62. // Adopt the size of the thumbnail to the iframe
  63. ytvid.css({
  64. height: thumb.height(),
  65. width: thumb.width(),
  66. });
  67. if (hidden)
  68. thumb.hide();
  69. }
  70. // On scroll, check that we are withing bounding. Close if not.
  71. window.addEventListener('scroll', function (e) {
  72. var w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
  73. var h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
  74. var d = $(document).scrollTop();
  75. $('.player').each(function (i) {
  76. if ($(this).find('iframe:visible').length == 0)
  77. return; // player not open (already showing thumb)
  78. var x = $(this).offset()['top'];
  79. var xoff = $(this).height() / 15;
  80. if ((x + $(this).height() - xoff) < d || (x + xoff) > (d + h))
  81. playerClose(i, $(this));
  82. /*
  83. No horizontal test... but...
  84. var y = $(this).offset()['left'];
  85. var yoff = ($(this).width() / 15);
  86. */
  87. })
  88. })
  89. window.addEventListener('resize', function () {
  90. $('.player').each(function() {
  91. console.log("Resize");
  92. adoptThumbSizeToIframe($(this));
  93. })
  94. })
  95. // Youtube API is reacy
  96. ytd.done(function (YT) {
  97. /*
  98. Initialize player on all of them.
  99. Could be optimized and wait with init until the player should start,
  100. and destroy it when it's closed.
  101. */
  102. $(".player").each(function (i) {
  103. var player = $(this).addClass('loading');
  104. var ytvid = player.find('iframe').hide();
  105. adoptThumbSizeToIframe(player);
  106. // Generate and add id to the iframe
  107. ytvid.attr('id', 'player-' + (i+1));
  108. ytp.push(new YT.Player(ytvid.attr('id'), {
  109. events: {
  110. 'onReady': function (e) {
  111. player.removeClass('loading').addClass('play');
  112. player.bind('click', function () {
  113. playerOpen(i, player);
  114. })
  115. },
  116. 'onStateChange': function (e) {
  117. // Add event specific data here
  118. switch (e.data) {
  119. case YT.PlayerState.ENDED:
  120. playerClose(i, player)
  121. break;
  122. case YT.PlayerState.PLAYING:
  123. // console.log("PLAYING");
  124. break;
  125. case YT.PlayerState.PAUSED:
  126. player.removeClass('stop loading').addClass('play');
  127. break;
  128. case YT.PlayerState.BUFFERING:
  129. player.removeClass('stop play').addClass('loading');
  130. break;
  131. case YT.PlayerState.CUED:
  132. //console.log("CUED");
  133. break;
  134. }
  135. },
  136. }
  137. }));
  138. })
  139. })
  140. });