Webflow sync, pageviews & more.
NEW
Answers

How can I fix the issue of the sound of a YouTube video continuing to play in the background after closing a pop-up modal on my Webflow site?

To fix the issue of the sound of a YouTube video continuing to play after closing a pop-up modal on your Webflow site, you can use JavaScript to control the YouTube video playback.

First, you'll need the YouTube iframe API enabled on your project. To do this, go to your Google Cloud Console and create a new project. Enable the YouTube Data API on this project and generate an API key. Next, add the following script to the head tag of your Webflow project:

```

```

Next, you'll need to add a custom code component to your modal. Open the modal and add a custom code component in the desired location. Inside the custom code component, you can add the following code:

```html

```

This code creates a YouTube player object and listens for state changes. When the video ends or is paused, you can use JavaScript to hide the modal or perform any other desired action.

In your modal trigger button or close button, you'll need to add an event listener to stop the video when the modal is closed:

```javascript
const closeModalButton = document.querySelector('.close-modal-button');
closeModalButton.addEventListener('click', function() {
player.pauseVideo();
});
```

Make sure to replace `.close-modal-button` with the CSS selector for your close button.

By incorporating this code, the YouTube video should stop playing when the modal is closed. Remember to also adjust the specific classes and identifiers to match your own Webflow setup.

It's worth noting that this solution assumes you are using the YouTube iframe embed method on your site. If you are using a different embed method or a different video player, you may need to modify the code accordingly.

Rate this answer

Other Webflow Questions