video-story-section.ejs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
  2. <div class="content-wrapper">
  3. <div class="content-title">
  4. <h2>
  5. <%= translate.videoStories[lang] %>:
  6. </h2>
  7. <a href="<%= lang %>/videos">
  8. <%= translate.more[lang] %> >>>
  9. </a>
  10. </div>
  11. <div class="content">
  12. <div class="album-icon-grid">
  13. <% videos.forEach(video=> { %>
  14. <div class="album-grid-card">
  15. <div style="padding: 2px; font-size: 14px; color: grey; text-align: center; white-space: nowrap;">
  16. <%= translate.videoStory[lang] %>
  17. </div>
  18. <a href="javascript:;" onclick="onPlay(`<%= video.url %>`, `<%= video.jsonStr %>`)">
  19. <img src="<%= video.poster %>" class="album-icon-img" alt="<%= video.seoTitle %>">
  20. <img src="/assets/svg/play-button.svg" , class="video-play-button" width="20px" height="20px"
  21. alt="Coloring Page Video Play Button">
  22. </a>
  23. </div>
  24. <% }); %>
  25. </div>
  26. </div>
  27. </div>
  28. <!-- 弹出层 -->
  29. <div id="video-popup" class="popup">
  30. <div class="popup-content-wrapper">
  31. <span class="close" onclick="closeVideoPopup()">&times;</span>
  32. <div class="popup-content-left">
  33. <video id="video-player" controls></video>
  34. </div>
  35. <div id="video-content" class="popup-content-right">
  36. </div>
  37. </div>
  38. </div>
  39. <script>
  40. function onPlay(url, videoJson) {
  41. console.log(url);
  42. console.log(videoJson);
  43. var video = JSON.parse(`${videoJson}`);
  44. generateVideoContent(video);
  45. var videoPopup = document.getElementById('video-popup');
  46. var videoPlayer = document.getElementById('video-player');
  47. videoPopup.style.display = 'block';
  48. if (Hls.isSupported()) {
  49. var hls = new Hls();
  50. hls.loadSource(url);
  51. hls.attachMedia(videoPlayer);
  52. hls.on(Hls.Events.MANIFEST_PARSED, function () {
  53. videoPlayer.play();
  54. });
  55. } else if (videoPlayer.canPlayType('application/vnd.apple.mpegurl')) {
  56. videoPlayer.src = url;
  57. videoPlayer.addEventListener('canplay', function () {
  58. videoPlayer.play();
  59. });
  60. }
  61. window.closeVideoPopup = function () {
  62. videoPopup.style.display = 'none';
  63. videoPlayer.pause(); // 停止视频播放
  64. videoPlayer.src = ''; // 重置视频源,避免浏览器缓存问题
  65. };
  66. // 点击模态对话框外部时关闭对话框(可选,但推荐添加)
  67. videoPopup.addEventListener('click', function (event) {
  68. if (event.target === videoPopup) {
  69. closeVideoPopup();
  70. }
  71. });
  72. // 防止点击关闭按钮时事件冒泡到弹出层导致关闭(因为我们已经为关闭按钮单独绑定了事件)
  73. var closeButton = document.querySelector('.close');
  74. if (closeButton) {
  75. closeButton.addEventListener('click', function (event) {
  76. event.stopPropagation();
  77. });
  78. }
  79. }
  80. function generateVideoContent(video) {
  81. var div = document.getElementById('video-content');
  82. div.innerHTML = `
  83. <h1 style="font-size: 16pt; font-weight: bold;">${video.seoTitle}</h1>
  84. `;
  85. var contentHTML = '<div>';
  86. video.contents.forEach(item => {
  87. contentHTML += `
  88. <div data-content-id="${item._id}" class="image-card" style="margin-bottom: 10px;">
  89. <a href=${item.uri}><img src=${item.thumb} alt='${item.title}'></a>
  90. <div class="card-title">${item.title}</div>
  91. </div>
  92. `
  93. });
  94. contentHTML += '</div>';
  95. div.innerHTML += contentHTML;
  96. }
  97. </script>