index.html 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Alt. youtube player</title>
  5. <style>
  6. /*
  7. Adapt to your needs,
  8. but it shouldn't be necessary to add much of css
  9. as the js-script will handle sizing.
  10. */
  11. * {padding: 0;margin: 0;} /* Not necessary when using frameworks */
  12. .player {
  13. width: 70vw;
  14. margin: 0 auto;
  15. }
  16. /*
  17. Note that the > restricts the img to be on the root level of .player
  18. */
  19. .player > img.thumb {
  20. width: 100%;
  21. }
  22. .player > iframe {
  23. width: 100%;
  24. height: 100%;
  25. }
  26. .player.play {
  27. cursor: pointer;
  28. }
  29. .player.stop {
  30. cursor: not-allowed;
  31. }
  32. .player.loading {
  33. cursor: wait;
  34. }
  35. </style>
  36. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  37. </head>
  38. <body>
  39. <!--
  40. A player MUST:
  41. - Have a class «player»
  42. - Have a child element of img
  43. - with the class «thumb»
  44. - Have a child of the iframe
  45. - that has the query-param «enablejsapi=1»
  46. -->
  47. <div class="player">
  48. <img class="thumb" src="./thumb.png" alt="Thumbnail" />
  49. <div class="something wf realated classed and shit">
  50. <iframe src="https://www.youtube-nocookie.com/embed/dcsvMySGmS8?enablejsapi=1" frameborder="0" allow="autoplay; encrypted-media; gyroscope" allowfullscreen></iframe>
  51. </div>
  52. </div>
  53. <!-- additional players -->
  54. <div class="player">
  55. <img class="thumb" src="./thumb.png" alt="Thumbnail" />
  56. <iframe src="https://www.youtube-nocookie.com/embed/pouODQkJQG0?enablejsapi=1" frameborder="0" allow="autoplay; encrypted-media; gyroscope" allowfullscreen></iframe>
  57. </div>
  58. <div class="player">
  59. <img class="thumb" src="./thumb.png" alt="Thumbnail" />
  60. <iframe src="https://www.youtube-nocookie.com/embed/Y-JQ-RCyPpQ?enablejsapi=1" frameborder="0" allow="autoplay; encrypted-media; gyroscope" allowfullscreen></iframe>
  61. </div>
  62. <div class="player">
  63. <img class="thumb" src="./thumb.png" alt="Thumbnail" />
  64. <iframe src="https://www.youtube-nocookie.com/embed/4-079YIasck?enablejsapi=1" frameborder="0" allow="autoplay; encrypted-media; gyroscope" allowfullscreen></iframe>
  65. </div>
  66. <!-- You need this before the </body>-tag (closing) -->
  67. <script>
  68. // Hack to load Youtube iframe_api
  69. jQuery.getScript("http://www.youtube.com/iframe_api")
  70. var ytd = $.Deferred();
  71. window.onYouTubeIframeAPIReady = function () {
  72. ytd.resolve(window.YT);
  73. };
  74. $(document).ready(function () {
  75. // Store all instances of «Youtube Players»
  76. var ytp = [];
  77. // When a player opens
  78. var playerOpen = function (i, player) {
  79. if (!ytp[i])
  80. return;
  81. // Remove thumb and show iframe
  82. player.find('.thumb').slideUp();
  83. player.find('iframe').slideDown({
  84. complete: function () {
  85. // When fully opened, plat video and change cursor
  86. ytp[i].playVideo()
  87. player.removeClass('play loading').addClass('stop');
  88. }
  89. });
  90. // Close all the players that might be open.
  91. $('.player').each(function (idx) {
  92. if (i == idx) return;
  93. playerClose(i, $(this));
  94. })
  95. }
  96. // When a player is closing
  97. var playerClose = function (i, player) {
  98. if (!ytp[i])
  99. return;
  100. // Stop the video
  101. ytp[i].stopVideo();
  102. // Hide iframe and show thumg
  103. player.find('iframe').slideUp()
  104. player.find('.thumb').slideDown(function() {
  105. player.removeClass('stop loading').addClass('play');
  106. });
  107. }
  108. // Make sure Iframe has the same size as the thumb-img
  109. var adoptThumbSizeToIframe = function (player) {
  110. var ytvid = player.find('iframe');
  111. var thumb = player.find('.thumb');
  112. var hidden = thumb.is(':hidden');
  113. // Hack to read size
  114. if (hidden)
  115. thumb.show();
  116. // Adopt the size of the thumbnail to the iframe
  117. ytvid.css({
  118. height: thumb.height(),
  119. width: thumb.width(),
  120. });
  121. if (hidden)
  122. thumb.hide();
  123. }
  124. // On scroll, check that we are withing bounding. Close if not.
  125. window.addEventListener('scroll', function (e) {
  126. var w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
  127. var h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
  128. var d = $(document).scrollTop();
  129. $('.player').each(function (i) {
  130. if ($(this).find('iframe:visible').length == 0)
  131. return; // player not open (already showing thumb)
  132. var x = $(this).offset()['top'];
  133. var xoff = $(this).height() / 15;
  134. if ((x + $(this).height() - xoff) < d || (x + xoff) > (d + h))
  135. playerClose(i, $(this));
  136. /*
  137. No horizontal test... but...
  138. var y = $(this).offset()['left'];
  139. var yoff = ($(this).width() / 15);
  140. */
  141. })
  142. })
  143. window.addEventListener('resize', function () {
  144. $('.player').each(function() {
  145. console.log("Resize");
  146. adoptThumbSizeToIframe($(this));
  147. })
  148. })
  149. // Youtube API is reacy
  150. ytd.done(function (YT) {
  151. /*
  152. Initialize player on all of them.
  153. Could be optimized and wait with init until the player should start,
  154. and destroy it when it's closed.
  155. */
  156. $(".player").each(function (i) {
  157. var player = $(this).addClass('loading');
  158. var ytvid = player.find('iframe').hide();
  159. adoptThumbSizeToIframe(player);
  160. // Generate and add id to the iframe
  161. ytvid.attr('id', 'player-' + (i+1));
  162. ytp.push(new YT.Player(ytvid.attr('id'), {
  163. events: {
  164. 'onReady': function (e) {
  165. player.removeClass('loading').addClass('play');
  166. player.bind('click', function () {
  167. playerOpen(i, player);
  168. })
  169. },
  170. 'onStateChange': function (e) {
  171. // Add event specific data here
  172. switch (e.data) {
  173. case YT.PlayerState.ENDED:
  174. playerClose(i, player)
  175. break;
  176. case YT.PlayerState.PLAYING:
  177. // console.log("PLAYING");
  178. break;
  179. case YT.PlayerState.PAUSED:
  180. player.removeClass('stop loading').addClass('play');
  181. break;
  182. case YT.PlayerState.BUFFERING:
  183. player.removeClass('stop play').addClass('loading');
  184. break;
  185. case YT.PlayerState.CUED:
  186. //console.log("CUED");
  187. break;
  188. }
  189. },
  190. }
  191. }));
  192. })
  193. })
  194. });
  195. </script>
  196. </body>
  197. </html>