Search code examples
javascripthtmljquerymodal-dialogsimplemodal

Autoplay video when modal opens html


i'm new to html and web development, I am testing a basic page where a button opens a modal and in that modal there's a video.

Can you help me making it autoplay when the modal opens and stop when it closes? I tried a lot op options i found on the web but I can´t get it to work.

I started using w3schools examples.

<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Work+Sans&display=swap" rel="stylesheet">
<style>
body, button {font-family: 'Work Sans', sans-serif;}

/* The Modal (background) */
.modal {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 1; /* Sit on top */
  padding-top: 7px; /* Location of the box */
  left: 0;
  top: 0;
  width: 100%; /* Full width */
  height: 100%; /* Full height */
  overflow: auto; /* Enable scroll if needed */
  background-color: rgb(0,0,0); /* Fallback color */
  background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content */
.modal-content {
  position: relative;
  background-color: #fefefe;
  margin: auto;
  padding: 0;
  border: 1px solid #888;
  width: 80%;
  box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
  -webkit-animation-name: animatetop;
  -webkit-animation-duration: 0.4s;
  animation-name: animatetop;
  animation-duration: 0.4s
}

/* Add Animation */
@-webkit-keyframes animatetop {
  from {top:-300px; opacity:0} 
  to {top:0; opacity:1}
}

@keyframes animatetop {
  from {top:-300px; opacity:0}
  to {top:0; opacity:1}
}

/* The Close Button */
.close {
  color: black;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}

.modal-header {
  padding: 2px 16px;
  height: 30px;
  background-color: white;
  color: white;
}

.modal-body {padding: 2px 16px;}

.modal-footer {
  padding: 2px 16px;
  background-color: #004F9E;
  color: white;
}

div.parent {
    text-align: center;
    }
ul { 
    display: inline-block; 
    text-align: left; 
    }
    
button {
        background-color: #004F9E; /* Green */
        border: 2px solid #004F9E;
        border-radius: 5px;
        color: white;
        padding: 5px 10px;
        text-align: center;
        text-decoration: none;
        display: inline-block;
        font-size: 14px;
        }
        
button {
        transition-duration: 0.4s;
        }

button:hover {
  background-color: white; 
  color: black; 
  border: 2px solid #004F9E;
}



myBtn {
        display: flex; 
        justify-content: center; 
        border: none;
        }
    
</style>
</head>
<body>

<h2 style='text-align:center'>Titulo</h2>

<div class="parent">
    <ul>
  <li>order1.</li>

  <li>order2.</li>

  <li>orde3.</li>

    
</ul>
</div>

<!-- Trigger/Open The Modal -->
<div class="parent">
    <button id="myBtn">Ver Video</button>
</div>

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <div class="modal-body">
    <span class="close">&times;</span>
     <div  style='text-align:center'>
        <video width="100%"; height="95%" controls>
        <source src="video.mp4" type="video/mp4">
        Your browser does not support the video tag.
        </video>
        </div>
    </div>
    
     
    </div>
  </div>

</div>

<script>
// Get the modal
var modal = document.getElementById("myModal");

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal 
btn.onclick = function() {
  modal.style.display = "block"
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
  modal.style.display = "none";
  
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}

  
</script>

</body>
</html>

Solution

  • You can use play() and pause()

    document.getElementById('myVideo').play(); // <-- PLAY
    
    document.getElementById('myVideo').pause(); // <-- PAUSE
    

    In the example, I have commented on the places where I added these new lines

    Example:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    
        <link rel="preconnect" href="https://fonts.gstatic.com">
        <link href="https://fonts.googleapis.com/css2?family=Work+Sans&display=swap" rel="stylesheet">
    
        <style>
            body,
            button {
                font-family: 'Work Sans', sans-serif;
            }
    
            /* The Modal (background) */
            .modal {
                display: none;
                /* Hidden by default */
                position: fixed;
                /* Stay in place */
                z-index: 1;
                /* Sit on top */
                padding-top: 7px;
                /* Location of the box */
                left: 0;
                top: 0;
                width: 100%;
                /* Full width */
                height: 100%;
                /* Full height */
                overflow: auto;
                /* Enable scroll if needed */
                background-color: rgb(0, 0, 0);
                /* Fallback color */
                background-color: rgba(0, 0, 0, 0.4);
                /* Black w/ opacity */
            }
    
            /* Modal Content */
            .modal-content {
                position: relative;
                background-color: #fefefe;
                margin: auto;
                padding: 0;
                border: 1px solid #888;
                width: 80%;
                box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
                -webkit-animation-name: animatetop;
                -webkit-animation-duration: 0.4s;
                animation-name: animatetop;
                animation-duration: 0.4s
            }
    
            /* Add Animation */
            @-webkit-keyframes animatetop {
                from {
                    top: -300px;
                    opacity: 0
                }
    
                to {
                    top: 0;
                    opacity: 1
                }
            }
    
            @keyframes animatetop {
                from {
                    top: -300px;
                    opacity: 0
                }
    
                to {
                    top: 0;
                    opacity: 1
                }
            }
    
            /* The Close Button */
            .close {
                color: black;
                float: right;
                font-size: 28px;
                font-weight: bold;
            }
    
            .close:hover,
            .close:focus {
                color: #000;
                text-decoration: none;
                cursor: pointer;
            }
    
            .modal-header {
                padding: 2px 16px;
                height: 30px;
                background-color: white;
                color: white;
            }
    
            .modal-body {
                padding: 2px 16px;
            }
    
            .modal-footer {
                padding: 2px 16px;
                background-color: #004F9E;
                color: white;
            }
    
            div.parent {
                text-align: center;
            }
    
            ul {
                display: inline-block;
                text-align: left;
            }
    
            button {
                background-color: #004F9E;
                /* Green */
                border: 2px solid #004F9E;
                border-radius: 5px;
                color: white;
                padding: 5px 10px;
                text-align: center;
                text-decoration: none;
                display: inline-block;
                font-size: 14px;
            }
    
            button {
                transition-duration: 0.4s;
            }
    
            button:hover {
                background-color: white;
                color: black;
                border: 2px solid #004F9E;
            }
    
            /* #myBtn {
                display: flex;
                justify-content: center;
                border: none;
            } */
        </style>
    
    </head>
    <body>
    
        <h2 style='text-align:center'>Titulo</h2>
    
        <div class="parent">
            <ul>
                <li>order1.</li>
    
                <li>order2.</li>
    
                <li>orde3.</li>
            </ul>
        </div>
    
        <!-- Trigger/Open The Modal -->
        <div class="parent">
            <button id="myBtn">Ver Video</button>
        </div>
    
        <!-- The Modal -->
        <div id="myModal" class="modal">
    
            <!-- Modal content -->
            <div class="modal-content">
                <div class="modal-body">
                    <span class="close">&times;</span>
                    <div style='text-align:center'>
                        <video id="myVideo" width="100%" height="95%" controls>
                            <source src="https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4"
                                type="video/mp4">
                            Your browser does not support the video tag.
                        </video>
                    </div>
                </div>
    
    
            </div>
        </div>
    
        </div>
    
        <script>
            // Get the modal
            var modal = document.getElementById("myModal");
    
            // Get the button that opens the modal
            var btn = document.getElementById("myBtn");
    
            // Get the <span> element that closes the modal
            var span = document.getElementsByClassName("close")[0];
    
            // When the user clicks the button, open the modal 
            btn.onclick = function () {
                modal.style.display = "block"
                document.getElementById('myVideo').play(); // <-- PLAY
            }
    
            // When the user clicks on <span> (x), close the modal
            span.onclick = function () {
                modal.style.display = "none";
                document.getElementById('myVideo').pause(); // <-- PAUSE
            }
    
            // When the user clicks anywhere outside of the modal, close it
            window.onclick = function (event) {
                if (event.target == modal) {
                    modal.style.display = "none";
                    document.getElementById('myVideo').pause(); // <-- PAUSE
                }
            }
        </script>
    
    </body>
    </html>