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

Having trouble integrating a custom plugin with CKEditor?

I am currently using version 4.7 of CKEditor, which is the latest version available. I am facing an issue where I cannot see a new custom plugin that I have added to the toolbar. I have checked my basic example of abbreviation (abbr) plugin but couldn&apos ...

How can I dynamically change the orderBy parameter based on conditions in an Angular application?

I need to dynamically change the orderBy parameter in a table row based on the result of a method. Here is an example of my current tr setup - <tr class="pl" ng-repeat="thing in things | orderBy:'oldId':reverse" ng-class="{'row-error&apo ...

Achieving a Full-Screen Three.js Canvas in React: A step-by-step guide on spanning the view height and width of a webpage

I've been working on tweaking the code found in this particular example: How to connect Threejs to React? This is the snippet of code I am focusing on: import React, { Component } from 'react' import * as THREE from 'three' clas ...

Is there a way to position the twitter embed at the center of a webpage?

I am attempting to embed a Twitter video and twit in such a manner that they are perfectly centered on the page and also take up the entire width of the container (my-container). Here is the HTML code that I currently have: <div class="my-container"&g ...

Unusual behavior observed in Safari regarding transitions and animations

I have encountered a strange bug in Safari 8 and 9 on desktop, as well as iOS 8.4 and newer on mobile devices. I am perplexed by why it is happening and how to resolve it. This issue can be replicated with 100% accuracy. Here are the steps to reproduce: ...

What is the best way to predefine a value for a checkbox in Angular?

Here is the code snippet I am currently using: <input type="checkbox" [(ngModel)]="i.checkt" [ngModelOptions]= {standalone:true} (change)="recovery(i.checkt,i.TherapeuticArea)"> {{i.TherapeuticArea}} I have encountered an issue where setting stan ...

The table on the page shifts position when viewed on different devices, sometimes not remaining between the header and footer

Here is a link to see how my page looks with the header, table, and footer: If you want to see how it looks when responsive (with smaller width), check out this link: I have encountered 2 main problems: The table overlaps the header, The footer ends up ...

Guide on setting the style attribute in HTML within double quotes (as a String)

I have a straightforward question. I am attempting to include a style attribute in an HTML code that is in string format. I am passing the HTML code as a string through a method, and later I need to apply CSS to it based on certain conditions. I attempted ...

HTML5 template design clashes with Smarty framework

I've been working with a simple pattern like this: <input type="text" pattern="[a-z]{2}" required ../> However, it never seems to be valid. The pattern doesn't seem to work as expected. I have only tested it in Firefox so far. Is there ...

Difficulty in arranging DIVs on a flat surface

As a user running Win XP with Firefox, IE, and Google Chrome browsers, I'm encountering an issue where I can't seem to align two DIVs on the same horizontal plane. My goal is to have the left div occupy 24% of the screen width while the right div ...

Modifying Table Cell Background Color in ReactJS: A Guide to Conditionally Updating Cell Color Without Affecting the Entire Row

I am working on changing the background color of a cell based on its value. Currently, I am utilizing the following library: react-data-table-component Procedure: If the value is above 0, then the cell's background color will be red. Otherwise, th ...

Transferring information from a form with multiple fieldsets to php

I am facing an issue with my form that is built using multiple fieldsets. The problem is related to passing values from each fieldset to welcome.php upon submitting the form. However, when I click submit, nothing happens and the values are not displayed. A ...

Please refrain from refreshing the page multiple times in order to receive updated data from the database

Currently, I have created a countdown timer from 00:60 to 00:00. However, once the timer reaches 00:00, I am looking to refresh the page only once in order to retrieve a new value from the database. Does anyone have any suggestions on how to achieve this ...

What are the best ways to engage with a div element using keyboard shortcuts?

Is it possible to enable keyboard shortcuts for interacting with div elements? I am working on a project tailored for seniors who may have difficulty using a mouse. Is there a way to utilize keyboard shortcuts to click on divs and access their contents? H ...

The dilemma between Nuxt Js global CSS and Local CSS

Currently, I am in the process of developing a Nuxt application utilizing an admin template. While I am well-versed in Vue.js, I am finding the aspect of loading assets within Nuxt.js to be a bit perplexing. I am in the process of converting the admin temp ...

Checking the validity of user input statements

When 2 conditions are met, both error links and messages will display on top of each other. If an input is 0, the validation statement for empty inputs will also trigger. Additionally, if one input is not numeric but the other is, PHP will calculate the va ...

Troubleshooting Problems with Adjusting Left Margin Using JQuery

function adjust_width() { $('#cont').toggle(function(){ $('#cont').animate({marginLeft:'0%'}); },function(){ $('#cont').animate({marginLeft:'18.4%'}); }); } The code above is in ...

Problem with fixed element on mobile due to viewport width

I am experiencing an issue with the fixed element's width. Here is a code snippet for reference: https://codepen.io/highfield/pen/PKpXGG <body> <nav class="navbar"> <div style="position:absolute; left:20px; top:12px"> & ...

How to Use jQuery to Detect Audio Position in HTML

Is it possible to use the HTML5 Audio tag with an eventListener or action using Dom/jQuery to trigger an event during playback or at the end? This answer mentions currentTime but lacks examples. Pseudo code provided below. <!DOCTYPE html> <html& ...

I was able to resolve the fixed position issue of a button on Safari for iPhone 3G

Here is the setup I have on jsfiddle: http://jsfiddle.net/zZDqH/ Currently, there is a button fixed at bottom:0px. You can scroll up and down the page, and the button will stay at the bottom of the page. However, when testing in Safari on an iPhone 3G ( ...