如何使用CSS创建全屏视频背景?
要使用CSS创建全屏视频背景,代码如下-
示例
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style> * { box-sizing: border-box; } body { margin: 0; font-family: "SegoeUI-", Tahoma, Geneva, Verdana, sans-serif; font-size: 17px; } .backgroundVideo { position: fixed; right: 0; bottom: 0; min-width: 100%; min-height: 100%; } .content { position: fixed; bottom: 0; background: rgba(0, 0, 0, 0.5); color: #f1f1f1; width: 100%; padding: 20px; } .playPauseBtn { width: 200px; font-size: 18px; padding: 10px; border: none; background: rgb(72, 22, 167); color: #fff; cursor: pointer; opacity: 0.8; } .playPauseBtn:hover { opacity: 1; } </style> </head> <body> <video autoplay muted loop class="backgroundVideo"> <source src="https://cdn.videvo.net/videvo_files/video/free/2019- 05/small_watermarked/190416_10_Drone1_04_preview.webm" type="video/mp4"/> </video> <div class="content"> <h1>Heading</h1> <h2>Some Sample text</h2> <button class="playPauseBtn">Pause</button> </div> <script> document .querySelector(".playPauseBtn") .addEventListener("click", toggleVideo); var video = document.querySelector(".backgroundVideo"); var btn = document.querySelector(".playPauseBtn"); function toggleVideo() { if (video.paused) { video.play(); btn.innerHTML = "Pause"; } else { video.pause(); btn.innerHTML = "Play"; } } </script> </body> </html>
输出结果
上面的代码将产生以下输出-