I am targeting multiple similar child divs inside multiple container divs. Subsequently the page scrolls towards the first targeted child div.
Unfortunately, the page doesn't scroll towards the first child div and then stop scrolling. The page keeps scrolling towards all of the child divs one-by-one-by-one.
Without luck I am trying to change this behaviour. I wish the page would just stop scrolling at the first child div.
To see my problem in action please click on any year in the header: http://paintings.directory
Current jQuery code:
var $sections = $('section');
$('#index_year p, section p').on('click', function(e) {
let year = $(this).data('y');
$sections.each(function() {
let $section = $(this);
let $target = $('.' + year, $section);
if ($target.length) {
$('html, body').animate({
scrollTop: $target.offset().top - 128
}, 2000);
$section.removeClass('yearNotFound');
$section.animate({
scrollLeft: $section.scrollLeft() + $target.position().left
}, 2000);
} else {
$section.addClass('yearNotFound');
};
});
});
So far I have unsuccessfully tried to:
- give the child divs I am targeting a class to then scroll to the first child div with that class as follows:
$('html, body').animate({
scrollTop: $('.class:visible:first').offset().top
}, 1000);
- add
return, false;
after the current scrolling code as follows:
$('html, body').animate({
scrollTop: $target.offset().top - 128
}, 2000); return, false;
- use a :first selector as follows:
$( "$target:first" )`
- place the 'scrolling-code' outside of the loop, by making a separate
$('#index_year p, section p').on('click', function() {
$('html, body').animate({
scrollTop: $target.offset().top - 128
}, 2000);
});
- add
scrollIntoView
, I just couldn't phantom where and how to use this.
So far it's not working out for me and I am therefor looking for help.