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?

TL;DR
  • Properly configure the modal's close action to stop video playback by ensuring the YouTube video is embedded correctly and linked to a trigger.
  • Add custom JavaScript in Webflow's Settings to halt the video using document.querySelector and test the functionality by publishing the site.

To resolve the issue of YouTube video sound continuing after closing a pop-up modal in Webflow, you'll need to properly configure the modal's close action to stop the video playback.

1. Ensure Popup Modal Links Correctly

  • Check that the modal is properly linked to a button or trigger that opens and closes the modal.
  • Verify that the YouTube video is correctly embedded within the modal.

2. Add Custom Code to Stop Video

  • Go to Project Settings in your Webflow Dashboard.
  • Select the Custom Code tab to insert additional JavaScript.

3. Implement JavaScript for Video Stop

  • Enter this JavaScript code in the Footer Code section:
  • Identify the video iframe using JavaScript.
  • Add code to stop the video when the modal is closed.

Here's an inline explanation: Use document.querySelector to select the video iframe and call the pause method or set the src to an empty string when the modal close button is clicked.

4. Test the Modal Functionality

  • Publish your site and test the modal by opening and closing it to ensure the sound stops when closed.

Summary

Ensure the YouTube video inside a Webflow modal stops playing after closing by adding custom JavaScript that targets the iframe element and controls its playback state when the modal is dismissed.

Rate this answer

Other Webflow Questions