.close-modal button, ensuring videos are paused or reset when the modal closes.Creating a script to close modals with embedded videos in Webflow involves using custom code and ensuring that videos stop playing when the modal is closed.
<head> tag or the "Footer Code" section, depending on your preference.
.close-modal button to the modal's closing function.
<script>
// jQuery is typically available in Webflow; ensure jQuery is included in your project.
$(document).ready(function() {
$('.close-modal').click(function() {
// Hide the modal container
$(this).closest('.modal-container').hide();
// Find the video elements within the modal and pause them
$(this).closest('.modal-container').find('video, iframe').each(function() {
// For HTML5 video elements
if ($(this).is('video')) {
this.pause();
this.currentTime = 0; // reset the video to the start
}
// For iframes (like YouTube or Vimeo videos)
if ($(this).is('iframe')) {
let src = $(this).attr('src');
$(this).attr('src', ''); // temporarily remove the src
$(this).attr('src', src); // reassign the src to reset the video
}
});
});
});
</script>
*.close-modal button is clicked.
To close modals with embedded videos in Webflow, add custom JavaScript that targets the modal’s .close-modal button. This script should also reset or pause any embedded videos when the modal is closed. Always test the implementation in a live environment to ensure it functions as expected.