html 视频播放选项

ryevplcw  于 2024-01-04  发布在  其他
关注(0)|答案(2)|浏览(264)

我是JavaScript的初学者,我设法合并了两个控制视频播放的函数,然而,它们有点相互干扰。
这个函数应该提供自定义暂停和播放视频控件

  1. // VIDEO CONTROLS START
  2. const video = document.getElementById("heroVideo"),
  3. pauseControl = document.getElementById("pauseControl"),
  4. playControl = document.getElementById("playControl"),
  5. playPauseButton = document.getElementById("playPauseButton");
  6. playPauseButton.addEventListener("click", function () {
  7. if (video.paused) {
  8. video.play();
  9. playPauseButton.classList.remove("play");
  10. playPauseButton.classList.add("pause");
  11. pauseControl.style.display = "unset";
  12. playControl.style.display = "none";
  13. } else {
  14. video.pause();
  15. playPauseButton.classList.remove("pause");
  16. playPauseButton.classList.add("play");
  17. pauseControl.style.display = "none";
  18. playControl.style.display = "unset";
  19. video.removeAttribute("controls");
  20. }
  21. });
  22. // VIDEO CONTROLS END

字符串
这一个暂停和自动播放视频时,用户的视野(所以它不播放的背景)

  1. // VIDEO OFFLOAD START
  2. function videoOffload() {
  3. // Get all video elements with the "video-offload" class
  4. const videos = document.querySelectorAll(".video-offload");
  5. // Function to handle the Intersection Observer for a single video
  6. function handleVideoIntersection(video) {
  7. // Define the options for the Intersection Observer
  8. const options = {
  9. root: null, // Use the viewport as the root
  10. rootMargin: "0px", // No margin
  11. threshold: 0.1, // 10% of the target element must be visible to trigger
  12. };
  13. // Callback function when the video enters or exits the viewport
  14. const callback = (entries, observer) => {
  15. entries.forEach((entry) => {
  16. if (entry.isIntersecting) {
  17. // The video is in the viewport, so play it and show controls
  18. video.play();
  19. video.removeAttribute("controls");
  20. } else {
  21. // The video is out of the viewport, so pause it and hide controls
  22. video.pause();
  23. }
  24. });
  25. };
  26. // Create an Intersection Observer with the specified options and callback for the current video
  27. const observer = new IntersectionObserver(callback, options);
  28. // Start observing the current video element
  29. observer.observe(video);
  30. }
  31. // Iterate over all video elements and apply the Intersection Observer
  32. videos.forEach((video) => {
  33. video.setAttribute("autoplay", "false"); // Disable autoplay initially
  34. // Apply Intersection Observer to the current video
  35. handleVideoIntersection(video);
  36. });
  37. }
  38. // Call the videoOffload function to initialize
  39. videoOffload();
  40. // VIDEO OFFLOAD END


视频控制按钮

  1. <button id="playPauseButton" class="play">
  2. <i id="playControl" class="fa-regular fa-circle-play fa-xl"></i>
  3. <i id="pauseControl" class="fa-regular fa-circle-pause fa-xl"></i>
  4. </button>


问题是只要我手动暂停视频(利用第一个功能)滚动出视频的视野,然后再次滚动回视频,它恢复播放(第二个函数覆盖了第一个函数)。不过,这很好,视频控制图标是反向的,你需要点击两次-暂停,然后根据暂停和播放功能逻辑播放(因为它们在点击时会发生变化,视频是由另一个功能触发的)。
它们都在同一个js文件中。我试图将它们分开保存,但问题仍然存在。
我还尝试根据IF语句将图标注入到HTML中-playPauseButton.innerHTML = "<i id='pauseControl' class='fa-regular fa-circle-pause fa-xl'></i>";,但是没有工作。
我想到的解决方案是将它们作为视频当前状态的指示器-播放或暂停,所以无论视频是如何触发的,它都会显示正确的图标或使这两个功能互不干扰。
你认为最好的解决方案和代码是什么?

sirbozc5

sirbozc51#

考虑将控件管理重构为video元素上的事件侦听器。这将是视频是否正在播放的最准确的事实来源:

  1. function updateControls() {
  2. if (video.paused) {
  3. playPauseButton.classList.remove("pause");
  4. playPauseButton.classList.add("play");
  5. pauseControl.style.display = "none";
  6. playControl.style.display = "unset";
  7. video.removeAttribute("controls");
  8. } else {
  9. playPauseButton.classList.remove("play");
  10. playPauseButton.classList.add("pause");
  11. pauseControl.style.display = "unset";
  12. playControl.style.display = "none";
  13. }
  14. }
  15. video.addEventListener('pause', updateControls);
  16. video.addEventListener('play', updateControls);
  17. playPauseButton.addEventListener("click", function() {
  18. if (video.paused) {
  19. video.play();
  20. } else {
  21. video.pause();
  22. }
  23. });

