How can I pause/stop a video from playing when a modal is closed in Webflow?
Published on
September 22, 2023
To pause or stop a video from playing when a modal is closed in Webflow, you can use a combination of Webflow interactions and custom code. Here are the steps to achieve this:
- Create your modal and add a video element to it. Make sure the video is set to autoplay.
- Set a trigger to open the modal, such as a button or a link.
- Add a close button inside the modal to trigger the modal's close interaction.
- Go to the Interactions panel and create a new interaction for the modal's close button.
- Add an animation to the close button to hide or fade out the modal.
- In the same interaction panel, add a second animation for the video element inside the modal.
- In this animation, set the video's playback state to "paused" or "stop".
- Publish your site and test the modal. The video should stop playing when the modal is closed.
If the above steps don't work for you, you can try using custom code. Follow the steps below to achieve this:
- Add a class to the modal's close button (e.g., "close-btn").
- Open the page settings and add a custom code block before the closing tag.
- In the custom code block, add the following JavaScript code:
<script>document.querySelector('.close-btn').addEventListener('click', function() { var video = document.querySelector('.video-element-class'); video.pause();});</script>
- Replace '.close-btn' with the class selector of your modal's close button.
- Replace '.video-element-class' with the class selector of your video element inside the modal.
- Save and publish your site. The video should now pause or stop when the modal is closed.
Following these steps will allow you to pause or stop a video from playing when a modal is closed in Webflow.
Additional Questions:
- How do I create a modal in Webflow?
- Can I autoplay a video in Webflow?
- How can I add custom code in Webflow?