Skip to content

Instantly share code, notes, and snippets.

@ptrin ptrin/example.js
Created May 13, 2020

What would you like to do?
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);
}.bind(this), 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.