Skip to content

Instantly share code, notes, and snippets.

@luislobo14rap
Created October 10, 2022 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luislobo14rap/961ee82c0983a5ebb3e3c1df460e7df7 to your computer and use it in GitHub Desktop.
Save luislobo14rap/961ee82c0983a5ebb3e3c1df460e7df7 to your computer and use it in GitHub Desktop.
getScrolls.js
// getScrolls.js v1
const getScrolls = {
element: window,
x: 'scrollX',
y: 'scrollY'
};
if (!('scrollX' in window && 'scrollY' in window)) {
if ('pageXOffset' in window && 'pageYOffset' in window) {
getScrolls.x = 'pageXOffset';
getScrolls.y = 'pageYOffset';
} else if (document.documentElement.clientHeight != 0) {
getScrolls.element = document.documentElement;
getScrolls.x = 'scrollLeft';
getScrolls.y = 'scrollTop';
} else {
getScrolls.element = document.body;
getScrolls.x = 'scrollLeft';
getScrolls.y = 'scrollTop';
};
};
window.getScrollX = function() {
return getScrolls.element[getScrolls.x];
};
window.getScrollY = function() {
return getScrolls.element[getScrolls.y];
};
// getScrolls.min.js v1
window.getScrolls={element:window,x:"scrollX",y:"scrollY"};"scrollX"in window&&"scrollY"in window||("pageXOffset"in window&&"pageYOffset"in window?(getScrolls.x="pageXOffset",getScrolls.y="pageYOffset"):"documentElement"in document||"parentNode"in document.body?(getScrolls.element=document.documentElement||document.body.parentNode,getScrolls.x="scrollLeft",getScrolls.y="scrollTop"):(getScrolls.element=document.body,getScrolls.x="scrollLeft",getScrolls.y="scrollTop"));window.getScrollX=function(){return getScrolls.element[getScrolls.x]},window.getScrollY=function(){return getScrolls.element[getScrolls.y]};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment