I have looked around for the solution but I haven’t found any efficient one so far.
I have quite a few modals in my website. Some of them have the video embed inside. I want to be able to stop/close them when I click the button .close-modal which will also close the modal.
How can I do that? So I need a script that says when you click the element with class .close-modal all the element with the class .embed will stop. Something like this.
This should help you out. Get that script to work when you click the close modal and your videos should stop. If you need someone to implement it for you just send me a private message.
No no no . You gotta write that code in a way that it works according to your website. It’s not only a copy and paste job.
For example:
You gotta specify the ID for the video you want to stop. If you’re talking about multiple videos, you gotta create an expression that selects only the video that is inside the modal.
Then, the function pauseVid needs to be called when a determined trigger is fired.
If you want your users to be able to get the video back when they open the modal again, you should also configure the playVid function.
Thank you for you help but if I knew how to code I wouldn’t be here asking for help.
I have multiple videos in my website, as I said in my first post I want to use classes and not IDs simply because I can’t apply the same ID to multiple videos.
I really don’t mind what happen to the video after the modal is closed, the goal is stopping the audio that comes out of the video when the modal is closed. It’s not nice having a random audio in background while navigating the website.
…so the script can also stop/reset all the video of the website, I really don’t mind.
Giuseppe, the thing is: We are helping. We are pointing you to the right path. We simply can’t teach you JavaScript in a forum post. It feels like you want us to create it for you so you can just copy and paste it inside your website. That’s not how web-developing works. We don’t even have your read-only. We don’t know what platform is hosting your video (each platform behaves differently)
These kind of situations are normally solved in 3 ways. You either:
Learn earn about the things we mentioned.
Share your read-only here and wait for someone who has the time to get it completely done for you and you just paste it into the website
Hire someone who has the skill set to implement it on your website.
I don’t want you to implement my website. I just asked a simple general question that’s why i didn’t put the read only link because there is no need for that.
I asked if there is a function that does this:
click class .close-modal = stop all videos with class .embed-video
I man are we on the webflow forum…I am talking about webflow here not coding. I need to fill the gaps (things that webflow doesn’t have) you also need to realise that this is an app like squarespace or similar so 50% of the people on this forum don’t know how to code.
If it’s too complicate for you, you don’t need to help me. I thought it would be very easy to make but apparently it’s a very complicate script.
No way to stop videos by event/click without code (For now). You don’t have to be JS-ninja to copy-paste 4-5 lines of scripts before the body.
Hard to answer without example. In general on click on X - you should loop throw all videos and stop (But “stop” also - Vimeo, youtube, html5 video - each one with another code/solutions).
Also - You should solve secnarios when the user close the modal without close-modal button (By esc, by click on the overlay area and so on).
@Siton_Systems Thank you for that explanation. However, if the button with the .stop-button also is supposed to trigger a Webflow interacition to close the modal, the interaction stops working (the button only stops the video).
Is there anyway to have a button both have the stop-button functionalty as in your solution AND fire an webflow interaction?
Based on @Siton_Systems excellent reply for Vimeo videos, I’ve put together the equivalent using the YouTube API too.
1 - Add a video
Add ID of youtubevideo for all of your videos. This could work for classes too but not without changing it. You must include ?enablejsapi=1 at the end of the src in the iframe below:
2 - Add classes to the modal close triggers in your Webflow
Or make a note of the existing classes.
In the code below I was using .close-modal-button and .modal-wrapper but you can use as many or few as you like, separated by a comma.
3 - Add custom code
Copy-paste before the close of body (it works in the head too though)
<script>
// Pause YouTube videos when modal windows are closed https://developers.google.com/youtube/iframe_api_reference
var tag = document.createElement('script');
tag.id = 'iframe-demo';
tag.src = 'https://www.youtube.com/iframe_api';
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('youtubeplayer', {
events: {
'onReady': onPlayerReady
}
});
}
function onPlayerReady(event) {
onModalWindowClosePauseVideo();
}
function onModalWindowClosePauseVideo() {
// Modal window close triggers must have one of the classes in this querySelectorAll for this to work
let closeModalTriggers = document.querySelectorAll('.close-modal-button, .modal-wrapper');
for(var i = 0; i < closeModalTriggers.length; i++) {
var closeModalTrigger = closeModalTriggers[i];
closeModalTrigger.onclick = function() {
player.pauseVideo()
}
}
}
</script>
Satuday afternoon and here i am finding out about this missing feature. Webflow doesnt give you the option to make a simples video galley. I think instead of wasting time making BLM propaganda about “master” or whatever on top of your projects, they should focus on developing basic and essential features. I am getting tired of webflow since everything has to be done by raw coding, so what´s the point in subscribing to webflow? It doesnt make sense.
Thanks for the steps but when you’re not using an iframe and try to ad the ‘?enablejsapi=1’ at the end of the source youtube code, webflow simple can’t accept the code and its automatically deleted when using the elements youtube setting on webflow.
Is there another way when your are not using iframe, just classes?
I don’t post the preview here coz you can’t see the video playing.