I have an onclick that triggers this function:
function thread2M() {
var elmnt = document.getElementById("scroll2");
elmnt.scrollIntoView({behavior: "smooth", block: "center", inline: "center"});
}
which scrolls the page to a particular pixel (a div) and centers it. Of course, scrollIntoViewOptions doesn't work in Safari, so I'd like to fall back to scrollIntoView() or scrollTo().
I've tried:
function thread2M() {
var elmnt = document.getElementById("scroll2");
try {
elmnt.scrollIntoView({behavior: "smooth", block: "center", inline: "center"});
} catch {
elmnt.scrollIntoView(true);
}
}
and
function thread2M() {
if (typeof document.body.scrollIntoView === 'function') {
var elmnt = document.getElementById("scroll2");
elmnt.scrollIntoView({behavior: "smooth", block: "center", inline: "center"});}
else {window.scrollTo(782, 280);}
}
but no scroll is initiated. I don't even have basic knowledge of how JS works, so the answer is probably related to fundamentals, but I can't find the solution out there. I think I just need to test if scrollIntoViewOptions works, if true, run one script, if false, run another.
Answers appreciated, thank you.