Hi everyone,
I’m trying to create an array of videos and run a simple function on them. What I want is for the video to stop playing if the user moves out of the viewport of that video. My code below works correctly for a single video:
export function videoTrailer_viewportLeave(event) {
let videoIsPlaying = $w(‘#videoTrailer’).isPlaying;
if (videoIsPlaying)
{
$w(‘#videoTrailer’).stop();
}
}
I have a few videos on this webpage, and want the same function for all of them, so I thought it’d be better to use an array instead, however it doesn’t seem to work with the array. I’ve looked around the forum, but not sure - I think the line $w(video).onViewportLeave is probably wrong. Would love your insights!
$w . onReady ( function () {
let videoArray = [ ‘#videoTrailer’ , ‘#video1Music’ , ‘#video2Sound’ , ‘#video4Visuals’ ];
videoArray . forEach (( video )=> {
let videoIsPlaying = $w ( video ). isPlaying ;
$w ( video ). onViewportLeave (()=> {
if ( videoIsPlaying ) {
$w ( video ). stop ();
}
});
});
});