Skip to content

Instantly share code, notes, and snippets.

@uditalias
Created September 5, 2017 08:02
Show Gist options
  • Save uditalias/e4605d7f457cffd188879e3c690d20bb to your computer and use it in GitHub Desktop.
Save uditalias/e4605d7f457cffd188879e3c690d20bb to your computer and use it in GitHub Desktop.
ADHD module
const ADHDHelpers = (function () {
let module = {
scrollTop: 0,
isDisabled: false,
shouldDisplayScrollbar: false,
disablePageScroll: function () {
if (this.isDisabled) {
return;
}
let $body = document.body;
this.isDisabled = true;
this.shouldDisplayScrollbar = $body.scrollHeight > window.innerHeight;
window.requestAnimationFrame(() => {
this.scrollTop = $body.scrollTop;
$body.style.position = 'fixed';
$body.style.top = `-${this.scrollTop}px`;
$body.style.width = '100%';
if (this.shouldDisplayScrollbar) {
$body.style.overflowY = 'scroll';
}
});
},
enablePageScroll: function () {
if (!this.isDisabled) {
return;
}
this.isDisabled = false;
let $body = document.body;
window.requestAnimationFrame(() => {
$body.style.position = '';
$body.style.top = '';
$body.style.width = '';
$body.scrollTop = this.scrollTop;
this.scrollTop = 0;
if(this.shouldDisplayScrollbar) {
$body.style.overflowY = '';
}
});
}
};
return {
disablePageScroll: module.disablePageScroll.bind(module),
enablePageScroll: module.enablePageScroll.bind(module)
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment