Advertisement
1xptolevitico69

Javascript video poster

Aug 22nd, 2022 (edited)
987
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.97 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <!-- Global site tag (gtag.js) - Google Analytics -->
  5.   <script async="" src="https://www.googletagmanager.com/gtag/js?id=G-420XFJRS1M"></script>
  6.   <script>
  7.     window.dataLayer = window.dataLayer || [];
  8.  
  9.     function gtag() {
  10.       dataLayer.push(arguments);
  11.     }
  12.     gtag('js', new Date());
  13.     gtag('config', 'G-420XFJRS1M');
  14.   </script>
  15.   <link rel="shortcut icon" href="https://1xpto.netlify.app/items/favicon.gif" type="image/x-icon">
  16.   <meta charset="UTF-8">
  17.   <meta http-equiv="X-UA-Compatible" content="IE=edge">
  18.   <meta name="keywords" content="Javascript video poster">
  19.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  20.   <title>Javascript video poster</title>
  21.   <style>
  22.  
  23.     .app {
  24.       font-size: 10px;
  25.       z-index: 1;
  26.       text-decoration: none;
  27.       font-family: verdana;
  28.       font-weight: bold;
  29.       color: red;
  30.       position: absolute;
  31.       top: 0;
  32.       right: 0;
  33.       margin: 5px;
  34.       border: 2px solid red;
  35.       padding: 5px 10px;
  36.     }
  37.  
  38.  
  39.  
  40.     body {
  41.       cursor: context-menu;
  42.       margin: 0;
  43.       font-family: calibri;
  44.     }
  45.  
  46.     #sec {
  47.       border: 5px solid red;
  48.       width: 60%;
  49.       height: calc(60vw / 1.777);
  50.       position: relative;
  51.       margin: 10px auto;
  52.     }
  53.  
  54.     #vid {
  55.       transform-origin: left top;
  56.       width: 100%;
  57.       position: absolute;
  58.     }
  59.  
  60.     #poster {
  61.       position: absolute;
  62.       width: 100%;
  63.       height: 100%;
  64.     }
  65.  
  66.     @media all and (orientation:portrait) {
  67.       #sec {
  68.         border: 0;
  69.         width: 100%;
  70.         height: calc(100vw / 1.777);
  71.         margin: 0;
  72.       }
  73.     }
  74.  
  75.     @media all and (orientation:landscape) and (max-width:768px) {
  76.       #sec {
  77.         border: 0;
  78.         width: 100%;
  79.         height: calc(100vw / 1.777);
  80.         margin: 0;
  81.       }
  82.     }
  83.   </style>
  84. </head>
  85.  
  86. <body>
  87.   <a class='app' href='https://1xpto.netlify.app/'>Visite Site</a>
  88.  
  89.  
  90.  
  91.   <section id='sec'>
  92.     <video id='vid' src='video.mp4' controls></video>
  93.     <img onclick='start()' id='poster' src='poster.jpg' />
  94.   </section>
  95.  
  96.   <script>
  97.     vid.addEventListener("loadedmetadata", function() {
  98.       x = this.videoWidth;
  99.       y = this.videoHeight;
  100.       videoRatio = x / y;
  101.       boxHeight = getComputedStyle(sec).getPropertyValue("height");
  102.       videoWidth = getComputedStyle(vid).getPropertyValue("width");
  103.       videoHeight = parseFloat(videoWidth) / parseFloat(videoRatio);
  104.       result = parseFloat(boxHeight) / parseFloat(videoHeight);
  105.       vid.style.transform = "scale(1," + result + ")";
  106.     });
  107.  
  108.     function start() {
  109.       poster.style.display = 'none';
  110.       vid.play();
  111.       vid.style.display = 'block';
  112.     }
  113.  
  114.      vid.ontimeupdate = () => {
  115.        if(vid.currentTime == vid.duration ){
  116.        poster.style.display = 'block';
  117.        vid.pause();
  118.        vid.currentTime = 0;
  119.        vid.style.display = 'none';
  120.     }
  121.     }
  122.  
  123.  
  124.    
  125.   </script>
  126. </body>
  127. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement