I'm trying to make the inside of a parent div scroll to the next child div on a set interval. However, the scroll only works half the time and I can't figure out why. It should scroll through all 8 children, but only goes through about half.
In this jsfiddle I made, the offset is just 1px off every other time when it should be ~250px off. In my actual code its 0px off, when it should be ~250px off.
https://jsfiddle.net/rLeLogx0/3/
Here's the JS:
//scroll to 2nd one first
var index = 1;
setInterval(function(){
var parent = $('.parent');
var children = parent.find('.child');
var target = children.eq(index);
var offset = target.offset().top - $('.parent').offset().top;
//ISSUE: outputs the "same" value every other time
console.log(target.offset().top);
parent.animate({
scrollTop: offset
}, 200);
index = (index+1) % children.length;
}, 1000);