I'm interested in how to use the scrollIntoView method to scroll an element with a specific target ID to the center of the page when clicking on a link.
Here's the code I currently have:
$("a").click(function () {
var target = $($(this).attr("href"));
target[0].scrollIntoView({
behavior: "smooth",
block: "center"
})
});
I've been able to successfully log the targeted element and make changes using other methods, but I'm struggling to figure out how to center it when scrolling.
You can view an example of what I'm trying to achieve in this Codepen, which uses scroll-margin-top: 50vh
instead.
Any help would be greatly appreciated. Thank you!