I have a page where a scroll bar containing table rows with divs in them is dynamically generated from the database. Each table row acts like a link, sort of like you\'d see
This solution belongs to @Arseniy-II, I have just simplified it into a function.
function _scrollTo(selector, yOffset = 0){
const el = document.querySelector(selector);
const y = el.getBoundingClientRect().top + window.pageYOffset + yOffset;
window.scrollTo({top: y, behavior: 'smooth'});
}
Usage (you can open up the console right here in StackOverflow and test it out):
_scrollTo('#question-header', 0);
I'm currently using this in production and it is working just fine.