Javascript .scrollIntoView(boolean)
provide only two alignment option.
What if I want to scroll the view such t
You can do it in two steps :
myElement.scrollIntoView(true);
var viewportH = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
window.scrollBy(0, -viewportH/2); // Adjust scrolling with a negative value here
You can add the height of the element if you want to center it globaly, and not center its top :
myElement.scrollIntoView(true);
var viewportH = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
window.scrollBy(0, (myElement.getBoundingClientRect().height-viewportH)/2);