Created
February 26, 2016 09:07
-
-
Save jhealey5/fa55714dc464fcf65614 to your computer and use it in GitHub Desktop.
No Scroll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // NoScroll https://github.com/davidtheclark/no-scroll | |
| (function(root) { | |
| var scrollbarSize; | |
| function getScrollbarSize() { | |
| if (typeof scrollbarSize !== 'undefined') return scrollbarSize; | |
| var doc = document.documentElement; | |
| var dummyScroller = document.createElement('div'); | |
| dummyScroller.setAttribute('style', 'width:99px;height:99px;' | |
| + 'position:absolute;top:-9999px;overflow:scroll;'); | |
| doc.appendChild(dummyScroller); | |
| scrollbarSize = dummyScroller.offsetWidth - dummyScroller.clientWidth; | |
| doc.removeChild(dummyScroller); | |
| return scrollbarSize; | |
| } | |
| function hasScrollbar() { | |
| return document.documentElement.scrollHeight > window.innerHeight; | |
| } | |
| function on(options) { | |
| if (typeof document === 'undefined') return; | |
| var doc = document.documentElement; | |
| var rightPad = parseInt(getComputedStyle(doc)['padding-right'], 10); | |
| doc.style.overflow = 'hidden'; | |
| if (hasScrollbar()) { | |
| rightPad += getScrollbarSize(); | |
| doc.style.paddingRight = rightPad + 'px'; | |
| } | |
| } | |
| function off() { | |
| if (typeof document === 'undefined') return; | |
| var doc = document.documentElement; | |
| doc.style.overflow = ''; | |
| doc.style.paddingRight = ''; | |
| } | |
| var noScroll = { | |
| on: on, | |
| off: off, | |
| }; | |
| if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { | |
| module.exports = noScroll; | |
| } | |
| else { | |
| root.noScroll = noScroll; | |
| } | |
| })(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment