Hello everyone!
I am looking to include multiple videos on my website and have them start playing automatically when a user scrolls and the videos are in view. I want the speakers to be muted by default using the mute() function.
Previously, I had success with one video on a single page, but now that I'm trying to add multiple videos, I'm encountering some issues. Can you help me figure out what I'm missing in my code?
Below is the HTML part:
<div class="video-container">
<div data-id="olBOo_S5AHY"></div>
</div>
<div class="video-container">
<div data-id="3IXKIVZ9T-U"></div>
</div>
<div class="video-container">
<div data-id="LAQDcnwwspQ"></div>
</div>
And this is the JavaScript part:
function onYouTubePlayerAPIReady() {
var players = document.querySelectorAll('.video-container div')
for (var i = 0; i < players.length; i++) {
new YT.Player(players[i], {
width: 600,
height: 400,
videoId: players[i].dataset.id,
events: {
onReady: initialize
}
});
}
}
function initialize(){
players[i].mute(); // Need help fixing this issue
var waypoints = jQuery('.video-container').waypoint(function() {
if( players[i]) { // Need help fixing this issue
var fn = function(){
players[i].playVideo(); // Need help fixing this issue
}
setTimeout(fn, 1000);
}
}, {
offset: '50%'
})
}
The videos are displaying on my site, but I'm struggling to add these events to each individual video. What am I doing wrong? Any assistance would be greatly appreciated!
Thank you so much!
Melanie