A bit late to the party, but in case somebody else is looking for another optionâŚ
Play/Pause video player inside a custom modal (not using the built-in lightbox element)
First, some context. WF uses embed.ly to embed videos when using a video element. Hereâs the documentation.
With this in mind, you can use some JS to target the video element and traverse outwards to target the correct elements.
In my case, I had the following HTML structure
<button class="modal-open-button">Watch Video</button>
<div class="modal-overlay">
<div class="video-player-wrapper">
<img src="icon.svg" class="modal-close-button">
<div class="video-player w-video w-embed"> <!-- this is the WF video element -->
<!-- WF loads an iframe element here... -->
</div>
</div>
</div>
So, this is the JS I came up with to control the video player with the custom modal triggers I have:
// First, load the Embed.ly Library
(function(w, d){
var id='embedly-platform', n = 'script';
if (!d.getElementById(id)){
w.embedly = w.embedly || function() {(w.embedly.q = w.embedly.q || []).push(arguments);};
var e = d.createElement(n); e.id = id; e.async=1;
e.src = ('https:' === document.location.protocol ? 'https' : 'http') + '://cdn.embedly.com/widgets/platform.js';
var s = d.getElementsByTagName(n)[0];
s.parentNode.insertBefore(e, s);
}
})(window, document);
// This controls the videos in modals
embedly('player', function(player){
const videoPlayer = player.frame.elem;
const videoModalOpenButton = videoPlayer.parentElement.parentElement.parentElement.previousElementSibling;
const videoModalOverlay = videoPlayer.parentElement.parentElement.parentElement;
const videoModalCloseButton =videoPlayer.parentElement.previousElementSibling;
const openVideoModalActions = () => {
player.play();
// You can add more action here ...
};
const closeVideoModalActions = () => {
player.pause();
// You can add more action here ...
};
videoModalOpenButton.addEventListener('click', openVideoModalActions, false);
videoModalOverlay.addEventListener('click', closeVideoModalActions, false);
videoModalCloseButton.addEventListener('click', closeVideoModalActions, false);
});
I know the traversing might look silly, but this way, you donât have to use any querySelectors and keep it more flexibleâespecially when you have multiple videos/modals on the same page.
Hope this helps someone out there!
Note 1: In the case of the videoModalOpenButton
var, this is equal to iframe > .video-player > .video-player-wrapper > .modal-overlay > .modal-open-button. Again, the point is to traverse inside-outâfrom the player loaded in the iframe to the elements above.
Note 2: Depending on your HTML structure, you might need to update the script to target the correct elements. Just play around with parentElement
and previousElementSibling
or nextElementSibling