字符串

  1. // VIDEO CONTROLS START
  2. const video = document.getElementById("heroVideo"),
  3. pauseControl = document.getElementById("pauseControl"),
  4. playControl = document.getElementById("playControl"),
  5. playPauseButton = document.getElementById("playPauseButton");
  6. function updateControls() {
  7. if (video.paused) {
  8. playPauseButton.classList.remove("pause");
  9. playPauseButton.classList.add("play");
  10. pauseControl.style.display = "none";
  11. playControl.style.display = "unset";
  12. video.removeAttribute("controls");
  13. } else {
  14. playPauseButton.classList.remove("play");
  15. playPauseButton.classList.add("pause");
  16. pauseControl.style.display = "unset";
  17. playControl.style.display = "none";
  18. }
  19. }
  20. video.addEventListener('pause', updateControls);
  21. video.addEventListener('play', updateControls);
  22. playPauseButton.addEventListener("click", function() {
  23. if (video.paused) {
  24. video.play();
  25. } else {
  26. video.pause();
  27. }
  28. });
  29. // VIDEO CONTROLS END
  30. // VIDEO OFFLOAD START
  31. function videoOffload() {
  32. // Get all video elements with the "video-offload" class
  33. const videos = document.querySelectorAll(".video-offload");
  34. // Function to handle the Intersection Observer for a single video
  35. function handleVideoIntersection(video) {
  36. // Define the options for the Intersection Observer
  37. const options = {
  38. root: null, // Use the viewport as the root
  39. rootMargin: "0px", // No margin
  40. threshold: 0.1, // 10% of the target element must be visible to trigger
  41. };
  42. // Callback function when the video enters or exits the viewport
  43. const callback = (entries, observer) => {
  44. entries.forEach((entry) => {
  45. if (entry.isIntersecting) {
  46. // The video is in the viewport, so play it and show controls
  47. video.play();
  48. video.removeAttribute("controls");
  49. } else {
  50. // The video is out of the viewport, so pause it and hide controls
  51. video.pause();
  52. }
  53. });
  54. };
  55. // Create an Intersection Observer with the specified options and callback for the current video
  56. const observer = new IntersectionObserver(callback, options);
  57. // Start observing the current video element
  58. observer.observe(video);
  59. }
  60. // Iterate over all video elements and apply the Intersection Observer
  61. videos.forEach((video) => {
  62. video.setAttribute("autoplay", "false"); // Disable autoplay initially
  63. // Apply Intersection Observer to the current video
  64. handleVideoIntersection(video);
  65. });
  66. }
  67. // Call the videoOffload function to initialize
  68. videoOffload();
  69. // VIDEO OFFLOAD END
  1. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha512-DTOQO9RWCH3ppGqcWaEA1BIZOC6xxalwEsw9c2QQeAIftl+Vegovlnee1c9QX4TctnWMn13TZye+giMm8e2LwA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
  2. <video id="heroVideo" class="video-offload" loop controls>
  3. <source src="https://v3.cdnpk.net/videvo_files/video/free/2013-08/large_preview/hd0992.mp4" type="video/mp4">
  4. <source src="https://v3.cdnpk.net/videvo_files/video/free/2013-08/large_watermarked/hd0992_preview.mp4" type="video/mp4">
  5. </video>
  6. <button id="playPauseButton" class="play">
  7. <i id="playControl" class="fa-regular fa-circle-play fa-xl"></i>
  8. <i id="pauseControl" class="fa-regular fa-circle-pause fa-xl"></i>
  9. </button>
  10. <div style="height: 150vh"></div>
展开查看全部
1tu0hz3e

1tu0hz3e2#

好的,我使用事件监听器来实现图标功能,用于视频播放或暂停。我还对状态变量进行了一些调整,该状态变量可以跟踪视频是由用户暂停还是由videoOffload()暂停。现在视频暂停和播放工作正常,但是,可见图标只是默认的暂停图标,暂停视频后不切换到播放。所以这条语句不会被执行:

  1. pauseControl.style.display = "none";
  2. playControl.style.display = "unset";

个字符

相关问题