https://jsfiddle.net/popnoodles/dda5z70r/5/
Check for collision each animate step.
Add your code to play the sound where I have added a class to show this visually.
NB I added the ID bar
to the bar that moves because it's faster to search the DOM for an ID than a class. If you have more than one moving bar I can alter this.
$( "#go" ).click(function() {
var stop = $(".stop").offset().left;
var obj = document.getElementById("audio");
// how to say whenever .move div arrives at .sound element, the sound associated to that .sound div starts to play?
//obj.play();
$( "#bar" ).animate({
opacity: 0.25,
left: stop-($(".stop").width()+$(this).width()),
}, {
duration: 5000,
step: function(){
$('.sound').each(function(){
if (collision($(this), $('#bar'))){
$(this).addClass('playme');
} else{
$(this).removeClass('playme');
}
});
}, complete: function() {
$(this).css('left',0);
$(this).css('height','100%');
}});
});
function collision($div1, $div2) {
var x1 = $div1.offset().left;
var r1 = x1 + $div1.width();
var x2 = $div2.offset().left;
var r2 = x2 + $div2.width();
if (r1 < x2 || x1 > r2) return false;
return true;
}