|
| 1 | +# 🎬 Video Modal Popup |
| 2 | + |
| 3 | +This project demonstrates how to create a **popup modal** that plays a **video trailer** when triggered by a button click. |
| 4 | +It’s perfect for **movie previews, product demos**, or any **interactive video showcase**. |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## 📁 Project Structure |
| 9 | +``` |
| 10 | +video-modal/ |
| 11 | +│ |
| 12 | +├── index.html # Main HTML structure |
| 13 | +├── style.css # Styling and responsive design |
| 14 | +├── script.js # Event handling and video control logic |
| 15 | +└── README.md # Project documentation |
| 16 | +``` |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +## 🧩 Features |
| 21 | +- 🎥 Opens a modal when clicking the **Play Trailer** button |
| 22 | +- ▶️ Plays an HTML5 video automatically inside the modal |
| 23 | +- ❌ Includes a close button and closes on outside click |
| 24 | +- 🔄 Automatically **pauses and resets** the video when closed |
| 25 | +- 📱 Fully **responsive** across all screen sizes |
| 26 | +- 💫 Smooth fade-in animation and hover effects |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +## ⚙️ How It Works (Detailed Explanation) |
| 31 | + |
| 32 | +### 🏗️ 1. index.html |
| 33 | + |
| 34 | +The `index.html` file defines the structure of the webpage. It includes: |
| 35 | + |
| 36 | +#### **Key Elements** |
| 37 | +- `<div class="movie-container">` — Wraps all content like the movie thumbnail, title, description, and button. |
| 38 | +- `<img>` — Displays the movie’s thumbnail or poster image. |
| 39 | +- `<h1>` — The movie title. |
| 40 | +- `<p>` — A short movie description or tagline. |
| 41 | +- `<button id="openModal">` — Button to trigger the modal popup. |
| 42 | +- `<div id="videoModal" class="modal">` — The modal container that holds the video player. |
| 43 | +- `<span class="close">×</span>` — The close icon (×) used to close the modal. |
| 44 | +- `<video>` — The HTML5 video element that plays the trailer inside the modal. |
| 45 | + |
| 46 | +These elements are organized in a clean hierarchy to ensure readability and easy maintenance. |
| 47 | + |
| 48 | +--- |
| 49 | + |
| 50 | +### 🎨 2. style.css |
| 51 | + |
| 52 | +The `style.css` file controls how the page looks and behaves on different screen sizes. It ensures the design is both **modern** and **responsive**. |
| 53 | + |
| 54 | +#### **Example Styles Explained** |
| 55 | + |
| 56 | +- `.modal` |
| 57 | + Creates a **full-screen overlay** when the modal is active. It uses properties like: |
| 58 | + ```css |
| 59 | + position: fixed; |
| 60 | + top: 0; |
| 61 | + left: 0; |
| 62 | + width: 100%; |
| 63 | + height: 100%; |
| 64 | + background: rgba(0, 0, 0, 0.7); |
| 65 | + display: none; |
| 66 | + justify-content: center; |
| 67 | + align-items: center; |
| 68 | + ``` |
| 69 | + This ensures the modal takes up the whole viewport with a dark transparent background. |
| 70 | + |
| 71 | +- `.modal-content` |
| 72 | + Styles the inner video player window. Example: |
| 73 | + ```css |
| 74 | + background: #000; |
| 75 | + padding: 10px; |
| 76 | + border-radius: 10px; |
| 77 | + max-width: 90%; |
| 78 | + width: 800px; |
| 79 | + ``` |
| 80 | + This keeps the video centered and scaled properly on all devices. |
| 81 | + |
| 82 | +- `@media (max-width: 768px)` |
| 83 | + Adjusts layout for **mobile and tablet** viewports, ensuring text and buttons resize correctly. |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | +### ⚙️ 3. script.js |
| 88 | + |
| 89 | +The `script.js` file manages all interactions between the user and the modal. |
| 90 | + |
| 91 | +#### **Functions Explained** |
| 92 | +1. **Element References** |
| 93 | + ```js |
| 94 | + const modal = document.getElementById("videoModal"); |
| 95 | + const btn = document.getElementById("openModal"); |
| 96 | + const span = document.getElementsByClassName("close")[0]; |
| 97 | + const video = document.getElementById("trailerVideo"); |
| 98 | + ``` |
| 99 | + These lines reference the key DOM elements for manipulation. |
| 100 | + |
| 101 | +2. **Open Modal Functionality** |
| 102 | + ```js |
| 103 | + btn.onclick = function() { |
| 104 | + modal.style.display = "flex"; |
| 105 | + video.play(); |
| 106 | + } |
| 107 | + ``` |
| 108 | + When the **Play Trailer** button is clicked, this function makes the modal visible and starts playing the video. |
| 109 | + |
| 110 | +3. **Close Modal Functionality** |
| 111 | + ```js |
| 112 | + span.onclick = function() { |
| 113 | + modal.style.display = "none"; |
| 114 | + video.pause(); |
| 115 | + video.currentTime = 0; |
| 116 | + } |
| 117 | + ``` |
| 118 | + When the close icon (×) is clicked, the modal hides, the video pauses, and the playback resets. |
| 119 | + |
| 120 | +4. **Outside Click Close** |
| 121 | + ```js |
| 122 | + window.onclick = function(event) { |
| 123 | + if (event.target == modal) { |
| 124 | + modal.style.display = "none"; |
| 125 | + video.pause(); |
| 126 | + video.currentTime = 0; |
| 127 | + } |
| 128 | + } |
| 129 | + ``` |
| 130 | + This ensures that clicking **outside the modal content** also closes the popup and stops the video. |
| 131 | + |
| 132 | +--- |
| 133 | + |
| 134 | +**Author:** Rohan Kedari |
0 commit comments