Skip to content

Instantly share code, notes, and snippets.

@ptrin
ptrin / example.js
Created May 13, 2020 13:15
Adjust scroll position on focus (if necessary)
const oldHTMLFocus = HTMLElement.prototype.focus;
HTMLElement.prototype.focus = function () {
oldHTMLFocus.apply(this, arguments);
setTimeout(function() {
const currentScrollPos = window.scrollY || window.scrollTop;
const offset = $(this).offset().top;
// if difference between currentScrollPos and offset from top of document is less than x, adjust
const difference = Math.abs(currentScrollPos - offset);
if (difference < 100) {
window.scrollTo(0, currentScrollPos - 100);