Tips on how to prevent a video from playing in the background of a popup even after it has been closed

After creating a video popup that plays upon clicking a button using jQuery, I encountered an issue where the video continues to play in the background even after closing the popup by clicking the close(X) button. Below is my code, can someone assist me in resolving this problem?

HTML:

<button class="light">Click Here</button>

    <div class="popup__overlay">
        <div id="popupVid" class="popup"><a class="popup__close" href="#">X</a><br />
            <video id="framevideo" controls="controls" width="300" height="150">
                <source src="http://indivillage.in/eocares_test/wp-content/uploads/2018/12/y2mate.com-doremon_episode_mini_doremon__yziyijrh5E_360p.mp4" type="video/mp4" />
            </video>
        </div>
    </div>

CSS:

.popup__overlay {
    display: none;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    text-align: center;
    z-index: 100;
}

.popup__overlay:after {
    display: inline-block;
    height: 100%;
    width: 0;
    vertical-align: middle;
    content: "";
}

.popup {
    display: inline-block;
    position: relative;
    width: 100%;
    height: 100%;
    max-width: 640px;
    max-height: 480px;
    padding: 20px;
    color: white;
    vertical-align: middle;
}

.popup-form__row {
    margin: 1em 0;
}

.popup__close {
    display: block;
    position: absolute;
    top: 20px;
    width: 20px;
    height: 12px;
    padding: 8px;
    cursor: pointer;
    text-align: center;
    font-size: 20px;
    line-height: 12px;
    color: white;
    text-decoration: none;
}

.popup__close:hover {
    color: #eea200;
}

#framevideo {
    width: 100%;
    height: 100%;
}

JS:

var p = jQuery(".popup__overlay");
jQuery(document).ready(function(){
    jQuery(".light").click(function() {
        p.css("display", "block");
    });
    p.click(function(event) {
        e = event || window.event;
        if (e.target == this) {
            jQuery(p).css("display", "none");
        }
    });

});

jQuery(".light").click(function() {
    p.css("visibility", "visible").css("opacity", "1");
});

p.click(function(event) {
    e = event || window.event;
    if (e.target == this) {
        jQuery(p)
            .css("visibility", "hidden")
            .css("opacity", "0");
        toggleVideo("hide");
    }
});

jQuery(".popup__close").click(function() {
    p.css("visibility", "hidden").css("opacity", "0");
    toggleVideo("hide");
});

Upon creating a video popup that launches when clicking a button using jQuery, I'm facing an issue where the video continues to play in the background even after closing the popup with the close(X) button. Can someone offer guidance on how to resolve this matter?

Answer №1

Implement the following modification in the jQuery script.

jQuery(".popup__close").on("click", function() {
    p.css("visibility", "hidden").css("opacity", "0");
    jQuery('#framevideo').get(0).pause();
    toggleVideo("hide");
});

Answer №2

In order to close the popup without any issues, make sure to pause the video beforehand. You can refer to this link for more information: or simply follow these steps:

function stopVideo() {
  video.pause();
  video.currentTime = 0;
}

If you're still facing problems, another approach could be to completely remove the content of the popup HTML which includes the video tag, and then recreate it when opening the popup again.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Extracting information from a JSON file is a straightforward process that involves accessing

After spending months trying to extract data from a JSON file, here is what I have encountered: [["wrere","bogdan12",31,21,"profile_pic/poza3.jpg",78,21,31,"profile_pic/download.jpg"] , ["hey men","bogdan12",31,21,"profile_pic/poza3.jpg",76,21,31,"pro ...

Encountering a 500 server error when using jQuery AJAX for a GET request

Check out the flask python code here: Also, take a look at the html+js in the index.html template: An issue arises where everything runs smoothly on the local Flask server but encounters a 500 error when attempting to search after deployment with Apache ...

Adding content to a div element using jQuery is a simple and effective way to

I am attempting to incorporate the content from a specific div into a jQuery function. Kindly refer to the details below: This is the content I want to include: <div id="year">2020</div> My objective is to replace the '2019' within ...

Close the ionicPopup by tapping anywhere

Currently, I have implemented an ionicPopup that automatically closes after a certain time limit. However, I am wondering if there is a way to configure it to close with a single or double tap anywhere on the screen instead. While I am aware that setting a ...

Tips for utilizing Jquery webcam on mobile devices like Android and iPhone

Currently, I am in the process of developing mobile web applications utilizing jquery mobile and HTML 5. I am working on a feature that requires access to the camera. In order to achieve this functionality, I have integrated the following plugin: While ...

Complete the form by submitting it along with the other forms once they have been added to the page

I've encountered a situation where I have a page containing a search button. Upon clicking the submit button, an ajax request is sent to fetch JSON data from the controller and populate my page with multiple product boxes. Each box represents a new fo ...

Unable to retrieve the value of a clicked button using jQuery

i am attempting to extract an array from checked buttons when a button is clicked. Although I can successfully retrieve the array, I am struggling to obtain the value of the clicked button. the code snippet below illustrates my attempt: $('.multiple& ...

The live() function is causing issues with my ajax request

Within my webpage, I have a link with an onclick() event that should display a div containing a date input text named "datepicker0", followed by another div with the id of "bContent". I've included the script below to implement a date filter on the d ...

Having trouble deleting files in PHP?

I am presenting an HTML form below: <form action='delete.php' method='POST'> <table> <div class = '.table'> <?php $dir = '../uploads/store/'; $new ...

TinyMCE toolbar missing the "hr" option

I am encountering an issue while using TinyMCE as my editor. I have added the plugin as instructed, but I cannot find the "hr" button/option in the editor interface. If anyone has any insights or solutions to this problem, please share! This is how I am ...

Choosing various li classes within a navigation bar

Struggling to pick the right JQuery elements for my portfolio site. The aim is to show/hide the .inner (Task) items by clicking on the .outer (Category) items, complete with rotating .arrows when the menu expands. Check out a similar question, along with ...

The Yii2 datepicker is failing to render

My goal is to incorporate multiple active forms into my app. I have used some jQuery code to append rows when the "add form" button is clicked, and everything is functioning correctly. However, I am encountering an issue where the datepicker input does not ...

Tips for making a sidebar sticky when scrolling

In order to keep the right-side bar fixed, I have implemented this javaScript function: <script type="text/javascript> $(document).ready(function () { var top = $('#rightsidebar-wrapper').offset().top - parseFloat($('#rightsideb ...

Tips for perfectly aligning all elements in a row within a card design

Hi there, I'm trying to figure out how to align the content of this card in a single row instead of stacked on top of each other. Here's the code snippet: <div class="container"> <form onSubmit={submitHandler}> ...

What is the best way to retrieve the @Url.Action url from a distinct script file?

Currently, I have organized my JavaScript file separately from my web pages. One issue I am encountering is that I am unable to write the following line of code: var url = '@Url.Action("AddTrade", "DataService")'; I hesitate to hard-code the UR ...

Looking for assistance with parsing out four numerical values from an HTML scrape using Python

I currently have code that opens a URL and retrieves HTML data into htmlA Within htmlA, I am attempting to extract 4 specific pieces of information: A date Price 1 Price 2 A percentage The section of htmlA where these 4 pieces of information are located ...

Is there a way to retrieve the HTML code of a DOM element created through JavaScript?

I am currently using java script to generate an svg object within my html document. The code looks something like this: mySvg = document.createElementNS("http://www.w3.org/2000/svg", "svg"); myPath = document.createElementNS("http://www.w3.org/2000/svg", ...

Viewing YouTube videos on an iPhone can be done through a web view

I am having some trouble playing videos in this specific format. http://www.youtube.com/embed/lNOMZoF9VlM?rel=0 Here is my HTML string. NSString *html = [NSString stringWithFormat:@"<html><head>\ <body s ...

Innovative sound system powered by React

I am working on implementing a music player feature on a website where users can select a song and have it play automatically. The challenge I am facing is with the play/pause button functionality. I have created all the necessary components, but there see ...

inconsistent firing of mousedown events

On my webpage, I am using the following JavaScript code: $("#attach-body").mousedown(function (event) { //alert(event.button); switch (event.button) { case 2: event.preventDefault(); event.stopPropagation(); break; default: ...