Advertisement
artemsemkin

Untitled

Oct 17th, 2023
620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function updateVideoSource(event) {
  2.   // Get a reference to the video element
  3.   const video = document.getElementsByTagName('video');
  4.  
  5.   if (video && video[0]) {
  6.     const currentSrc = video[0].getAttribute('src');
  7.     const smallVideoSrc = "https://projekt-login.de/wp-content/uploads/2023/10/4d-werk_comp_9x16.mp4";
  8.     const largeVideoSrc = "https://projekt-login.de/wp-content/uploads/2023/09/4d-werk_home.mp4";
  9.  
  10.     if (event.matches) {
  11.       // If the screen width is less than 768 pixels, use the small video
  12.       if (currentSrc !== smallVideoSrc) {
  13.         video[0].setAttribute('src', smallVideoSrc);
  14.       }
  15.     } else {
  16.       // If the screen width is 768 pixels or more, use the large video
  17.       if (currentSrc !== largeVideoSrc) {
  18.         video[0].setAttribute('src', largeVideoSrc);
  19.       }
  20.     }
  21.   }
  22. }
  23.  
  24. const mqVideo = window.matchMedia('(max-width: 767px)');
  25.  
  26. // BC for old Safari
  27. if (typeof mqVideo.addEventListener === 'function') {
  28.   mqVideo.addEventListener('change', updateVideoSource);
  29. } else {
  30.   mqVideo.addListener(updateVideoSource);
  31. }
  32.  
  33. // Initial set
  34. updateVideoSource({matches: mqVideo.matches});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement