I have 4 divs with heights set to window height. I want on each scroll down to scroll to the next one, however after the first scroll down it just keeps on scrolling, seemingly ignoring is:animated
<style>
.div {
width: 100%;
}
.red {
background: red;
}
.yellow {
background: yellow;
}
.green {
background: green;
}
.blue {
background: blue;
}
</style>
<div id="id0" data-id="0" class="nbhd red scrolledto"></div>
<div id="id1" data-id="1" class="nbhd yellow"></div>
<div id="id2" data-id="2" class="nbhd green"></div>
<div id="id3" data-id="3" class="nbhd blue"></div>
function setHeights() {
$('.nbhd').css('height', window.innerHeight);
}
function scrollSections() {
if ($('body, html').is(':animated')) {
return;
}
var currentSectionId = $('.nbhd.scrolledto').data('id');
currentSectionId++;
$('.scrolledto').removeClass('scrolledto');
var section = $('#id'+currentSectionId);
section.addClass('scrolledto');
var pos = section.offset().top;
$('body, html').animate({scrollTop: pos}, 1000, function() {
console.log('animated');
});
}
setHeights();
$( window ).on( "scroll", function() {
scrollSections();
});
fiddle: https://jsfiddle.net/2d47k6af/
I also get animated
logged 6 times for some reason, I expected 3.